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

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

commit a2479641d9b9adcd9a3f5065710ac34871e0a7aa
Author: Eric Milles <[email protected]>
AuthorDate: Sun Jan 26 16:02:10 2025 -0600

    GROOVY-11559: fix `addAllInterfaces` for `UnionTypeClassNode`
---
 .../codehaus/groovy/ast/tools/GenericsUtils.java   |  2 +-
 .../transform/stc/TypeInferenceSTCTest.groovy      | 23 +++++++++++++++++-----
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java 
b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
index 1c2706c71c..aa5ced7702 100644
--- a/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
+++ b/src/main/java/org/codehaus/groovy/ast/tools/GenericsUtils.java
@@ -290,7 +290,7 @@ public class GenericsUtils {
         Map<String, ClassNode> gt;
 
         // relationship may be reversed for cases like "Iterable<String> x = 
[]"
-        if (!cn.equals(hint) && implementsInterfaceOrIsSubclassOf(target, 
hint)) {
+        if (!cn.equals(hint) && (hint.isInterface() ? 
cn.implementsInterface(hint) : cn.isDerivedFrom(hint))) { // GROOVY-11559
             do { // walk target type hierarchy towards hint
                 cn = ClassHelper.getNextSuperClass(cn, hint);
                 if (hasUnresolvedGenerics(cn)) {
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy 
b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index e66b1d811c..508be6d96f 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -412,11 +412,11 @@ class TypeInferenceSTCTest extends 
StaticTypeCheckingTestCase {
 
     // GROOVY-8965
     void testMultipleInstanceOf4() {
-        ['o', '((Number) o)'].each {
+        for (o in ['o', '((Number) o)']) {
             assertScript """
                 def foo(o) {
                     if (o instanceof Integer || o instanceof Double) {
-                        ${it}.floatValue() // ClassCastException
+                        ${o}.floatValue() // ClassCastException
                     }
                 }
                 def bar = foo(1.1d)
@@ -427,7 +427,20 @@ class TypeInferenceSTCTest extends 
StaticTypeCheckingTestCase {
         }
     }
 
+    // GROOVY-11559
     void testMultipleInstanceOf5() {
+        assertScript '''
+            def foo(o) {
+                if (o instanceof Set || o instanceof List) {
+                    o = "" // NullPointerException
+                }
+            }
+            foo("")
+            foo([])
+        '''
+    }
+
+    void testMultipleInstanceOf6() {
         assertScript '''
             void test(thing) {
                 if (thing instanceof Deque) {
@@ -445,8 +458,8 @@ class TypeInferenceSTCTest extends 
StaticTypeCheckingTestCase {
     }
 
     // GROOVY-10668
-    void testMultipleInstanceOf6() {
-        ['(value as String)', 'value.toString()'].each { string ->
+    void testMultipleInstanceOf7() {
+        for (string in ['(value as String)', 'value.toString()']) {
             assertScript """
                 def toArray(Object value) {
                     def array
@@ -466,7 +479,7 @@ class TypeInferenceSTCTest extends 
StaticTypeCheckingTestCase {
     }
 
     // GROOVY-8828
-    void testMultipleInstanceOf7() {
+    void testMultipleInstanceOf8() {
         assertScript '''
             interface Foo { }
             interface Bar { String name() }

Reply via email to