You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Daniil Ovchinnikov (Jira)" <ji...@apache.org> on 2019/12/19 14:05:00 UTC

[jira] [Created] (GROOVY-9345) Closure shared variable type is polluted with unreached assignment type

Daniil Ovchinnikov created GROOVY-9345:
------------------------------------------

             Summary: Closure shared variable type is polluted with unreached assignment type
                 Key: GROOVY-9345
                 URL: https://issues.apache.org/jira/browse/GROOVY-9345
             Project: Groovy
          Issue Type: Bug
          Components: Static compilation, Static Type Checker
            Reporter: Daniil Ovchinnikov


{code}
@groovy.transform.CompileStatic
def cs() {
    def var
    
    var = "hi"
    println var.toUpperCase() // [Static type checking] - A closure shared variable [var] has been assigned with various types and the method [toUpperCase()] does not exist in the lowest upper bound of those types: [java.lang.Object]. In general, this is a bad practice (variable reuse) because the compiler cannot determine safely what is the type of the variable at the moment of the call in a multithreaded context.
    
    def c = {
        var = new Object()
    }
    c()
    var.toString()
}

assert cs() != null
{code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)