From 058310cb700473827fbeb120e56cb1b43d868b45 Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Mon, 17 May 2021 14:16:09 -0500 Subject: [PATCH] GROOVY-8946 --- .../tests/xform/StaticCompilationTests.java | 39 ++++++++++++++++--- .../stc/StaticTypeCheckingVisitor.java | 3 ++ .../stc/StaticTypeCheckingVisitor.java | 3 ++ .../stc/StaticTypeCheckingVisitor.java | 3 ++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java index 347b0fef9d..a8a1b329b6 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java @@ -592,6 +592,24 @@ public void testCompileStatic21() { runConformTest(sources, "22121"); } + @Test + public void testCompileStatic22() { + //@formatter:off + String[] sources = { + "Main.groovy", + "@groovy.transform.CompileStatic\n" + + "void test() {\n" + + " def x = '123';\n" + + " { -> x = new StringBuffer() }\n" + + " print x.charAt(0) // available in String and StringBuffer\n" + + "}\n" + + "test()\n", + }; + //@formatter:on + + runConformTest(sources, "1"); + } + @Test public void testCompileStatic1505() { //@formatter:off @@ -2357,11 +2375,12 @@ public void testCompileStatic8873() { runConformTest(sources, ""); } - @Test @Ignore("https://issues.apache.org/jira/browse/GROOVY-8946") + @Test public void testCompileStatic8946() { //@formatter:off String[] sources = { "Main.groovy", + /* "@GrabResolver(name='grails', root='https://repo.grails.org/grails/core')\n" + "@Grapes([\n" + " @Grab('javax.servlet:javax.servlet-api:3.0.1'),\n" + @@ -2370,16 +2389,26 @@ public void testCompileStatic8946() { "])\n" + "@GrabExclude('org.codehaus.groovy:*')\n" + "import static grails.converters.JSON.parse\n" + - "\n" + + */ + "class JSONElement {\n" + + " def getProperty(String name) {\n" + + " if (name == 'k') return [1,2]\n" + + " }\n" + + "}\n" + + "JSONElement parse(String json) {\n" + + " new JSONElement()\n" + + "}\n" + + "@groovy.transform.CompileStatic\n" + - "void test() {\n" + - " def json = parse('[{\"k\":1},{\"k\":2}]')\n" + // returns org.grails.web.json.JSONElement + "def test() {\n" + + " def json = parse('[{\"k\":1},{\"k\":2}]')\n" + " def vals = json['k']\n" + " assert vals == [1,2]\n" + " boolean result = 'k'.tokenize('.').every { token ->\n" + // 'k' represents a path like 'a.b.c.d' - " json = json[token]\n" + // GroovyCastException: Cannot cast object '[1, 2]' with class 'java.util.ArrayList' to class 'org.grails.web.json.JSONElement' + " json = json[token]\n" + " }\n" + " assert result\n" + + " return json\n" + // ClassCastException: java.util.ArrayList cannot cast to org.grails.web.json.JSONElement "}\n" + "test()\n", }; diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index a610ac4b15..ff74ea3e1e 100644 --- a/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -3013,6 +3013,9 @@ protected void restoreVariableExpressionMetadata(final Map