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)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]