[ 
https://issues.apache.org/jira/browse/GROOVY-6668?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16478703#comment-16478703
 ] 

ASF GitHub Bot commented on GROOVY-6668:
----------------------------------------

Github user blackdrag commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/708#discussion_r188869080
  
    --- Diff: 
src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java 
---
    @@ -4101,13 +4101,17 @@ protected static ClassNode 
getGroupOperationResultType(ClassNode a, ClassNode b)
             return Number_TYPE;
         }
     
    -    protected ClassNode inferComponentType(final ClassNode containerType, 
final ClassNode indexType) {
    +    private static ClassNode convertGStringToStringType(ClassNode cn) {
    +        return GSTRING_TYPE.equals(cn) ? STRING_TYPE : cn;
    +    }
    +
    +    protected ClassNode inferComponentType(final ClassNode containerType, 
ClassNode indexType) {
             final ClassNode componentType = containerType.getComponentType();
             if (componentType == null) {
                 // GROOVY-5521
                 // try to identify a getAt method
                 typeCheckingContext.pushErrorCollector();
    -            MethodCallExpression vcall = callX(varX("_hash_", 
containerType), "getAt", varX("_index_", indexType));
    +            MethodCallExpression vcall = callX(varX("_hash_", 
containerType), "getAt", varX("_index_", 
convertGStringToStringType(indexType)));
    --- End diff --
    
    we can treat GString as a String if we know the target is a String. Plus 
the rules for assignment and for method calls are not identical. But my comment 
was not about adding getAt(GString) for Map. My comment was that this change 
looks like the following will no longer work:
      
          class X {
            int getAt(GString gs) {1}
          }
          assert new X()["${42}"] == 1


> Static compiler doesn't coerce GString for getAt() call
> -------------------------------------------------------
>
>                 Key: GROOVY-6668
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6668
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 2.3.0-beta-1, 2.4.0-rc-1
>            Reporter: Luke Daley
>            Priority: Major
>
> This might not be a bug, but it's a behaviour change from 2.2.
> {code}
> @groovy.transform.CompileStatic
> class OtherThing {
>     OtherThing() {
>         Map<String, String> m = [:]
>         def k = "foo"
>         m["$k"].toUpperCase() // fails, no method toUpperCase() on object
>         m[k].toUpperCase() // works
>     }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to