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

@CompileStatic doesn't respect resolve strategy on closures

    XMLWordPrintableJSON

Details

    Description

      
      class Root {
        Parent parent = new Parent()
      
        def parent(@DelegatesTo(Parent) Closure cl) {
          cl.delegate = parent
          cl()
        }
      }
      
      class Parent {
        Child child = new Child()
      
        def child(@DelegatesTo(Child) Closure cl) {
          cl.delegate = child
          cl()
        }
      
        def foo() { "parent" }
      }
      
      class Child {
        def foo() { "child" }
      }
      
      @groovy.transform.CompileStatic
      def usage() {
        new Root().parent {
          child {
            foo()
          }
        }
      }
      
      println usage()
      

      Expected result: parent
      Actual result: child

      Closure inside child {} call has default resolve strategy, i.e. owner first. Its owner is a closure passed to parent {}, which is delegated to Parent, which in turn has the method foo.

      Works properly without @CompileStatic.

      Attachments

        Activity

          People

            Unassigned Unassigned
            daniilo Daniil Ovchinnikov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: