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 Feb 9, 2025
1 parent d5b22aa commit 9f73dbc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,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
25 changes: 19 additions & 6 deletions src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,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 @@ -429,8 +429,21 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
}
}

@NotYetImplemented
// GROOVY-11559
void testMultipleInstanceOf5() {
assertScript '''
def foo(o) {
if (o instanceof Set || o instanceof List) {
o = "" // NullPointerException
}
}
foo("")
foo([])
'''
}

@NotYetImplemented
void testMultipleInstanceOf6() {
assertScript '''
void test(thing) {
if (thing instanceof Deque) {
Expand All @@ -448,8 +461,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 @@ -469,7 +482,7 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
}

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

0 comments on commit 9f73dbc

Please sign in to comment.