Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9346

Closure shared variable type is polluted with previous assignment type

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.5.8
    • Fix Version/s: None
    • Component/s: Static Type Checker
    • Labels:
      None

      Description

      class A { def foo() {42} }
      
      @groovy.transform.CompileStatic
      def cs() {
          def var  
          var = "hi" // commenting this line fixes the issue 
          var = new A() 
          def c = {
              var = new A()
          }
          c()
          var.foo() // [Static type checking] - A closure shared variable [var] has been assigned with various types and the method [foo()] 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.
      }
      assert cs() == 42
      
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              daniilo Daniil Ovchinnikov
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: