You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2025/04/04 21:33:26 UTC

(groovy) branch GROOVY-11597 created (now 65f6559ee4)

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a change to branch GROOVY-11597
in repository https://gitbox.apache.org/repos/asf/groovy.git


      at 65f6559ee4 GROOVY-11597: STC: loop over enum class

This branch includes the following new commits:

     new 65f6559ee4 GROOVY-11597: STC: loop over enum class

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(groovy) 01/01: GROOVY-11597: STC: loop over enum class

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-11597
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 65f6559ee41b718734e8dbdaf6fb0a714a1f491f
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Apr 4 16:28:48 2025 -0500

    GROOVY-11597: STC: loop over enum class
---
 .../codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java | 8 ++++++--
 src/test/groovy/ForLoopTest.groovy                               | 3 ---
 src/test/groovy/transform/stc/LoopsSTCTest.groovy                | 9 +++++++++
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 1fa3ee51a6..94dd15e53d 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -158,6 +158,7 @@ import static org.codehaus.groovy.ast.ClassHelper.CLASS_Type;
 import static org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.Character_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.Double_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.Enum_Type;
 import static org.codehaus.groovy.ast.ClassHelper.Float_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.Integer_TYPE;
 import static org.codehaus.groovy.ast.ClassHelper.Iterator_TYPE;
@@ -2120,6 +2121,9 @@ out:    if ((samParameterTypes.length == 1 && isOrImplements(samParameterTypes[0
                 } else {
                     componentType = STRING_TYPE;
                 }
+            } else if (isClassType(collectionType) && collectionType.getGenericsTypes() != null
+                  && collectionType.getGenericsTypes()[0].getType().isDerivedFrom(Enum_Type)) {
+                componentType = collectionType.getGenericsTypes()[0].getType(); // GROOVY-11597
             } else {
                 componentType = inferComponentType(collectionType, null);
                 if (componentType == null) {
@@ -2134,11 +2138,11 @@ out:    if ((samParameterTypes.length == 1 && isOrImplements(samParameterTypes[0
             if (!isDynamicTyped(forLoopVariableType)) { // user-supplied type
                 componentType = forLoopVariableType;
             }
-            typeCheckingContext.controlStructureVariables.put(forLoop.getVariable(), componentType);
+            typeCheckingContext.controlStructureVariables.put(forLoop.getValueVariable(), componentType);
             try {
                 forLoop.getLoopBlock().visit(this);
             } finally {
-                typeCheckingContext.controlStructureVariables.remove(forLoop.getVariable());
+                typeCheckingContext.controlStructureVariables.remove(forLoop.getValueVariable());
             }
         }
         if (isSecondPassNeededForControlStructure(varTypes, oldTracker)) {
diff --git a/src/test/groovy/ForLoopTest.groovy b/src/test/groovy/ForLoopTest.groovy
index 90b3009b03..c44a28955d 100644
--- a/src/test/groovy/ForLoopTest.groovy
+++ b/src/test/groovy/ForLoopTest.groovy
@@ -142,9 +142,6 @@ final class ForLoopTest {
             assert i == v
             continue
         }
-        for (int i, def ts in Thread.State) {
-            assert i == ((Thread.State) ts).ordinal() // GROOVY-11597
-        }
     }
 
     @Test
diff --git a/src/test/groovy/transform/stc/LoopsSTCTest.groovy b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
index b104af4a14..fc36b5402d 100644
--- a/src/test/groovy/transform/stc/LoopsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
@@ -310,6 +310,15 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-11597
+    void testForInLoopOnAnEnumClass() {
+        assertScript '''
+            for (i, s in Thread.State) {
+                assert i === s.ordinal()
+            }
+        '''
+    }
+
     // GROOVY-11305
     void testForInLoopOnNearlyIterable() {
         assertScript '''