You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/01/31 18:55:32 UTC
[groovy] 02/06: GROOVY-9455: !(x instanceof T) shouldn't propagate T as inferred type
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit c06ae8aa4e795129a922e363b8409a12ad3bd7b3
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Mar 10 11:39:24 2020 -0500
GROOVY-9455: !(x instanceof T) shouldn't propagate T as inferred type
(cherry picked from commit d5e217aa6fa17b5b015a9f7120ba77817ddbb127)
Conflicts:
src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
src/test/groovy/bugs/Groovy8523Bug.groovy
---
.../transform/stc/StaticTypeCheckingVisitor.java | 10 ++-
.../transform/stc/TypeInferenceSTCTest.groovy | 96 ++++++++++++++++++++++
2 files changed, 105 insertions(+), 1 deletion(-)
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 d6508cb..1c064ea 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -782,7 +782,15 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
}
@Override
- public void visitBinaryExpression(BinaryExpression expression) {
+ public void visitNotExpression(final NotExpression expression) {
+ // GROOVY-9455: !(x instanceof T) shouldn't propagate T as inferred type
+ typeCheckingContext.pushTemporaryTypeInfo();
+ super.visitNotExpression(expression);
+ typeCheckingContext.popTemporaryTypeInfo();
+ }
+
+ @Override
+ public void visitBinaryExpression(final BinaryExpression expression) {
int op = expression.getOperation().getType();
if (op == COMPARE_IDENTICAL) {
return; // we'll report those as errors later
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index cabdcc0..6e8128a 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -216,6 +216,102 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-8523
+ void testNotInstanceof1() {
+ assertScript '''
+ class Test1 {
+ static int checkRes = 0
+
+ static void f1(Object var1) {
+ if (!(var1 instanceof Runnable)){
+ checkRes = 3
+ return
+ }
+ f2(var1)
+ }
+
+ static void f2(Runnable var2) {
+ checkRes = 4
+ }
+ }
+
+ Runnable r = {}
+ Test1.f1(r)
+ assert Test1.checkRes == 4
+ Test1.f1(42)
+ assert Test1.checkRes == 3
+ '''
+ }
+
+ // GROOVY-8523
+ void testNotInstanceOf2() {
+ assertScript '''
+ class Test1 {
+ static int checkRes = 0
+
+ static void f1(Object var1) {
+ if (!(var1 instanceof Runnable)){
+ checkRes = 3
+ return
+ }
+ if (!(var1 instanceof List)){
+ checkRes = 5
+ return
+ }
+ f2(var1)
+ }
+
+ static void f2(Runnable var2) {
+ checkRes = 4
+ }
+ }
+
+ Runnable r = {}
+ Test1.f1(r)
+ assert Test1.checkRes == 5
+ '''
+ }
+
+ // GROOVY-8523
+ void testNotInstanceOf3() {
+ assertScript '''
+ class Test1 {
+ static int checkRes = 0
+
+ static void f1(Object var1) {
+ if (!(var1 instanceof Runnable)){
+ checkRes = 3
+ return
+ }
+ if (!(var1 instanceof Thread)){
+ checkRes = 5
+ return
+ }
+ f2(var1)
+ }
+
+ static void f2(Runnable var2) {
+ checkRes = 4
+ }
+ }
+
+ Runnable r = {}
+ Test1.f1(r)
+ assert Test1.checkRes == 5
+ '''
+ }
+
+ // GROOVY-9455
+ void testNotInstanceOf4() {
+ shouldFailWithMessages '''
+ void test(object) {
+ if (!(object instanceof String)) {
+ object.toUpperCase()
+ }
+ }
+ ''', 'Cannot find matching method java.lang.Object#toUpperCase()'
+ }
+
void testShouldNotAllowDynamicVariable() {
shouldFailWithMessages '''
String name = 'Guillaume'