[ 
https://issues.apache.org/jira/browse/GROOVY-12846?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King updated GROOVY-12846:
-------------------------------
    Description: 

**Summary:** SC: constructor call with a Map ignores a matching constructor 
generated from default arguments

**Affects:** 5.1.4, 6.0.1 (regression); 4.0.33, 5.1.3 and 6.0.0 too if you want 
the literal part covered
**Fix:** 4.0.34, 5.1.5, 6.0.2, 7.0.0-alpha-1

**Description:**

```
h3. Regression: a Map that isn't a literal

Since GROOVY-12454, a statically compiled constructor call that passes a Map 
expression may be compiled as a map-style call (no-arg constructor, then set 
each property) even though the class has a matching constructor. This happens 
when both:
* the matching constructor exists only through default arguments, e.g. 
{{A(Map)}} from {{A(Map opts, String env = 'x')}}
* the class is compiled in the same compilation unit as the call

{code:groovy}
import groovy.transform.CompileStatic

@CompileStatic
class A {
    final Map opts
    A(Map opts, String env = 'x') { this.opts = opts }
}

@CompileStatic
class B {
    final boolean enabled
    B() { enabled = false }
    B(Map opts, String env = 'x') { enabled = opts.enabled as boolean }
}

@CompileStatic
class C {
    boolean enabled
    String how = 'no-arg + setters'
    C() {}
    C(Map opts, String env = 'x') { how = 'Map constructor' }
}

@CompileStatic def a(Map m) { new A(m).opts }
@CompileStatic def b(Map m) { new B(m).enabled }
@CompileStatic def c(Map m) { new C(m).how }
{code}

With {{[enabled: true]}} passed to each method:

||Call||4.0.33, 5.1.3, 6.0.0||5.1.4, 6.0.1||Dynamic (all five)||
|{{new A(m)}}|map constructor|{{NoSuchMethodError}}: {{A.<init>()}}|map 
constructor|
|{{new B(m)}}|map constructor|{{ReadOnlyPropertyException}}: {{enabled}}|map 
constructor|
|{{new C(m)}}|map constructor|no-arg + setters (silently)|map constructor|

There is no compile error in any of these cases. Constructor parameters of a 
Map subtype and {{super(map)}}/{{this(map)}} calls are also affected, as are 
the constructors generated by e.g. {{@TupleConstructor}} when the first 
property is a Map.

This was found by Nextflow when upgrading to 5.1.4: 
https://github.com/nextflow-io/nextflow/pull/8137#issuecomment-7284693772

GROOVY-12454 is also in the unreleased 4.0.34, so that release would have the 
regression as well.

h3. Existing inconsistency: named arguments and map literals

The same wrong choice has long been made for named arguments and map literals. 
It's present in at least 4.0.33, 5.1.3 and 6.0.0, so it isn't part of the 
regression. Using {{C}} from above, and a class with no no-arg constructor:

{code:groovy}
@CompileStatic
class D {
    boolean enabled
    String how
    D(Map opts, String env = 'x') { how = 'Map constructor' }
}

@CompileStatic def c() { new C(enabled: true).how }
@CompileStatic def d() { new D(enabled: true).how }
{code}

||Call||{{@CompileStatic}}||Dynamic||
|{{new C(enabled: true)}}|no-arg + setters|map constructor|
|{{new D(enabled: true)}}|{{NoSuchMethodError}}: {{D.<init>()}}|map constructor|

{{@TypeChecked}} agrees with dynamic Groovy. The {{@CompileStatic}} result also 
differs depending on whether the class is in the same compilation unit: if 
{{C}} was compiled earlier, the map constructor is called. In addition, {{new 
C(nope: 1)}} passes type checking, since {{C(Map)}} accepts any key, but then 
fails at runtime with {{MissingPropertyException}}.

h3. Cause

{{ConstructorCallTransformer}} recognises a map-style call by its target having 
{{GENERATED_EMPTY_STATEMENT}} as its code. That is {{EmptyStatement.INSTANCE}}, 
which is also the code of the stubs that 
{{MethodNodeUtils.withDefaultArgumentMethods}} creates for default arguments. 
The transformer tries to skip declared constructors, but it compares by 
identity and the stubs are created afresh for each lookup. GROOVY-12454 
extended the rewrite from map literals to any Map expression, which exposed 
this.

h3. Expected behaviour

A constructor that accepts the map, including one implied by default arguments, 
is used. The no-arg constructor and setters are used only if there is no such 
constructor. {{MetaClassImpl#invokeConstructor}} and the static type checker 
already follow this rule.

The GROOVY-12454 fix itself should be kept: a Map expression must still give a 
map-style call when no constructor matches.

h3. Scope

* *7.0 and 6.0.2:* fix both parts, giving static compilation the same result as 
dynamic Groovy. The second part changes behaviour for code which relied on the 
setters being called, so it needs an entry in {{COMPATIBILITY.md}}. The 
documentation on named parameters should also state the precedence.
* *5.1.5 and 4.0.34:* fix the regression only. Named arguments and map literals 
are unchanged.

PR for the first part: https://github.com/apache/groovy/pull/3382


  was:
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.


> Fix regression for: "Target constructor for constructor call expression 
> hasn't been set" when using map-constructor-call with method-returned map 
> under static compilation
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12846
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12846
>             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
>
> **Summary:** SC: constructor call with a Map ignores a matching constructor 
> generated from default arguments
> **Affects:** 5.1.4, 6.0.1 (regression); 4.0.33, 5.1.3 and 6.0.0 too if you 
> want the literal part covered
> **Fix:** 4.0.34, 5.1.5, 6.0.2, 7.0.0-alpha-1
> **Description:**
> ```
> h3. Regression: a Map that isn't a literal
> Since GROOVY-12454, a statically compiled constructor call that passes a Map 
> expression may be compiled as a map-style call (no-arg constructor, then set 
> each property) even though the class has a matching constructor. This happens 
> when both:
> * the matching constructor exists only through default arguments, e.g. 
> {{A(Map)}} from {{A(Map opts, String env = 'x')}}
> * the class is compiled in the same compilation unit as the call
> {code:groovy}
> import groovy.transform.CompileStatic
> @CompileStatic
> class A {
>     final Map opts
>     A(Map opts, String env = 'x') { this.opts = opts }
> }
> @CompileStatic
> class B {
>     final boolean enabled
>     B() { enabled = false }
>     B(Map opts, String env = 'x') { enabled = opts.enabled as boolean }
> }
> @CompileStatic
> class C {
>     boolean enabled
>     String how = 'no-arg + setters'
>     C() {}
>     C(Map opts, String env = 'x') { how = 'Map constructor' }
> }
> @CompileStatic def a(Map m) { new A(m).opts }
> @CompileStatic def b(Map m) { new B(m).enabled }
> @CompileStatic def c(Map m) { new C(m).how }
> {code}
> With {{[enabled: true]}} passed to each method:
> ||Call||4.0.33, 5.1.3, 6.0.0||5.1.4, 6.0.1||Dynamic (all five)||
> |{{new A(m)}}|map constructor|{{NoSuchMethodError}}: {{A.<init>()}}|map 
> constructor|
> |{{new B(m)}}|map constructor|{{ReadOnlyPropertyException}}: {{enabled}}|map 
> constructor|
> |{{new C(m)}}|map constructor|no-arg + setters (silently)|map constructor|
> There is no compile error in any of these cases. Constructor parameters of a 
> Map subtype and {{super(map)}}/{{this(map)}} calls are also affected, as are 
> the constructors generated by e.g. {{@TupleConstructor}} when the first 
> property is a Map.
> This was found by Nextflow when upgrading to 5.1.4: 
> https://github.com/nextflow-io/nextflow/pull/8137#issuecomment-7284693772
> GROOVY-12454 is also in the unreleased 4.0.34, so that release would have the 
> regression as well.
> h3. Existing inconsistency: named arguments and map literals
> The same wrong choice has long been made for named arguments and map 
> literals. It's present in at least 4.0.33, 5.1.3 and 6.0.0, so it isn't part 
> of the regression. Using {{C}} from above, and a class with no no-arg 
> constructor:
> {code:groovy}
> @CompileStatic
> class D {
>     boolean enabled
>     String how
>     D(Map opts, String env = 'x') { how = 'Map constructor' }
> }
> @CompileStatic def c() { new C(enabled: true).how }
> @CompileStatic def d() { new D(enabled: true).how }
> {code}
> ||Call||{{@CompileStatic}}||Dynamic||
> |{{new C(enabled: true)}}|no-arg + setters|map constructor|
> |{{new D(enabled: true)}}|{{NoSuchMethodError}}: {{D.<init>()}}|map 
> constructor|
> {{@TypeChecked}} agrees with dynamic Groovy. The {{@CompileStatic}} result 
> also differs depending on whether the class is in the same compilation unit: 
> if {{C}} was compiled earlier, the map constructor is called. In addition, 
> {{new C(nope: 1)}} passes type checking, since {{C(Map)}} accepts any key, 
> but then fails at runtime with {{MissingPropertyException}}.
> h3. Cause
> {{ConstructorCallTransformer}} recognises a map-style call by its target 
> having {{GENERATED_EMPTY_STATEMENT}} as its code. That is 
> {{EmptyStatement.INSTANCE}}, which is also the code of the stubs that 
> {{MethodNodeUtils.withDefaultArgumentMethods}} creates for default arguments. 
> The transformer tries to skip declared constructors, but it compares by 
> identity and the stubs are created afresh for each lookup. GROOVY-12454 
> extended the rewrite from map literals to any Map expression, which exposed 
> this.
> h3. Expected behaviour
> A constructor that accepts the map, including one implied by default 
> arguments, is used. The no-arg constructor and setters are used only if there 
> is no such constructor. {{MetaClassImpl#invokeConstructor}} and the static 
> type checker already follow this rule.
> The GROOVY-12454 fix itself should be kept: a Map expression must still give 
> a map-style call when no constructor matches.
> h3. Scope
> * *7.0 and 6.0.2:* fix both parts, giving static compilation the same result 
> as dynamic Groovy. The second part changes behaviour for code which relied on 
> the setters being called, so it needs an entry in {{COMPATIBILITY.md}}. The 
> documentation on named parameters should also state the precedence.
> * *5.1.5 and 4.0.34:* fix the regression only. Named arguments and map 
> literals are unchanged.
> PR for the first part: https://github.com/apache/groovy/pull/3382



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to