Repository navigation
GROOVY-11492: add support for Type[] a = {} style array initialization - #3054
eric-milles wants to merge 3 commits into
Conversation
ba0fbfe to
79c4f01
Compare
|
AI read:
|
2b149a8 to
57980d8
Compare
daniellansun
left a comment
There was a problem hiding this comment.
Thanks for pushing this, Eric β the dim0()-gated reading is the right place to attack GROOVY-11492, and the non-closure cases (primitives, String[], multi-dim, multi-declarator, parameter defaults) look much better than master.
I do not think this tip is mergeable yet, mainly because of Closure/SAM/Object element types.
-
Silent behaviour change
Closure[] a = { println('x') }(and similar Runnable/Callable bodies that are not command chains) no longer stores a closure; the body runs at initialisation and the element is typically null. That is a silent semantic change, which is harder to deal with than a hard failure. The same class of issue affectsObject[] o = {},Closure[] a = {}, and default parameters. COMPATIBILITY currently documents the Java-faithful reading, but there is no diagnostic and no tests locking these cases down. -
Nested "produce closure" path drops expressions
WhenelementType.getComponentType()is null,visitArrayInitializeralways keepsemptyOrValue.get(0)even if there are several initialisers. The comment says "0..1"; the code should either rejectsize() > 1or build a proper multi-statement closure. As written, nested braces can quietly lose elements. -
Error quality regression
def m( {now points at the closing}instead of the{. Prefer restoring the old recovery/location rather than updating the negative tests to accept the worse message. -
Design choice
Paul's A/B/C write-up still stands. If we keep A, I would like a short dev-list confirmation, a stronger COMPATIBILITY note, and an explicit test matrix for{}/{ it }/{ -> }/{ { } }/Runnable[]/Object[]/@CompileStatic. Path B (array reading except empty/singleton when the leaf type is Closure, SAM, or possibly Object) would avoid breaking working Groovy while still unlocking the Java forms people actually paste. -
Docs
Happy to see the terse form documented, but the differences-from-Java / syntax pages still need a precise statement of when{}is an array vs a closure (and how to spell the old promotion).
Happy to re-review quickly once the Closure/SAM story and the nested truncation are settled.
|
re point 2: The goal was to move re point 3: I looked at the other similar failure messages and found the new error location more consistent than the previous behavior. re point 4: An email was sent to the dev list. |
|
@eric-milles Thanks for the follow-up. 2. Agreed β getting 3. If the new caret lines up with the other similar recovery messages, Iβm fine leaving the negative tests as updated. 4. Thanks for writing the list β Iβll follow that thread. 1 is still my main merge concern: 5. Happy with the terse form in the docs; a short explicit rule on the differences/syntax pages for when a non-empty Happy to re-review quickly once the list direction is clear, or if you push diagnostics / B. |
57980d8 to
0029968
Compare
|
I added a test for the various behavior changes discussed above. 0029968 The silent failures appear to be restricted to Closure/SAM expression evaluation with the intent of coercion. I don't have an immediate plan to single those out for alternate parsing or some compiler warning or something else. I did not include the |
|
I added a warning for possible instances of closure coercion: 25ecea6 |
daniellansun
left a comment
There was a problem hiding this comment.
Thanks for the follow-ups, Eric β the edge-case matrix in Groovy11492 and the new warning are real progress on the GROOVY-8561 concern from the earlier review. The non-closure paths (primitives, String[], multi-dim, multi-declarator) still look solid, and Iβm fine leaving points 2/3 as previously agreed.
I still need two fixes before I can clear the prior REQUEST_CHANGES:
-
Warning scope is too broad
checkArrayInitEdgeCasecurrently warns for every single-element, non-ClosureExpressioninitializer when the leaf type is non-primitive. That fires on the main Java idioms this ticket unlocks, e.g.String[] a = {'x'}andBoolean[] a = { false }(and the docs example). Those should stay quiet.Please gate the diagnostic on leaves that could previously have meant βclosure promotionβ β at least
Closureand SAM-capable types, andObjectif we want to cover that promotion flip too β rather than all reference component types. AstBuilder-time SAM detection is imperfect; a conservativeClosure/Object/ interface (orClassHelper.isSAMTypewhen resolved) gate is enough for this pass. Please also assert that the String/Boolean happy paths are warning-free. -
Formal-parameter defaults still skip the warning
visitVariableDeclaratorcallscheckArrayInitEdgeCase, butvisitFormalParameterdoes not. Sodef m(Closure[] cs = { println('x') })can still eager-eval and store[null]with no diagnostic β the silent case that blocked the earlier review. Please run the same check on parameter array initializers (and cover it in Groovy11492).
Soft / non-blocking for a re-review once 1β2 land:
- Docs still say βcommon scenariosβ / βexcept for auto-promotion casesβ without a precise array-vs-closure rule or how to spell the old promotion (
{ -> }, outer braces/brackets). A short note in differences-from-Java / syntax (and optionally COMPATIBILITY for non-empty Closure/SAM bodies) would help. - Tip
25ecea64isdirtyvs master and has no checks reported yet; prior tip was all green. Please rebase and let tip CI finish. - Remaining exotic nested-dimension NPE/truncation can stay on follow-up tickets as discussed.
Happy to re-review quickly after the warning predicate is tightened and wired through parameter defaults.
| if (ctx.arrayInitializer() != null) { | ||
| ClassNode elementType = parameterType.getComponentType(); | ||
| ctx.arrayInitializer().putNodeMetaData("elementType", elementType); | ||
| init = configureAST( |
There was a problem hiding this comment.
After building this ArrayExpression, please also call checkArrayInitEdgeCase(init) (same as visitVariableDeclarator). Parameter defaults such as Closure[] cs = { println('x') } otherwise keep the silent eager-eval behaviour with no diagnostic.
daniellansun
left a comment
There was a problem hiding this comment.
Thanks Eric β re-reviewed tip 25ecea64 against your inline reply on the warning predicate and the earlier edge-case / warning commits.
Progress since the first pass is real: the Groovy11492 matrix documents the Closure/SAM behaviour changes, and emitting some diagnostic for the eager-eval case is the right instinct. I still cannot clear the prior Request changes, for two concrete tip gaps:
-
Warning predicate is still too broad.
checkArrayInitEdgeCasewarns on every single-element, non-ClosureExpressioninitializer whose leaf type is non-primitive. That fires on the main Java idioms this ticket unlocks βString[] a = {'x'},Boolean[] a = { false }, and the docs examples β which should stay quiet.I hear that the type is unresolved at AstBuilder time and that a precise Closure/SAM gate is therefore unavailable. That does not force βwarn for all reference leavesβ. Please pick one of:
- Name-based heuristic on the unresolved
ClassNode: warn only when the leafβs name isClosure,Object, or a small allow-list of common SAM simple names (Runnable,Callable,Comparator, β¦), optionally plus βname looks like an interfaceβ if you can do that cheaply from the node; or - Defer the diagnostic to a later phase that sees resolved types (
ResolveVisitor/ a small custom visitor) and then useClassHelper.isSAMType/isDerivedFrom(CLOSURE_TYPE); or - Drop the warning for this PR and instead land path B (array reading except empty/singleton when the leaf is Closure/SAM/Object) once types are known β which avoids the silent eager-eval case without noisy warnings on
String[].
Please also assert in
Groovy11492thatString[]/Boolean[]happy paths are warning-free. - Name-based heuristic on the unresolved
-
Formal-parameter defaults still skip the warning.
visitVariableDeclaratorcallscheckArrayInitEdgeCase, butvisitFormalParameterstill builds theArrayExpressionwithout calling it. Sodef m(Closure[] cs = { println('x') })can still eager-eval and store[null]with no diagnostic β the silent case that blocked the earlier review. Please wire the same check there and cover it in the test matrix.
Soft / non-blocking once 1β2 land:
- Docs still need a precise array-vs-closure rule (and how to spell the old promotion:
{ -> }, outer braces/brackets), beyond βcommon scenariosβ. - Tip still needs a clean CI run on current master.
- Remaining exotic nested-dimension issues can stay on follow-up tickets as discussed.
Happy to re-review quickly after the predicate is narrowed (by name heuristic or deferred resolution) and parameter defaults emit the same diagnostic.
Request changes.
25ecea6 to
5f82f3e
Compare
11d24cf to
add5261
Compare
Codecov Reportβ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3054 +/- ##
==================================================
+ Coverage 74.4991% 74.5036% +0.0045%
- Complexity 44952 44961 +9
==================================================
Files 1703 1703
Lines 152332 152359 +27
Branches 29187 29194 +7
==================================================
+ Hits 113486 113513 +27
+ Misses 28416 28413 -3
- Partials 10430 10433 +3
π New features to boost your workflow:
|
|
JMH summary β classic (commit
|
| Group | Speedup | Calibrated | n |
|---|---|---|---|
| bench | 1.562 Γ | 0.941 Γ | 134 |
| core | 1.110 Γ | 1.130 Γ | 108 |
| grails | 1.260 Γ | 1.016 Γ | 80 |
β οΈ 1 benchmark at least 1.5Γ slower than the 90-day baseline:
org.apache.groovy.bench.AryBench.groovyCS ( {"n":"10"} )β 1.70Γ slower (calibrated)
β οΈ Runner speed differs β₯15% from the historical baseline hardware for: bench, grails-ad, grails-ez. Raw speedups are not meaningful for those parts β use the calibrated column.
Runner calibration (this run vs baseline hardware): bench 1.63Γ (27 rulers) Β· core-ag 1.04Γ (3 rulers) Β· core-hz 0.89Γ (3 rulers) Β· grails-ad 1.22Γ (3 rulers) Β· grails-ez 1.26Γ (3 rulers)
Baseline: dev/bench/jmh/<part>/classic/data.js on gh-pages, trailing 90 days. Daily dashboard Β· Per-suite raw data
JMH summary β indy (commit
|
| Group | Speedup | Calibrated | n |
|---|---|---|---|
| bench | 0.952 Γ | 0.974 Γ | 134 |
| core | 2.280 Γ | 2.031 Γ | 108 |
| grails | 1.492 Γ | 1.201 Γ | 80 |
β οΈ 3 benchmarks at least 1.5Γ slower than the 90-day baseline:
org.apache.groovy.bench.AryBench.groovyCS ( {"n":"1000000"} )β 1.98Γ slower (calibrated)org.apache.groovy.bench.ClosureDoCallBench.each_mono ( {"pack":"true"} )β 1.84Γ slower (calibrated)org.apache.groovy.bench.StaticMethodCallIndyBench.staticChain_groovyCSβ 1.54Γ slower (calibrated)
β οΈ Runner speed differs β₯15% from the historical baseline hardware for: core-ag, grails-ad, grails-ez. Raw speedups are not meaningful for those parts β use the calibrated column.
Runner calibration (this run vs baseline hardware): bench 0.97Γ (27 rulers) Β· core-ag 1.23Γ (3 rulers) Β· core-hz 0.96Γ (3 rulers) Β· grails-ad 1.31Γ (3 rulers) Β· grails-ez 1.19Γ (3 rulers)
Baseline: dev/bench/jmh/<part>/indy/data.js on gh-pages, trailing 90 days. Daily dashboard Β· Per-suite raw data
β All tests passed βπ·οΈ Commit: add5261 Learn more about TestLens at testlens.app/docs. |
daniellansun
left a comment
There was a problem hiding this comment.
Thanks Eric β I re-reviewed the current tip (add52613) after the rebase onto present master.
The rebase itself looks clean, and tip CI is green, which addresses the earlier request to land this on current master and let the checks finish. The non-closure paths (primitives, the String[] behaviour, multi-dimensional initializers, multi-declarator, { -> } / { { } } escape hatches) still look solid, and the Groovy11492 matrix remains useful documentation of the GROOVY-8561 edge. I also appreciate the work to get Object[] a = { {} } off the NPE path.
I am afraid I still cannot clear the prior Request changes. The two concrete gaps from the 30 October / 8 November reviews appear unchanged on this tip (the array-initializer methods match the previously reviewed warning commit; only the branch has been rebased).
-
The warning predicate is still too broad.
checkArrayInitEdgeCasestill warns on every single-element, non-ClosureExpressioninitializer whose leaf type is non-primitive. That includes the main Java idioms this ticket unlocks βString[] a = {'x'},Boolean[] a = { false },Integer[] a = {1}, a single-elementString[][]inner array, the field form,@CompileStatic/@TypeCheckedof the same, and the docs exampleString[] javaBooks = {'Java in Action'}. Those should stay quiet. (int[] a = {1}correctly stays silent, which is helpful.)I quite understand that the type is not resolved at
AstBuildertime, and that a preciseClosure/ SAM gate is therefore unavailable there. That does not force βwarn for all reference leavesβ. Would you be willing to pick one of:- a name-based heuristic on the unresolved
ClassNode: warn only when the leafβs name isClosure,Object, or a small allow-list of common SAM simple names (Runnable,Callable,Comparator, β¦); or - defer the diagnostic to a later phase that sees resolved types (
ResolveVisitoror a small custom visitor) and then useClassHelper.isSAMType/isDerivedFrom(CLOSURE_TYPE); or - path B (array reading, except empty/singleton when the leaf is Closure/SAM/Object), once types are known β which avoids the silent eager-eval case without noisy warnings on
String[].
It would also help to assert in
Groovy11492that theString[]/Boolean[]happy paths (and the docs example) are warning-free. - a name-based heuristic on the unresolved
-
Formal-parameter defaults still skip the warning.
visitVariableDeclaratorcallscheckArrayInitEdgeCase, butvisitFormalParameterstill builds theArrayExpressionwithout calling it. Sodef m(Closure[] cs = { println('x') }) { cs } m()
still eagerly evaluates the body when the default is applied, stores
[null], and emits no diagnostic β the silent case that blocked the earlier review. The same pattern is quiet forRunnable/Callable/Comparatorparameter defaults, and constructor, closure, and lambda parameter defaults share this path. Locals of those types at least warn; parameters do not. Could you wire the same check through and cover it in the test matrix?(Under
@CompileStaticthis particular example is a hard type-checking error, which is safer; the dynamic parameter path is the one that remains silent.)
Soft / non-blocking once 1β2 land:
- The docs still describe the new syntax as applying in βcommon scenariosβ, with ambiguity only βexcept for auto-promotion casesβ, without a precise array-versus-closure rule or how to spell the old promotion (
{ -> },{ { } }, or outer brackets). A short note on the differences-from-Java / syntax pages would help.COMPATIBILITYdocuments the empty-{}flip well; it does not yet mention the non-empty eager-evaluation case. - Remaining exotic nested-dimension issues can stay on follow-up tickets, as we discussed earlier.
Happy to re-review quickly after the predicate is narrowed (by name heuristic or deferred resolution, or by path B) and parameter defaults emit the same diagnostic.
Request changes.
| } | ||
|
|
||
| if (!elementType.isArray()) { | ||
| if (array.getExpressions().size() == 1 && !(array.getExpression(0) instanceof ClosureExpression)) { |
There was a problem hiding this comment.
Please narrow this predicate so we only warn when the leaf element type could previously have been closure / SAM / (optional) Object promotion β not for String[] / Boolean[] / Integer[] / other ordinary reference arrays. As written, the primary Java single-element idiom, including the docs example String[] javaBooks = {'Java in Action'}, gets a warning on every use. I understand the type is unresolved here; a name-based heuristic, or deferring the diagnostic until types are resolved, would be enough for this pass.
| if (ctx.arrayInitializer() != null) { | ||
| ClassNode elementType = parameterType.getComponentType(); | ||
| ctx.arrayInitializer().putNodeMetaData("elementType", elementType); | ||
| init = configureAST( |
There was a problem hiding this comment.
After building this ArrayExpression, please also call checkArrayInitEdgeCase(init) (same as visitVariableDeclarator). Parameter defaults such as Closure[] cs = { println('x') } otherwise keep the silent eager-eval behaviour with no diagnostic. Constructor, closure, and lambda parameter defaults share this path.



@daniellansun @paulk-asert Could you have a look at this and let me know if I'm heading in the right direction? I did mean to get this into 6.0 but missed the train.
GROOVY-8561 discusses
Closureor SAM-type array promotion behavior that is impacted by this change.The adoc updates are very hastily done to get this to the evaluation stage.