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/06/27 22:46:53 UTC
[groovy] branch GROOVY_4_0_X updated: GROOVY-10667: STC: variable declared and `instanceof` type
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new 57ffb0f7f5 GROOVY-10667: STC: variable declared and `instanceof` type
57ffb0f7f5 is described below
commit 57ffb0f7f5196b625a6ed4a9bf0934c1ab2bf394
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Jun 27 11:53:07 2022 -0500
GROOVY-10667: STC: variable declared and `instanceof` type
---
.../transform/stc/StaticTypeCheckingVisitor.java | 6 +-
.../transform/stc/TypeInferenceSTCTest.groovy | 146 ++++++++++++---------
2 files changed, 88 insertions(+), 64 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 3ceff46d76..480c7aa1e0 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2359,9 +2359,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
return node;
}
- protected ClassNode[] getArgumentTypes(final ArgumentListExpression args) {
- return args.getExpressions().stream().map(exp ->
- isNullConstant(exp) ? UNKNOWN_PARAMETER_TYPE : getInferredTypeFromTempInfo(exp, getType(exp))
+ protected ClassNode[] getArgumentTypes(final ArgumentListExpression argumentList) {
+ return argumentList.getExpressions().stream().map(exp ->
+ isNullConstant(exp) ? UNKNOWN_PARAMETER_TYPE : getType(exp)
).toArray(ClassNode[]::new);
}
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index 5f4e75ffaa..e06715b4f9 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -91,25 +91,14 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
- void testInstanceOf() {
+ void testInstanceOf1() {
assertScript '''
Object o
if (o instanceof String) o.toUpperCase()
'''
}
- void testEmbeddedInstanceOf() {
- assertScript '''
- Object o
- if (o instanceof Object) {
- if (o instanceof String) {
- o.toUpperCase()
- }
- }
- '''
- }
-
- void testEmbeddedInstanceOf2() {
+ void testInstanceOf2() {
assertScript '''
Object o
if (o instanceof String) {
@@ -120,18 +109,7 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
- void testEmbeddedInstanceOf3() {
- shouldFailWithMessages '''
- Object o
- if (o instanceof String) {
- if (o instanceof Object) { // causes the inferred type of 'o' to be overwritten
- o.toUpperCase()
- }
- }
- ''', 'Cannot find matching method java.lang.Object#toUpperCase()'
- }
-
- void testInstanceOfAfterEach() {
+ void testInstanceOf3() {
shouldFailWithMessages '''
Object o
if (o instanceof String) {
@@ -141,19 +119,60 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
''', 'Cannot find matching method java.lang.Object#toUpperCase()'
}
- void testInstanceOfInElseBranch() {
+ void testInstanceOf4() {
shouldFailWithMessages '''
Object o
if (o instanceof String) {
o.toUpperCase()
} else {
- o.toUpperCase() // ensure that type information is lost in else
+ o.toUpperCase() // ensure that type information is reset
}
- ''', 'Cannot find matching method java.lang.Object#toUpperCase()'
+ ''',
+ 'Cannot find matching method java.lang.Object#toUpperCase()'
+ }
+
+ void testInstanceOf5() {
+ assertScript '''
+ class A {
+ int foo() { 1 }
+ }
+ class B {
+ int foo2() { 2 }
+ }
+ def o = new A()
+ int result = o instanceof A?o.foo():(o instanceof B?o.foo2():3)
+ '''
+ }
+
+ void testInstanceOf6() {
+ assertScript '''
+ class A {
+ void foo() { println 'ok' }
+ }
+
+ class B {
+ void foo() { println 'ok' }
+ void foo2() { println 'ok 2' }
+ }
+
+ def o = new A()
+
+ if (o instanceof A) {
+ o.foo()
+ }
+
+ if (o instanceof B) {
+ o.foo()
+ }
+
+ if (o instanceof A || o instanceof B) {
+ o.foo()
+ }
+ '''
}
// GROOVY-9454
- void testInstanceOfOnGenericProperty() {
+ void testInstanceOf7() {
assertScript '''
interface Face {
}
@@ -179,46 +198,50 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
- void testMultipleInstanceOf() {
+ // GROOVY-10667
+ void testInstanceOf8() {
assertScript '''
- class A {
- void foo() { println 'ok' }
+ trait Tagged {
+ String tag
}
-
- class B {
- void foo() { println 'ok' }
- void foo2() { println 'ok 2' }
+ class TaggedException extends Exception implements Tagged {
}
- def o = new A()
-
- if (o instanceof A) {
- o.foo()
+ static void doSomething1(Exception e) {
+ if (e instanceof Tagged) {
+ //println e.tag
+ doSomething2(e) // Cannot find matching method #doSomething2(Tagged)
+ }
}
-
- if (o instanceof B) {
- o.foo()
+ static void doSomething2(Exception e) {
}
- if (o instanceof A || o instanceof B) {
- o.foo()
- }
+ doSomething1(new TaggedException(tag:'Test'))
'''
}
- void testInstanceOfInTernaryOp() {
+ void testNestedInstanceOf1() {
assertScript '''
- class A {
- int foo() { 1 }
- }
- class B {
- int foo2() { 2 }
+ Object o
+ if (o instanceof Object) {
+ if (o instanceof String) {
+ o.toUpperCase()
+ }
}
- def o = new A()
- int result = o instanceof A?o.foo():(o instanceof B?o.foo2():3)
'''
}
+ void testNestedInstanceOf2() {
+ shouldFailWithMessages '''
+ Object o
+ if (o instanceof String) {
+ if (o instanceof Object) { // causes the inferred type of 'o' to be overwritten
+ o.toUpperCase()
+ }
+ }
+ ''', 'Cannot find matching method java.lang.Object#toUpperCase()'
+ }
+
// GROOVY-8523
void testNotInstanceof1() {
assertScript '''
@@ -352,13 +375,6 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
''', 'Cannot find matching method java.lang.Object#toUpperCase()'
}
- void testShouldNotAllowDynamicVariable() {
- shouldFailWithMessages '''
- String name = 'Guillaume'
- println naamme
- ''', 'The variable [naamme] is undeclared'
- }
-
void testInstanceOfInferenceWithImplicitIt() {
assertScript '''
['a', 'b', 'c'].each {
@@ -501,6 +517,14 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
+ void testShouldNotAllowDynamicVariable() {
+ shouldFailWithMessages '''
+ String name = 'Guillaume'
+ println naamme
+ ''',
+ 'The variable [naamme] is undeclared'
+ }
+
void testShouldNotFailWithWith() {
assertScript '''
class A {