Skip to content

Commit cc8bc3e

Browse files
committed
GROOVY-11815: STC: retain generics of LUB under an instanceof guard
1 parent 3286c04 commit cc8bc3e

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6215,7 +6215,7 @@ private ClassNode getInferredTypeFromTempInfo(final Expression expression, final
62156215
ClassNode superclass;
62166216
ClassNode[] interfaces;
62176217
if (expressionType instanceof WideningCategories.LowestUpperBoundClassNode) {
6218-
superclass = expressionType.getSuperClass();
6218+
superclass = expressionType.getUnresolvedSuperClass(); // GROOVY-11815
62196219
interfaces = expressionType.getInterfaces();
62206220
} else if (expressionType != null && expressionType.isInterface()) {
62216221
superclass = OBJECT_TYPE;

src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
127127
shouldFailWithMessages '''
128128
Object o
129129
if (o instanceof String) {
130-
o.toUpperCase()
130+
o.toUpperCase()
131131
} else {
132132
o.toUpperCase() // ensure that type information is reset
133133
}
@@ -297,6 +297,25 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
297297
'''
298298
}
299299

300+
// GROOVY-11815
301+
void testInstanceOf13() {
302+
assertScript '''
303+
def c = true ? new ArrayDeque() : new Stack()
304+
if (c instanceof Deque) {
305+
c.addFirst(1)
306+
// ^ (AbstractCollection<Object> & Serializable & ... & Deque)
307+
}
308+
'''
309+
shouldFailWithMessages '''
310+
def c = true ? new ArrayDeque<String>() : new Stack<String>()
311+
if (c instanceof Serializable) {
312+
c.add(1)
313+
// ^ (AbstractCollection<String> & Serializable & ... & Deque)
314+
}
315+
''',
316+
'Cannot call <UnionType:java.util.AbstractCollection','#add(java.lang.String) with arguments [int]'
317+
}
318+
300319
// GROOVY-5226
301320
void testNestedInstanceOf1() {
302321
assertScript '''

0 commit comments

Comments
 (0)