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 2020/06/14 12:54:07 UTC
[groovy] 01/01: GROOVY-9344,
GROOVY-9516: track assign in closure like if/else branch
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY-9516
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 1d21c3d3db43b6b016f3fdf1e9ad92a058bb0247
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jun 14 07:51:15 2020 -0500
GROOVY-9344, GROOVY-9516: track assign in closure like if/else branch
---
.../transform/stc/StaticTypeCheckingVisitor.java | 3 +++
src/test/groovy/transform/stc/ClosuresSTCTest.groovy | 18 +++++++++++++++++-
.../classgen/asm/sc/StaticCompileFlowTypingTest.groovy | 15 +++++++--------
3 files changed, 27 insertions(+), 9 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 f5485fa..723965c 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2392,6 +2392,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (typesBeforeVisit != null) {
for (Map.Entry<VariableExpression, Map<StaticTypesMarker, Object>> entry : typesBeforeVisit.entrySet()) {
for (StaticTypesMarker marker : StaticTypesMarker.values()) {
+ // GROOVY-9344, GROOVY-9516
+ if (marker == INFERRED_TYPE) continue;
+
Object value = entry.getValue().get(marker);
if (value == null) {
entry.getKey().removeNodeMetaData(marker);
diff --git a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
index 11ce461..9d73f1a 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -168,7 +168,23 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
def x = '123';
{ -> x = 123 }
x.charAt(0) // available in String but not available in Integer
- ''', 'A closure shared variable [x] has been assigned with various types and the method [charAt(int)] does not exist in the lowest upper bound'
+ ''', 'Cannot find matching method java.io.Serializable or java.lang.Comparable#charAt(int)'
+ }
+
+ // GROOVY-9516
+ void testClosureSharedVariable3() {
+ shouldFailWithMessages '''
+ class A {}
+ class B extends A { def m() {} }
+ class C extends A {}
+
+ void test() {
+ def x = new B();
+ { -> x = new C() }();
+ def c = x
+ c.m()
+ }
+ ''', 'Cannot find matching method A#m()'
}
void testClosureCallAsAMethod() {
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/StaticCompileFlowTypingTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/StaticCompileFlowTypingTest.groovy
index 86632f8..4b48fe4 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/StaticCompileFlowTypingTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/StaticCompileFlowTypingTest.groovy
@@ -18,7 +18,6 @@
*/
package org.codehaus.groovy.classgen.asm.sc
-import groovy.test.NotYetImplemented
import groovy.transform.CompileStatic
import org.junit.Test
@@ -51,10 +50,10 @@ final class StaticCompileFlowTypingTest {
@groovy.transform.CompileStatic
String m() {
- def var = new A()
+ def x = new A()
def c = { ->
- var = new B()
- var.class.simpleName
+ x = new B()
+ x.class.simpleName
}
c()
}
@@ -62,7 +61,7 @@ final class StaticCompileFlowTypingTest {
'''
}
- @NotYetImplemented @Test // GROOVY-9344
+ @Test // GROOVY-9344
void testFlowTyping3() {
assertScript '''
class A {}
@@ -70,12 +69,12 @@ final class StaticCompileFlowTypingTest {
@groovy.transform.CompileStatic
String m() {
- def var = new A()
+ def x = new A()
def c = { ->
- var = new B()
+ x = new B()
}
c()
- var.class.simpleName
+ x.class.simpleName
}
assert m() == 'B'
'''