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

CompileStatic: Runtime error: Access outer class property from static inner in @DelegatesTo

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.4
    • 4.0.0-alpha-1, 3.0.5
    • Static compilation
    • None
    • openjdk version "1.8.0_242"
      OpenJDK Runtime Environment (Zulu 8.44.0.11-CA-macosx) (build 1.8.0_242-b20)
      OpenJDK 64-Bit Server VM (Zulu 8.44.0.11-CA-macosx) (build 25.242-b20, mixed mode)

    Description

      When trying to access the property of a outer class from a static inner class the code should fail to compile.

      It doesn't fail to compile when the access happens from inside a @DelegatesTo Closure.

       

      
      import groovy.transform.CompileStatic
      
      @CompileStatic
      class Delegate {
        static void delegate(@DelegatesTo(Delegate) Closure c) {
            c.delegate = new Delegate()
            c()
        }
      }
      
      @CompileStatic
      class Outer {
        String propertyName
      
        static class Inner {
          void someFunction() {
            // Moving this here results in a compilation error
            // println(propertyName)
            Delegate.delegate {
              // This causes a GroovyCastException at runtime because the generated code tries to access propertyName as if this were a instance of Outer?
              println(propertyName)
            }
          }
        }
      }
      
      
      new Outer.Inner().someFunction()
      

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              fesc Felix Scheinost
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1h
                  1h