Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GROOVY-11559: fix addAllInterfaces for UnionTypeClassNode #2147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading