[
https://issues.apache.org/jira/browse/GROOVY-9603?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17142121#comment-17142121
]
Eric Milles commented on GROOVY-9603:
-------------------------------------
This is from StaticTypeCheckingVisitor:
{code:java}
private void addMapAssignmentConstructorErrors(final ClassNode
leftRedirect, final Expression leftExpression, final Expression
rightExpression) {
// if left type is not a list but right type is a map, then we're in
the case of a groovy
// constructor type : A a = [x:2, y:3]
// In this case, more checks can be performed
if (!implementsInterfaceOrIsSubclassOf(leftRedirect, MAP_TYPE) &&
rightExpression instanceof MapExpression) {
if (!(leftExpression instanceof VariableExpression) ||
!((VariableExpression) leftExpression).isDynamicTyped()) {
ArgumentListExpression argList = args(rightExpression);
ClassNode[] argTypes = getArgumentTypes(argList);
checkGroovyStyleConstructor(leftRedirect, argTypes,
rightExpression);
// perform additional type checking on arguments
MapExpression mapExpression = (MapExpression) rightExpression;
checkGroovyConstructorMap(leftExpression, leftRedirect,
mapExpression);
}
}
}
{code}
> Assignment of Map literal to Map element fails to compile (static)
> ------------------------------------------------------------------
>
> Key: GROOVY-9603
> URL: https://issues.apache.org/jira/browse/GROOVY-9603
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation, Static Type Checker
> Affects Versions: 3.0.3, 3.0.4
> Reporter: Jochen Eddelbuettel
> Priority: Major
>
> Declare a Map<String,Object>. When trying to put a map literal into that map,
> assignments in property style (map.key = ...) and subscript style (map[key] =
> ...) fail with
> *[Static type checking] - No such property: ... for class: java.lang.Object*
> First assigning to a local variable gives no such problems. If the Map were
> to contain objects of a specific type, coercion from map to that particular
> type would make sense. This coercion from Map to Object makes to sense. A Map
> is an Object and should simply become the new value for that particular key,
> which is just what is happening with regular compilation.
> Same for Map<String,?> and Map<String,? extends Object
> {code:groovy}
> @groovy.transform.CompileStatic
> def updateMap(Map<String,Object> map) {
> def innerMap = [ foo: "bar", bar: "foo" ]
> // No problem on this assignment
> map.someProp = innerMap
> assert map.someProp["foo"] == "bar"
> // The next three lines should all be equivalent
> // but the 2nd and 3rd don't CompileStatic
> map.put("someProp", [ abc: "efg" ])
> map["someProp"] = [ abc: "hij" ]
> map.someProp = [ abc: "klm" ]
> assert map.someProp["abc"] == "klm"
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)