Skip to content

Commit 058310c

Browse files
committed
GROOVY-8946
1 parent a35832d commit 058310c

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java

+34-5
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,24 @@ public void testCompileStatic21() {
592592
runConformTest(sources, "22121");
593593
}
594594

595+
@Test
596+
public void testCompileStatic22() {
597+
//@formatter:off
598+
String[] sources = {
599+
"Main.groovy",
600+
"@groovy.transform.CompileStatic\n" +
601+
"void test() {\n" +
602+
" def x = '123';\n" +
603+
" { -> x = new StringBuffer() }\n" +
604+
" print x.charAt(0) // available in String and StringBuffer\n" +
605+
"}\n" +
606+
"test()\n",
607+
};
608+
//@formatter:on
609+
610+
runConformTest(sources, "1");
611+
}
612+
595613
@Test
596614
public void testCompileStatic1505() {
597615
//@formatter:off
@@ -2357,11 +2375,12 @@ public void testCompileStatic8873() {
23572375
runConformTest(sources, "");
23582376
}
23592377

2360-
@Test @Ignore("https://issues.apache.org/jira/browse/GROOVY-8946")
2378+
@Test
23612379
public void testCompileStatic8946() {
23622380
//@formatter:off
23632381
String[] sources = {
23642382
"Main.groovy",
2383+
/*
23652384
"@GrabResolver(name='grails', root='https://repo.grails.org/grails/core')\n" +
23662385
"@Grapes([\n" +
23672386
" @Grab('javax.servlet:javax.servlet-api:3.0.1'),\n" +
@@ -2370,16 +2389,26 @@ public void testCompileStatic8946() {
23702389
"])\n" +
23712390
"@GrabExclude('org.codehaus.groovy:*')\n" +
23722391
"import static grails.converters.JSON.parse\n" +
2373-
"\n" +
2392+
*/
2393+
"class JSONElement {\n" +
2394+
" def getProperty(String name) {\n" +
2395+
" if (name == 'k') return [1,2]\n" +
2396+
" }\n" +
2397+
"}\n" +
2398+
"JSONElement parse(String json) {\n" +
2399+
" new JSONElement()\n" +
2400+
"}\n" +
2401+
23742402
"@groovy.transform.CompileStatic\n" +
2375-
"void test() {\n" +
2376-
" def json = parse('[{\"k\":1},{\"k\":2}]')\n" + // returns org.grails.web.json.JSONElement
2403+
"def test() {\n" +
2404+
" def json = parse('[{\"k\":1},{\"k\":2}]')\n" +
23772405
" def vals = json['k']\n" +
23782406
" assert vals == [1,2]\n" +
23792407
" boolean result = 'k'.tokenize('.').every { token ->\n" + // 'k' represents a path like 'a.b.c.d'
2380-
" json = json[token]\n" + // GroovyCastException: Cannot cast object '[1, 2]' with class 'java.util.ArrayList' to class 'org.grails.web.json.JSONElement'
2408+
" json = json[token]\n" +
23812409
" }\n" +
23822410
" assert result\n" +
2411+
" return json\n" + // ClassCastException: java.util.ArrayList cannot cast to org.grails.web.json.JSONElement
23832412
"}\n" +
23842413
"test()\n",
23852414
};

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+3
Original file line numberDiff line numberDiff line change
@@ -3013,6 +3013,9 @@ protected void restoreVariableExpressionMetadata(final Map<VariableExpression, L
30133013
Object value = metadata.get(marker);
30143014
if (value != null) ve.setNodeMetaData(marker, value);
30153015
}
3016+
// GRECLIPSE add -- GROOVY-8946
3017+
entry.getKey().removeNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
3018+
// GRECLIPSE end
30163019
}
30173020
}
30183021
}

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+3
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,9 @@ protected void restoreVariableExpressionMetadata(final Map<VariableExpression, M
27332733
entry.getKey().putNodeMetaData(marker, value);
27342734
}
27352735
}
2736+
// GRECLIPSE add -- GROOVY-8946
2737+
entry.getKey().removeNodeMetaData(DECLARATION_INFERRED_TYPE);
2738+
// GRECLIPSE end
27362739
}
27372740
}
27382741
}

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+3
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,9 @@ protected void restoreVariableExpressionMetadata(final Map<VariableExpression, M
26552655
entry.getKey().putNodeMetaData(marker, value);
26562656
}
26572657
}
2658+
// GRECLIPSE add -- GROOVY-8946
2659+
entry.getKey().removeNodeMetaData(DECLARATION_INFERRED_TYPE);
2660+
// GRECLIPSE end
26582661
}
26592662
}
26602663
}

0 commit comments

Comments
 (0)