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

SC: calling trait method inside closure has incorrect receivers data

    XMLWordPrintableJSON

Details

    Description

      Consider the following

      class C {
        def m(@DelegatesTo(strategy=Closure.OWNER_ONLY, type='Void') Closure<?> block) {
          block.setResolveStrategy(Closure.OWNER_ONLY)
          block.setDelegate(null)
          return block.call()
        }
        def x() { 'C' }
        }
      
      trait T {
        def test() {
          new C().m { -> x() } // "x" must come from owner
        }
        def x() { 'T' }
      }
      
      class U implements T {
      }
      
      assert new U().test() == 'T'
      

      Under static compilation, the implicit receiver metadata that is collected by StaticTypeCheckingVisitor is incorrect because the receiver T is only added for T$Trait$Helper under the delegate path. When using OWNER_ONLY, the trait is not checked and no direct method call target is set for "x()". So it falls back to dynamic behavior.

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: