You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "paulk-asert (via GitHub)" <gi...@apache.org> on 2026/02/06 11:20:01 UTC

[PR] encapsulate ForStatement without value variable [groovy]

paulk-asert opened a new pull request, #2378:
URL: https://github.com/apache/groovy/pull/2378

   (no comment)


-- 
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: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] encapsulate ForStatement without value variable [groovy]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2378:
URL: https://github.com/apache/groovy/pull/2378#discussion_r2776569136


##########
src/testFixtures/groovy/org/codehaus/groovy/ast/builder/AstAssert.groovy:
##########
@@ -237,7 +237,10 @@ class AstAssert {
                 assertSyntaxTree([expected.arguments], [actual.arguments])
             },
             ForStatement : { expected, actual ->
-                assertSyntaxTree([expected.variable], [actual.variable])
+                if (expected.hasValueVariable()) {
+                    assertSyntaxTree([expected.valueVariable], [actual.valueVariable])
+                }
+                assertSyntaxTree([expected.indexVariable], [actual.indexVariable])

Review Comment:
   The ForStatement assertion only compares valueVariable when `expected.hasValueVariable()` is true, but it doesn't assert that `actual` has the same presence/absence. This can let a regression slip through where the actual AST unexpectedly has a value variable (or vice versa) and the test still passes. Consider first asserting `expected.hasValueVariable() == actual.hasValueVariable()`, and then comparing `valueVariable` when present (or asserting it is null when absent).



##########
src/main/java/org/codehaus/groovy/ast/stmt/ForStatement.java:
##########
@@ -74,7 +83,14 @@ public Parameter getIndexVariable() {
      * @since 5.0.0
      */
     public Parameter getValueVariable() {
-        return valueVariable != FOR_LOOP_DUMMY ? valueVariable : null;
+        return valueVariable != DUMMY_VALUE_VARIABLE ? valueVariable : null;
+    }
+
+    /**
+     * @since 6.0.0
+     */
+    public boolean hasValueVariable() {
+        return valueVariable != null && valueVariable != DUMMY_VALUE_VARIABLE;

Review Comment:
   `hasValueVariable()` checks `valueVariable != null`, but `valueVariable` is always non-null due to `requireNonNull(valueVariable)` in the constructor. Dropping the null check would simplify the method and better reflect the invariant (only the dummy sentinel indicates absence).
   ```suggestion
           return valueVariable != DUMMY_VALUE_VARIABLE;
   ```



-- 
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: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] encapsulate ForStatement without value variable [groovy]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #2378:
URL: https://github.com/apache/groovy/pull/2378#issuecomment-3859980493

   ## [Codecov](https://app.codecov.io/gh/apache/groovy/pull/2378?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `95.45455%` with `1 line` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 68.3327%. Comparing base ([`92643ee`](https://app.codecov.io/gh/apache/groovy/commit/92643ee9bbd4482b7f5c27f6eae82e25752fdcad?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`faa932f`](https://app.codecov.io/gh/apache/groovy/commit/faa932f93b1c02a34f52b71aff9385280efca9b5?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 6 commits behind head on master.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/groovy/pull/2378?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...ava/org/codehaus/groovy/ast/stmt/ForStatement.java](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fast%2Fstmt%2FForStatement.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2FzdC9zdG10L0ZvclN0YXRlbWVudC5qYXZh) | 83.3333% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   [![Impacted file tree graph](https://app.codecov.io/gh/apache/groovy/pull/2378/graphs/tree.svg?width=650&height=150&src=pr&token=1r45138NfQ&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   ```diff
   @@                Coverage Diff                 @@
   ##               master      #2378        +/-   ##
   ==================================================
   - Coverage     68.3800%   68.3327%   -0.0473%     
   + Complexity      30341      30324        -17     
   ==================================================
     Files            1382       1382                
     Lines          116151     116085        -66     
     Branches        20462      20466         +4     
   ==================================================
   - Hits            79424      79324       -100     
   - Misses          30230      30260        +30     
   - Partials         6497       6501         +4     
   ```
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/groovy/pull/2378?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [...va/org/apache/groovy/parser/antlr4/AstBuilder.java](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fapache%2Fgroovy%2Fparser%2Fantlr4%2FAstBuilder.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2dyb292eS9wYXJzZXIvYW50bHI0L0FzdEJ1aWxkZXIuamF2YQ==) | `86.5524% <100.0000%> (ø)` | |
   | [...aus/groovy/ast/ClassCodeExpressionTransformer.java](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fast%2FClassCodeExpressionTransformer.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2FzdC9DbGFzc0NvZGVFeHByZXNzaW9uVHJhbnNmb3JtZXIuamF2YQ==) | `100.0000% <100.0000%> (ø)` | |
   | [...g/codehaus/groovy/ast/ClassCodeVisitorSupport.java](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fast%2FClassCodeVisitorSupport.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2FzdC9DbGFzc0NvZGVWaXNpdG9yU3VwcG9ydC5qYXZh) | `99.2126% <100.0000%> (+0.0062%)` | :arrow_up: |
   | [...ehaus/groovy/classgen/ClassCompletionVerifier.java](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fclassgen%2FClassCompletionVerifier.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2NsYXNzZ2VuL0NsYXNzQ29tcGxldGlvblZlcmlmaWVyLmphdmE=) | `84.8429% <100.0000%> (+0.0845%)` | :arrow_up: |
   | [...vy/classgen/asm/sc/StaticTypesStatementWriter.java](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fclassgen%2Fasm%2Fsc%2FStaticTypesStatementWriter.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2NsYXNzZ2VuL2FzbS9zYy9TdGF0aWNUeXBlc1N0YXRlbWVudFdyaXRlci5qYXZh) | `93.7984% <100.0000%> (ø)` | |
   | [...va/org/codehaus/groovy/control/ResolveVisitor.java](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fcontrol%2FResolveVisitor.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2NvbnRyb2wvUmVzb2x2ZVZpc2l0b3IuamF2YQ==) | `90.0510% <100.0000%> (+0.0127%)` | :arrow_up: |
   | [...roovy/transform/stc/StaticTypeCheckingVisitor.java](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Ftransform%2Fstc%2FStaticTypeCheckingVisitor.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L3RyYW5zZm9ybS9zdGMvU3RhdGljVHlwZUNoZWNraW5nVmlzaXRvci5qYXZh) | `87.8502% <100.0000%> (ø)` | |
   | [...ava/org/codehaus/groovy/ast/stmt/ForStatement.java](https://app.codecov.io/gh/apache/groovy/pull/2378?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Forg%2Fcodehaus%2Fgroovy%2Fast%2Fstmt%2FForStatement.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL21haW4vamF2YS9vcmcvY29kZWhhdXMvZ3Jvb3Z5L2FzdC9zdG10L0ZvclN0YXRlbWVudC5qYXZh) | `92.8571% <83.3333%> (-7.1429%)` | :arrow_down: |
   
   ... and [22 files with indirect coverage changes](https://app.codecov.io/gh/apache/groovy/pull/2378/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   </details>
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
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: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] encapsulate ForStatement without value variable [groovy]

Posted by "paulk-asert (via GitHub)" <gi...@apache.org>.
paulk-asert closed pull request #2378: encapsulate ForStatement without value variable
URL: https://github.com/apache/groovy/pull/2378


-- 
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: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] encapsulate ForStatement without value variable [groovy]

Posted by "paulk-asert (via GitHub)" <gi...@apache.org>.
paulk-asert commented on PR #2378:
URL: https://github.com/apache/groovy/pull/2378#issuecomment-3881984591

   Tweaked then merged.


-- 
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: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] encapsulate ForStatement without value variable [groovy]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2378:
URL: https://github.com/apache/groovy/pull/2378#discussion_r2779162176


##########
src/testFixtures/groovy/org/codehaus/groovy/ast/builder/AstAssert.groovy:
##########
@@ -237,7 +237,11 @@ class AstAssert {
                 assertSyntaxTree([expected.arguments], [actual.arguments])
             },
             ForStatement : { expected, actual ->
-                assertSyntaxTree([expected.variable], [actual.variable])
+                assert expected.valueVariable.asBoolean() == actual.valueVariable.asBoolean()
+                if (expected.valueVariable) {
+                    assertSyntaxTree([expected.valueVariable], [actual.valueVariable])
+                }
+                assertSyntaxTree([expected.indexVariable], [actual.indexVariable])

Review Comment:
   `ForStatement` assertion calls `expected.valueVariable.asBoolean()` / `actual.valueVariable.asBoolean()` and unconditionally passes `expected.indexVariable` into `assertSyntaxTree`. If `valueVariable` or `indexVariable` is null (common for C-style loops and for-in loops without an index variable), this can throw at runtime (null receiver for `asBoolean()` and/or `item.getClass()` inside `assertSyntaxTree` when the list contains null). Consider comparing presence with a null-safe check (e.g., `expected.valueVariable != null`) and only calling `assertSyntaxTree` for `indexVariable`/`valueVariable` when they are non-null; otherwise assert the corresponding actual variable is also null.
   ```suggestion
                   boolean expectedHasValueVariable = (expected.valueVariable != null)
                   boolean actualHasValueVariable = (actual.valueVariable != null)
                   Assert.assertEquals("Wrong presence of value variable in ForStatement", expectedHasValueVariable, actualHasValueVariable)
                   if (expectedHasValueVariable) {
                       assertSyntaxTree([expected.valueVariable], [actual.valueVariable])
                   }
                   boolean expectedHasIndexVariable = (expected.indexVariable != null)
                   boolean actualHasIndexVariable = (actual.indexVariable != null)
                   Assert.assertEquals("Wrong presence of index variable in ForStatement", expectedHasIndexVariable, actualHasIndexVariable)
                   if (expectedHasIndexVariable) {
                       assertSyntaxTree([expected.indexVariable], [actual.indexVariable])
                   }
   ```



-- 
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: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] encapsulate ForStatement without value variable [groovy]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2378:
URL: https://github.com/apache/groovy/pull/2378#discussion_r2779126902


##########
src/main/java/org/codehaus/groovy/ast/stmt/ForStatement.java:
##########
@@ -52,6 +54,13 @@ public ForStatement(final Parameter variable, final Expression collectionExpress
         this(null, variable, collectionExpression, loopBlock);
     }
 
+    /**
+     * @since 6.0.0
+     */
+    public ForStatement(final Expression collectionExpression, final Statement loopBlock) {
+        this(DUMMY_VALUE_VARIABLE, collectionExpression, loopBlock);
+    }

Review Comment:
   The new `ForStatement(Expression collectionExpression, Statement loopBlock)` constructor accepts any `Expression`, but creates a statement with no value variable (dummy), which makes the AST invalid for non-`ClosureListExpression` collections and can lead to downstream NPEs (e.g., visitors assume `getValueVariable()` is non-null for for-in loops). Consider tightening the signature to `ClosureListExpression` (or validating `collectionExpression instanceof ClosureListExpression` and throwing an `IllegalArgumentException`) and documenting that this constructor is only for C-style `for(init;cond;update)` loops.



##########
src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java:
##########
@@ -2176,7 +2176,7 @@ public void visitForLoop(final ForStatement forLoop) {
             visitStatement(forLoop);
             collectionExpression.visit(this);
             ClassNode collectionType = getType(collectionExpression);
-            ClassNode forLoopVariableType = forLoop.getVariableType();
+            ClassNode forLoopVariableType = forLoop.getValueVariable().getType();
             ClassNode componentType;

Review Comment:
   `forLoop.getValueVariable()` can be `null` (e.g., C-style loops use the dummy value variable which `getValueVariable()` intentionally hides). While this branch currently excludes `ClosureListExpression`, a programmatically-constructed `ForStatement` without a value variable but with a non-`ClosureListExpression` collection would NPE here. Consider guarding against `null` and either falling back to `getVariable()`/`getVariableType()` or producing a clearer static type checking error for malformed loops.



-- 
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: notifications-unsubscribe@groovy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org