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

   A packed closure (`@PackedClosures`, or `-Dgroovy.target.closure.pack=true` 
under `@CompileStatic`) read and wrote owner properties as fields, skipping 
user-defined and overriding getters and setters:
   
   ```groovy
   @groovy.transform.CompileStatic
   @groovy.transform.PackedClosures
   class Box {
       int gets, sets
       BigDecimal e = 1
       BigDecimal getE() { gets++; e }
       void setE(BigDecimal v) { sets++; e = v }
       def m() { def r = { -> e++ }(); [r, this.@e, gets, sets] }
   }
   assert new Box().m() == [1, 2, 1, 1] // packed: [1, 2, 0, 0]
   ```
   
   The same held for `e`, `this.e`, `e = v`, `e += v` and prefix/postfix forms, 
in dynamic and static compilation. It also held for a subclass override of a 
generated accessor, static properties, `is` getters, and a dynamic owner 
overriding `getProperty`. It made 
`AssignmentsStaticCompileTest.testPostfixAndPrefixOnPropertyCallGetterOnce2` 
fail with the packing flag. Seen in 6.0.0 too.
   
   The body is hoisted into a method of the owner class. 
`AsmClassGenerator.visitPropertyExpression` resolves `this.name` and implicit 
`name` to the declared field in a method of the class, but not in a closure 
class. A hoisted body now skips that field shortcut, so the access takes the 
same route as in the closure class: the accessor under `@CompileStatic` (a 
plain field still compiles to direct field access), or the property call site 
in dynamic code. `this.@name` is unchanged. These closures stay packed instead 
of being declined. This is the same closure-context rule 
`StaticTypesCallSiteWriter.makeGroovyObjectGetPropertySite` already applies to 
packed bodies.
   
   ## Tests
   
   
`PackedClosuresTransformTest.packedClosureReadsAndWritesOwnerPropertiesThroughAccessors`,
 `packedClosureReachesAccessorsOverriddenOrDeclaredElsewhere` and 
`dynamicPackedClosureReadsOwnerPropertyThroughGetProperty` fail without the 
change. 
`AssignmentsStaticCompileTest.testPostfixAndPrefixOnPropertyCallGetterOnce2` 
now passes with `-Pgroovy.target.closure.pack=true`. 
`org.codehaus.groovy.classgen.asm.sc.*`, `groovy.transform.stc.*` and the 
closure-packing tests pass, and with the packing flag the same set has no new 
failures. The Checkstyle and CodeNarc gates pass.
   


-- 
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