Paul King created GROOVY-12762:
----------------------------------
Summary: @NamedVariant: default values aren't handled correctly by
the named-arg variant
Key: GROOVY-12762
URL: https://issues.apache.org/jira/browse/GROOVY-12762
Project: Groovy
Issue Type: Bug
Affects Versions: 6.0.0
Reporter: Paul King
The named-arg variant generated by {{@NamedVariant}} mishandles default
parameter values in several ways. All examples below use:
{code:groovy}
import groovy.transform.*
@ToString(includeNames=true)
class Color { int r, g, b }
{code}
h3. 1. A positional parameter's default is never used
When named arguments ({{@NamedParam}}/{{@NamedDelegate}}) are mixed with a
positional parameter that has a default value, the named-arg variant has no
default-argument variants, so the default can never be used by leaving the
argument out:
{code:groovy}
@NamedVariant
String m(@NamedDelegate Color color, Number alpha = 4.5) { "$color $alpha" }
assert m(r:1, g:2, b:3, 0.0) == 'Color(r:1, g:2, b:3) 0.0'
assert m(r:1, g:2, b:3) == 'Color(r:1, g:2, b:3) 4.5' //
MissingMethodException: no m(Map)
{code}
Only {{m(Map, Number)}} is generated. The named-arg variant reuses the original
method's {{Parameter}} objects, and the Verifier clears each default once it
has expanded the original method, so nothing is left for the variant. This is
the {{// TODO account for default params giving multiple signatures}} in
{{NamedVariantASTTransformation.createMapVariant}}. It was raised in review of
GROOVY-10498 (PR #3295), where the suggested assertion {{m(r:1, g:2, b:3)}}
couldn't be added for this reason.
h3. 2. A default that references a {{@NamedDelegate}} fails at runtime
{code:groovy}
@NamedVariant
String s(@NamedDelegate Color c, @NamedParam String z = c.toString()) { z }
assert s(r:1, g:2, b:3) == 'Color(r:1, g:2, b:3)' // MissingPropertyException:
No such property: c
{code}
This also happens under {{@CompileStatic}}. The default is copied into the
named-arg variant unchanged, where {{c}} doesn't exist.
h3. 3. A nested reference to an earlier parameter in a default isn't rewritten
{code:groovy}
@NamedVariant
String n(@NamedParam int x, @NamedParam int y = x * 2 + 1) { "$x $y" }
assert n(x: 5) == '5 11' // MissingPropertyException: No such property: x
{code}
{{getDefaultValue}} rewrites references to earlier parameters (GROOVY-10561,
GROOVY-10889, GROOVY-11325) only one level deep: {{x + 1}} works, {{x * 2 + 1}}
doesn't.
h3. 4. A closure in a default that references an earlier parameter crashes the
compiler
{code:groovy}
@NamedVariant
String s(@NamedParam int x, @NamedParam Closure z = { "v$x" }) { z() }
s(x: 1)
{code}
{noformat}
BUG! exception in phase 'class generation' ... tried to get a variable with the
name x as stack variable, but a variable with this name was not created
{noformat}
The same happens for {{@NamedParam String z = { "v$x" }()}} and for references
to a {{@NamedDelegate}}.
h3. 5. A {{@NamedVariant}} constructor with {{@NamedDelegate}} fails to compile
without {{@CompileStatic}}
{code:groovy}
class K {
String s
@NamedVariant
K(@NamedDelegate Color c, @NamedParam String z = 'q') { s = "$c $z" }
}
new K(r:1, g:2, b:3)
{code}
{noformat}
Cannot reference 'subMap' before supertype constructor has been called.
{noformat}
The {{subMap}} call that builds the delegate is created with
{{implicitThis=true}}, so the Verifier treats it as an instance call before
{{this(...)}}. The neighbouring {{containsKey}} helper already does
{{setImplicitThis(false)}} for this reason. It works under {{@CompileStatic}}.
h3. Proposed fix
# *Default-argument variants for the named-arg variant (part 1).* Give the
named-arg variant its own copies of positional parameters, keeping their
default values, so the Verifier generates {{m(Map)}} etc.
# *Clashes are a compile-time error.* If a default-argument variant of the
named-arg method/constructor duplicates a declared member, report an error, as
{{@TupleConstructor(namedVariant=true)}} does (GROOVY-11644), e.g.
{noformat}Class W already has method m(java.util.Map) which duplicates a
default-argument variant of the named-arg method{noformat}
Code that declares its own {{m(Map)}} alongside such a {{@NamedVariant}}
compiles today, so this is a deliberate behaviour change.
# *A {{@NamedDelegate}} referenced by a default is built once and shared (part
2).*
#* Methods: bind the delegate to a local in the named-arg variant and use it
for both the argument and the default(s) that reference it.
#* Constructors, where no statements are allowed before {{this(...)}}: the
named-arg constructor chains to a private helper constructor {{(Map,
<positional...>, <referenced delegates...>)}}. It is generated only when a
default references a delegate, and it is a compile-time error if that signature
is already declared.
In both cases the method receives the same instance the default saw.
# *A positional default that references a {{@NamedDelegate}} is a compile-time
error*, for methods and constructors:
{noformat}The default value of positional parameter 'd' can't reference
@NamedDelegate parameter 'c'.{noformat}
Such a default is evaluated in a Verifier-generated variant where only the map
is in scope, so the shared instance isn't available. For methods this could
later be relaxed by having the transform generate those variants itself; for
constructors that would need extra private constructors with synthetic marker
parameters to avoid signature clashes. {{@NamedParam}} defaults are unaffected.
# *Rewrite references to earlier parameters at any depth (part 3).*
# *A default that references an earlier parameter from within a closure is a
compile-time error (part 4).* Supporting it would mean copying the closure and
redoing variable scoping for the generated member.
# *Create the delegate's {{subMap}} call with {{setImplicitThis(false)}} (part
5).*
Parts 3 and 5 are small and independent of the rest, so they could be
backported on their own.
h3. Example after the fix
{code:groovy}
@NamedVariant
String s(@NamedDelegate Color c, @NamedParam int x, @NamedParam int y = x * 2 +
1, @NamedParam String z = c.toString()) {
"$c $x $y $z"
}
assert s(r:1, g:2, b:3, x:5) == 'Color(r:1, g:2, b:3) 5 11 Color(r:1, g:2, b:3)'
class K {
boolean same
@NamedVariant
K(@NamedDelegate Color c, @NamedParam Color d = c) { same = c.is(d) }
}
assert new K(r:1, g:2, b:3).same
{code}
h3. Notes
* With default-argument variants, an explicit {{null}} for a positional
parameter is passed through, as for ordinary Groovy default parameters; only
leaving the argument out selects the default. This matches GROOVY-10498.
* A prototype of all of the above passes the existing
{{org.codehaus.groovy.transform.*}} tests (1336).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)