Skip to content

Commit

Permalink
GROOVY-8946
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed May 17, 2021
1 parent a35832d commit 058310c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" +
Expand All @@ -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",
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3013,6 +3013,9 @@ protected void restoreVariableExpressionMetadata(final Map<VariableExpression, L
Object value = metadata.get(marker);
if (value != null) ve.setNodeMetaData(marker, value);
}
// GRECLIPSE add -- GROOVY-8946
entry.getKey().removeNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
// GRECLIPSE end
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2733,6 +2733,9 @@ protected void restoreVariableExpressionMetadata(final Map<VariableExpression, M
entry.getKey().putNodeMetaData(marker, value);
}
}
// GRECLIPSE add -- GROOVY-8946
entry.getKey().removeNodeMetaData(DECLARATION_INFERRED_TYPE);
// GRECLIPSE end
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,9 @@ protected void restoreVariableExpressionMetadata(final Map<VariableExpression, M
entry.getKey().putNodeMetaData(marker, value);
}
}
// GRECLIPSE add -- GROOVY-8946
entry.getKey().removeNodeMetaData(DECLARATION_INFERRED_TYPE);
// GRECLIPSE end
}
}
}
Expand Down

0 comments on commit 058310c

Please sign in to comment.