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 2026/03/19 14:31:37 UTC

(groovy) branch GROOVY-11769 updated (904e55989a -> aec028c531)

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

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


    omit 904e55989a GROOVY-11375, GROOVY-11769: SC: lookup type of variable before target
     new aec028c531 GROOVY-11375, GROOVY-11769: SC: lookup type of variable before target

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (904e55989a)
            \
             N -- N -- N   refs/heads/GROOVY-11769 (aec028c531)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

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.


Summary of changes:
 .../transform/stc/TypeInferenceSTCTest.groovy      | 31 +++++++++++++---------
 1 file changed, 18 insertions(+), 13 deletions(-)


(groovy) 01/01: GROOVY-11375, GROOVY-11769: SC: lookup type of variable before target

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

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

commit aec028c5319d29bb7d4de36ab9bafd20c35cd41b
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Oct 3 18:02:20 2025 -0500

    GROOVY-11375, GROOVY-11769: SC: lookup type of variable before target
    
    see also GROOVY-7971, GROOVY-9344, GROOVY-9607
    
    4_0_X backport
---
 .../classgen/asm/sc/StaticTypesTypeChooser.java    | 15 ++++++-----
 .../groovy/transform/stc/STCAssignmentTest.groovy  | 15 +++++++++++
 .../transform/stc/STCnAryExpressionTest.groovy     | 20 ++++++++++++++
 .../transform/stc/TypeInferenceSTCTest.groovy      | 31 +++++++++++++---------
 .../asm/sc/AssignmentsStaticCompileTest.groovy     |  8 ++++++
 5 files changed, 70 insertions(+), 19 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesTypeChooser.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesTypeChooser.java
index 5f7750575f..5c00ceed4f 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesTypeChooser.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesTypeChooser.java
@@ -32,20 +32,23 @@ import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveVoid;
  * type information from node metadata generated by the static type checker.
  */
 public class StaticTypesTypeChooser extends StatementMetaTypeChooser {
+
     @Override
     public ClassNode resolveType(final Expression exp, final ClassNode current) {
-        ASTNode target = getTarget(exp); // see GROOVY-9344, GROOVY-9607
-
-        ClassNode inferredType = target.getNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
+        ClassNode inferredType = exp.getNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
+        if (inferredType == null) {
+            inferredType = exp.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
+        }
         if (inferredType == null) {
-            inferredType = target.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
+            ASTNode node = getTarget(exp); // GROOVY-9344, GROOVY-9607
+            inferredType = node.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
         }
         if (inferredType != null && !isPrimitiveVoid(inferredType)) {
             return inferredType;
         }
 
-        // AsmClassGenerator may create "this" expressions that the type checker knows nothing about
-        if (target instanceof VariableExpression && ((VariableExpression) target).isThisExpression()) {
+        // AsmClassGenerator creates "this" expressions that the type checker knows nothing about
+        if (exp instanceof VariableExpression && ((VariableExpression) exp).isThisExpression()) {
             return current;
         }
 
diff --git a/src/test/groovy/groovy/transform/stc/STCAssignmentTest.groovy b/src/test/groovy/groovy/transform/stc/STCAssignmentTest.groovy
index 91b14bd590..129501a9c3 100644
--- a/src/test/groovy/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/groovy/transform/stc/STCAssignmentTest.groovy
@@ -645,6 +645,21 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    void testCastObjectToInterface() {
+        assertScript '''
+            List<Object> m(object) {
+                if (object == null) return new ArrayList<>()
+                if (object instanceof Collection) return new ArrayList<Object>((Collection) object)
+                return [object]
+            }
+            def item = 0
+            def list = [1,2]
+            assert m(null).size() == 0
+            assert m(item).size() == 1
+            assert m(list).size() == 2
+        '''
+    }
+
     void testIfElseBranch() {
         shouldFailWithMessages '''
             def x
diff --git a/src/test/groovy/groovy/transform/stc/STCnAryExpressionTest.groovy b/src/test/groovy/groovy/transform/stc/STCnAryExpressionTest.groovy
index bff8c0a82a..7bb95c1ad6 100644
--- a/src/test/groovy/groovy/transform/stc/STCnAryExpressionTest.groovy
+++ b/src/test/groovy/groovy/transform/stc/STCnAryExpressionTest.groovy
@@ -132,6 +132,26 @@ class STCnAryExpressionTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-11375
+    void testShiftOnPrimitivesVariableFlowType() {
+        assertScript '''
+            def x = "--"
+            x = x.size()
+            def y = x << x
+            assert y === 8
+        '''
+    }
+
+    // GROOVY-11375
+    void testPowerOnPrimitivesVariableFlowType() {
+        assertScript '''
+            def x = "--"
+            x = x.size()
+            def y = x ** x
+            assert y === 4
+        '''
+    }
+
     // GROOVY-5644
     void testSpaceshipOperatorNoAmbiguousError() {
         assertScript '''
diff --git a/src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy
index 4cf9527ff1..aa14e79a6c 100644
--- a/src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -18,7 +18,6 @@
  */
 package groovy.transform.stc
 
-import groovy.test.NotYetImplemented
 import org.codehaus.groovy.ast.ClassHelper
 import org.codehaus.groovy.ast.ClassNode
 import org.codehaus.groovy.ast.MethodNode
@@ -242,23 +241,29 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    // GROOVY-10096
-    @NotYetImplemented
+    // GROOVY-11769
     void testInstanceOf10() {
-        shouldFailWithMessages '''
-            class Foo {
-                void foo() {
-                }
+        assertScript '''
+            abstract class Foo {
+                abstract boolean isBaz()
             }
             class Bar extends Foo {
-                void bar() {
-                }
+                final boolean baz = false
             }
-            static Bar baz(Foo foo) {
-                (false || foo instanceof Bar) ? foo : new Bar()
+            class Baz extends Foo {
+                final boolean baz = true
             }
-        ''',
-        'Cannot return value of type Foo for method returning Bar'
+
+            void test(Foo foo) {
+                if (foo instanceof Bar || foo.isBaz()) {
+                    foo.toString()
+                }
+            }
+
+            test(new Bar())
+            test(new Baz())
+            test(new Foo(){ boolean isBaz() { false } })
+        '''
     }
 
     // GROOVY-11007
diff --git a/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/AssignmentsStaticCompileTest.groovy b/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/AssignmentsStaticCompileTest.groovy
index 538589f10d..7eead72ade 100644
--- a/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/AssignmentsStaticCompileTest.groovy
+++ b/src/test/groovy/org/codehaus/groovy/classgen/asm/sc/AssignmentsStaticCompileTest.groovy
@@ -31,4 +31,12 @@ final class AssignmentsStaticCompileTest extends STCAssignmentTest implements St
         String bytecode = astTrees['C'][1]
         assert !bytecode.contains('ScriptBytecodeAdapter.setGroovyObjectProperty') : '"c.i += 10" should use setter, not dynamic property'
     }
+
+    @Override // GROOVY-11769
+    void testCastObjectToInterface() {
+        super.testCastObjectToInterface()
+        String bytecode = astTrees.values()[0][1]
+        bytecode = bytecode.substring(bytecode.indexOf('m(Ljava/lang/Object;)'))
+        assert bytecode.count('CHECKCAST') == 1 // guarded typecast isn't groovy
+    }
 }