codeconsole opened a new pull request, #3144:
URL: https://github.com/apache/groovy/pull/3144
An implicit-this call from a closure in trait code to a public static method
inherited from a super trait always called the super trait's method. The
closure's delegate was never consulted:
```groovy
trait T { static int size() { 1 } }
trait U extends T { def m() { [5, 6].with { size() } } }
class C implements U {}
assert new C().m() == 2 // was 1; the same code with classes gives 2
```
5.0.8 and 6.0.0 behave the same, in dynamic, `@TypeChecked` and
`@CompileStatic` code. This PR is stacked on #3138 (GROOVY-12611), which made
this work for private and static methods declared by the trait itself.
- `TraitReceiverTransformer` now attaches the plain implicit-this call to
calls to super trait static methods too. #3138 excluded them by checking that
the trait declares the method.
- The type checker then selects the delegate the same way it does for the
trait's own methods. Statically compiled code otherwise keeps the helper call.
- In dynamically compiled code, `ClosureMetaClass` falls back to the public
static method of a super trait helper when the trait helper does not declare
the method. It searches the super traits in the order the trait helper's
`$static_methodMissing` forwards to them. Without that fallback the closure
would reach the static method copied into the implementing class, so an
implementing class that declares its own static method of that name would be
called, while trait code outside closures calls the super trait's method.
## Tests
`TraitASTTransformationTest.testSuperTraitStaticMethodInDelegateFirstClosure`
and `testSuperTraitStaticMethodInClosureWithDelegateSetAtRuntime` fail without
the change, in dynamic, `@TypeChecked` and `@CompileStatic` modes.
`org.codehaus.groovy.transform.traitx`, `groovy.transform.stc`,
`org.codehaus.groovy.classgen.asm.sc`, `gls.closures`,
`org.codehaus.groovy.runtime.metaclass`, `groovy.Closure*` and
`groovy.ThisAndSuperTest` pass. 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]