This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new cc8bc3e408 GROOVY-11815: STC: retain generics of LUB under an
`instanceof` guard
cc8bc3e408 is described below
commit cc8bc3e408f5bc7a6779c70f11a29a4cffdf04a8
Author: Eric Milles <[email protected]>
AuthorDate: Mon Dec 8 10:48:57 2025 -0600
GROOVY-11815: STC: retain generics of LUB under an `instanceof` guard
---
.../transform/stc/StaticTypeCheckingVisitor.java | 2 +-
.../transform/stc/TypeInferenceSTCTest.groovy | 21 ++++++++++++++++++++-
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 2e63acbe50..bf4451ecae 100644
---
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -6215,7 +6215,7 @@ out: for (ClassNode type : todo) {
ClassNode superclass;
ClassNode[] interfaces;
if (expressionType instanceof
WideningCategories.LowestUpperBoundClassNode) {
- superclass = expressionType.getSuperClass();
+ superclass = expressionType.getUnresolvedSuperClass(); //
GROOVY-11815
interfaces = expressionType.getInterfaces();
} else if (expressionType != null &&
expressionType.isInterface()) {
superclass = OBJECT_TYPE;
diff --git a/src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy
b/src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy
index bb12727f5c..dbf1cf374e 100644
--- a/src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -127,7 +127,7 @@ class TypeInferenceSTCTest extends
StaticTypeCheckingTestCase {
shouldFailWithMessages '''
Object o
if (o instanceof String) {
- o.toUpperCase()
+ o.toUpperCase()
} else {
o.toUpperCase() // ensure that type information is reset
}
@@ -297,6 +297,25 @@ class TypeInferenceSTCTest extends
StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-11815
+ void testInstanceOf13() {
+ assertScript '''
+ def c = true ? new ArrayDeque() : new Stack()
+ if (c instanceof Deque) {
+ c.addFirst(1)
+ // ^ (AbstractCollection<Object> & Serializable & ... & Deque)
+ }
+ '''
+ shouldFailWithMessages '''
+ def c = true ? new ArrayDeque<String>() : new Stack<String>()
+ if (c instanceof Serializable) {
+ c.add(1)
+ // ^ (AbstractCollection<String> & Serializable & ... & Deque)
+ }
+ ''',
+ 'Cannot call
<UnionType:java.util.AbstractCollection','#add(java.lang.String) with arguments
[int]'
+ }
+
// GROOVY-5226
void testNestedInstanceOf1() {
assertScript '''