From b1cb54e17a2a93d048f49bca32e2e233891dc01c Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Sun, 14 Jun 2020 07:51:15 -0500 Subject: [PATCH] GROOVY-9344, GROOVY-9516: track assign in closure like if/else branch --- .../stc/StaticTypeCheckingVisitor.java | 3 +++ .../transform/stc/ClosuresSTCTest.groovy | 18 +++++++++++++++++- .../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 f5485fa5022..723965c7605 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 @@ protected void restoreVariableExpressionMetadata(final Map> 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 11ce46174c8..3eb150288fc 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' + } + + // 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 86632f8be5e..4b48fe48c40 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' '''