[ 
https://issues.apache.org/jira/browse/GROOVY-11492?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18145279#comment-18145279
 ] 

ASF GitHub Bot commented on GROOVY-11492:
-----------------------------------------

daniellansun commented on code in PR #3054:
URL: https://github.com/apache/groovy/pull/3054#discussion_r4579463105


##########
src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java:
##########
@@ -3284,10 +3294,30 @@ public DeclarationExpression 
visitVariableDeclarator(final VariableDeclaratorCon
                                 ),
                                 ctx.variableDeclaratorId()),
                         token,
-                        
this.visitVariableInitializer(ctx.variableInitializer())),
+                        init),
                 ctx);
     }
 
+    private void checkArrayInitEdgeCase(final Expression expr) {
+        if (expr instanceof ArrayExpression array && array.hasInitializer()) {
+            ClassNode elementType = array.getElementType();
+
+            ClassNode baseType = elementType;
+            while (baseType.isArray()) baseType = baseType.getComponentType();
+            if (isPrimitiveType(baseType)) {
+                return; // closure coerced to boolean or integer is not likely
+            }
+
+            if (!elementType.isArray()) {
+                if (array.getExpressions().size() == 1 && 
!(array.getExpression(0) instanceof ClosureExpression)) {

Review 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.



##########
src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java:
##########
@@ -5981,7 +6019,32 @@ private void validateParameterList(final List<Parameter> 
parameterList) {
      */
     @Override
     public Parameter visitFormalParameter(final FormalParameterContext ctx) {
-        return this.processFormalParameter(ctx, ctx.variableModifiersOpt(), 
ctx.type(), ctx.ELLIPSIS(), ctx.variableDeclaratorId(), ctx.expression());
+        ClassNode parameterType = this.visitType(ctx.type());
+        var init = (Expression) this.visit(ctx.expression());
+
+        if (ctx.arrayInitializer() != null) {
+            ClassNode elementType = parameterType.getComponentType();
+            ctx.arrayInitializer().putNodeMetaData("elementType", elementType);
+            init = configureAST(

Review 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.





> Java compatibility: array initializers without "new Type[]"
> -----------------------------------------------------------
>
>                 Key: GROOVY-11492
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11492
>             Project: Groovy
>          Issue Type: Improvement
>          Components: parser-antlr4
>            Reporter: Eric Milles
>            Assignee: Eric Milles
>            Priority: Major
>              Labels: arrays
>
> There are a few cases where bare array initializers are not yet supported:
> # {{int[][] array = \{ \{1,2\}, \{3,4\} \};}}
> # {{@Target(\{ ElementType.FIELD, ElementType.METHOD \})}}
> # {code:groovy}@interface A {
>   String[] strings() default {}
> }
> {code}
> These were implemented as part of [1] but the PR was closed.  It may be worth 
> looking back over what was done and re-implementing one or more of these 
> cases.  It was noted that {{Closure[] array = \{\};}} is one of the edge 
> cases that leads to ambiguity.
> [1] https://github.com/apache/groovy/pull/691



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

Reply via email to