Skip to content

Commit

Permalink
GROOVY-11559: fix addAllInterfaces for UnionTypeClassNode
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 26, 2025
1 parent 9acdef1 commit a247964
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public static ClassNode parameterizeType(ClassNode hint, final ClassNode target)
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)) {
Expand Down
23 changes: 18 additions & 5 deletions src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -466,7 +479,7 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
}

// GROOVY-8828
void testMultipleInstanceOf7() {
void testMultipleInstanceOf8() {
assertScript '''
interface Foo { }
interface Bar { String name() }
Expand Down

0 comments on commit a247964

Please sign in to comment.