[
https://issues.apache.org/jira/browse/GROOVY-12454?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152945#comment-18152945
]
ASF GitHub Bot commented on GROOVY-12454:
-----------------------------------------
pditommaso opened a new pull request, #3382:
URL: https://github.com/apache/groovy/pull/3382
Follow-up to GROOVY-12454 (#3013). That change shipped in 5.1.4 and 6.0.1,
and is scheduled for 4.0.34. It makes some statically compiled constructor
calls that work in 4.0.33, 5.1.3 and 6.0.0 fail at runtime.
### Problem
When the constructor's Map parameter comes from default arguments, `new
Foo(map)` with a non-literal map no longer calls `Foo(Map)`. It is rewritten
into a map-style call: the no-arg constructor followed by property setters.
```groovy
@groovy.transform.CompileStatic
class Foo {
final boolean enabled
Foo() {}
Foo(Map opts, Map<String,String> env = null) { this.enabled =
opts.enabled as boolean }
}
@groovy.transform.CompileStatic
class Main {
static void main(String[] args) {
Map m = [enabled: true]
new Foo(m)
// 4.0.33, 5.1.3, 6.0.0: calls Foo(Map)
// 5.1.4, 6.0.1: new Foo() + setProperty('enabled', true)
// -> ReadOnlyPropertyException: Cannot set
read-only property: enabled
}
}
```
If the class has no no-arg constructor, for example `Bar(Config c, String
kind = 'process')` where `Config extends LinkedHashMap`, the compiler emits
`Bar.<init>()` anyway, and the call fails with `NoSuchMethodError` at runtime.
Dynamic Groovy and a hand-written `Foo(Map)` constructor are not affected.
We found this while upgrading
[Nextflow](https://github.com/nextflow-io/nextflow) to 5.1.4: several hundred
tests fail in
[nextflow-io/nextflow#8137](https://github.com/nextflow-io/nextflow/pull/8137).
### Cause
For `Foo(Map)`, the type checker selects the stub that
`MethodNodeUtils.withDefaultArgumentMethods` creates from `Foo(Map, Map =
null)`. The stub's code is `EmptyStatement.INSTANCE`, which is the same
instance as `StaticTypeCheckingVisitor.GENERATED_EMPTY_STATEMENT`. Because the
stub is created on every lookup, it is not found by identity in
`getDeclaredConstructors()`. `ConstructorCallTransformer` therefore treats it
as the synthetic map-style constructor.
Before GROOVY-12454, only `MapExpression` arguments reached that branch.
Since GROOVY-12454, every Map-typed argument does.
### Fix
For a non-literal map, `ConstructorCallTransformer` now checks whether the
target is a variant generated for default arguments of a declared constructor
(same parameters as one from `withDefaultArgumentMethods`). If it is, the call
is left as a normal constructor call. This restores the 5.1.3 / 4.0.x behaviour.
Map literals keep their current handling, which has been the same since 4.x.
A real map-style call on a class that only has a no-arg constructor (the
GROOVY-12454 case) still works.
### Tests
Three tests were added to `ConstructorsSTCTest`, so they also run under
static compilation through `StaticCompileConstructorsTest`:
- `testNonLiteralMapWithDefaultArgumentConstructor`: a typed variable, a
cast, a method result, and an elvis with `Collections.emptyMap()`. Fails
without the fix with `ReadOnlyPropertyException`.
- `testNonLiteralMapSubtypeWithDefaultArgumentConstructor`: a `Map` subtype
parameter and no no-arg constructor. Fails without the fix with
`NoSuchMethodError`.
- `testNonLiteralMapWithDefaultArgumentConstructorOfOtherType`: a
default-argument constructor of another type does not block the map-style call.
All `org.codehaus.groovy.classgen.asm.sc.*` and `groovy.transform.stc.*`
tests pass: 6,097 tests, 0 failures.
The same change applies to GROOVY_5_1_X and GROOVY_6_0_X, which have the
same `ConstructorCallTransformer` code. A backport would be appreciated,
ideally before 4.0.34 is released.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
> "Target constructor for constructor call expression hasn't been set" when
> using map-constructor-call with method-returned map under static compilation
> ------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12454
> URL: https://issues.apache.org/jira/browse/GROOVY-12454
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.5.23, 3.0.25, 5.0.3, 4.0.33, 5.1.3, 6.0.0
> Reporter: Björn Kautler
> Assignee: Paul King
> Priority: Major
> Fix For: 4.0.34, 7.0.0-alpha-1, 5.1.4, 6.0.1
>
>
> Given the snippet
> {code:groovy}
> class Person2 {
> Â def name
> Â def age
> }
> def <T> T record(T value) {
> Â value
> }
> Map recordMap(Map value) {
> Â value
> }
> @groovy.transform.CompileStatic
> def foo() {
> Â [
> Â Â new Person2(record([name: 'fred', age: 25])),
> Â Â new Person2([name: 'fred', age: 25]),
> Â Â new Person2(recordMap([name: 'fred', age: 25]))
> Â ]
> }
> foo() {code}
> Â
> With the {{CompileStatic}} line commented out, all versions mentioned as
> affected compile fine and run fine, returning a list with three identical
> person instances with the properties set.
> With the snippet as given, none of the versions can compile the snippet due
> to the first and last constructor lines.
> Except of 3.0.25 all versions tell for both lines "Target constructor for
> constructor call expression hasn't been set".
> 3.0.25 shows the same for the first one, and "Cannot find matching method
> Person2#<init>(java.util.Map). Please check if the declared type is correct
> and if the method exists." for the last one.
> With {{TypeChecked}} instead of {{CompileStatic}} it also works fine in all
> versions except 3.0.25. 3.0.25 with only type-checke can compile the first
> line but still has the same error for the last line.z
> Â
> So it seems the SC does not properly use the "map-constructor-call" semantics
> if a normal or generic method call returned type {{{}Map{}}}, plus 3.0.25
> having some additional problem.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)