This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 5fce9c218b GROOVY-11597: STC: loop over enum class
5fce9c218b is described below
commit 5fce9c218bdaebc41455b6af770ea83d2e601003
Author: Eric Milles <[email protected]>
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 '''