codeconsole opened a new pull request, #3106:
URL: https://github.com/apache/groovy/pull/3106

   An implicit-this call from an inner class to a method of an enclosing class 
now runs on the innermost enclosing instance whose class has the method, in 
dynamic and statically compiled code, inside and outside closures and lambdas.
   
   Before, a closure in a field initializer of an inner class of a 
`@CompileStatic` class failed in each `@CompileDynamic` constructor:
   
   ```groovy
   @CompileStatic
   class Outer {
       String pub() { 'pub' }
       class Inner {
           String v = { -> pub() }()
           @CompileDynamic Inner() {}   // GroovyCastException: Outer$Inner 
cannot be cast to Outer
       }
   }
   ```
   
   The static compiler gives the call a method target, and the dynamic 
invocation writer, which writes the initializer into the `@CompileDynamic` 
constructor, kept the closure's this object as the receiver. Lambdas, nested 
closures, private methods, interface default methods and trait methods failed 
the same way, as did a `@CompileStatic` inner class of a dynamic class.
   
   A method inherited by an intermediate enclosing class (`Outer > Mid extends 
Base > Inner` calling `base()`) failed verification under `@CompileStatic`, or 
ran on the outermost instance when that class inherits the method too. Both 
invocation writers loaded `DeclaringClass.this`; they now use the innermost 
enclosing instance whose class extends or implements the declaring class, as 
the static writer already did inside closures.
   
   Tests: `InnerClassTest.testUsageOfOuterMethod11` and 
`testUsageOfOuterMethod12` (dynamic, `@TypeChecked` and `@CompileStatic`). 
`testUsageOfOuterMethod11` and the `@CompileStatic` case of 
`testUsageOfOuterMethod12` fail without the change; the other two cases guard 
the dynamic and type-checked paths. The `groovy`, `gls`, `bugs` and 
`org.codehaus.groovy` packages pass (15,499 tests). The Checkstyle and CodeNarc 
gates pass.
   
   Seen in 4.0.33, 5.0.8 and 6.0.0.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to