paulk-asert opened a new pull request, #3297: URL: https://github.com/apache/groovy/pull/3297
This bundles twelve open fixes for static properties, static accessors and `@Virtual` statics in traits, so that their behaviour can be checked as a whole across dynamic, `@TypeChecked` and `@CompileStatic` code, and adds a specification of the rules to the traits guide. It targets master; which commits go to `GROOVY_6_0_X` is noted at the end. **_The "Decisions taken" section lists what was put into this PR, not decisions of the Groovy project. Each bundled PR is one commit with its original author, so the bundle can be merged as it is, trimmed or squashed._** ## What is in it One commit per PR, in this order: | JIRA | PR | Change | |---|---|---| | GROOVY-12590 | #3115 | SC: call a trait's static accessor read as a property with the implementing class | | GROOVY-12596 | #3123 | STC: accept `this.name` for a static accessor of a trait in trait code | | GROOVY-12608 | #3135 | SC: a super trait's static accessor read as a property in a closure | | GROOVY-12605 | #3132 | a static accessor of a trait as a property in a static scope of the implementing class | | GROOVY-12594 | #3121 | STC: a `@Virtual` static accessor read or written as a property | | GROOVY-12609 | #3136 | STC: the remapped name of a super trait field in a closure or for an instance field | | GROOVY-12639 | #3162 | convert a value written to a trait field as an assignment to the field | | GROOVY-12597 | #3124 | report trait static state access through the trait itself clearly | | GROOVY-12593 | #3120 | look up a static trait property in the trait, not in `java.lang.Class` | | GROOVY-12598 | #3125 | a trait property referenced from a closure is resolved through the closure's delegate | | GROOVY-12601 | #3128 | keep a `@Virtual` static call dynamic when `Class` declares a method of the same name | | GROOVY-12604 | #3131 | use the trait's own `@Virtual` static when a promoted static is called through the trait | Three commits on top, described below: two that change behaviour and one for the guide, spec tests and a behaviour matrix. ## Combining the PRs - #3123 and #3135 are stacked on #3115; each commit here has only its own change. - #3135, #3136, #3125 and #3120 change the same code in `TraitReceiverTransformer`. All of the changes are kept. - #3121 and #3128 each add an `onMethodSelection` override to `TraitTypeCheckingExtension`. They merge without a textual conflict but do not compile together, so the two are one method here (in the GROOVY-12601 commit). ## Changes on top of the twelve PRs **1. A static accessor read as a property is a call of the accessor (GROOVY-12614)** For a trait with `static String getLabel()`, and an implementing class that declares its own static `getLabel()`: | In a trait method body | dynamic | `@TypeChecked` | `@CompileStatic` | |---|---|---|---| | `label`, `this.label` (before) | class | class | trait | | `getLabel()` (before and now) | trait | trait | trait | | `label`, `this.label` (now) | trait | trait | trait | The guide and the `@Virtual` javadoc already say that a trait static is not overridden by the implementing class unless it is `@Virtual`. The property form now follows that in every mode: it is rewritten to the method form and dispatched as that is. Assignment calls the setter, and a compound assignment calls the setter with the value computed from the getter. With `@Virtual` the property form uses the implementing class's accessor, as the method form does (that needs GROOVY-12594 above under `@TypeChecked` and `@CompileStatic`). `TraitStaticDispatchMatrix.row09acc` asserted the old dynamic behaviour for a plain accessor. It now uses `@Virtual`, and `row09accp` covers the plain accessor. **2. `this.name` for a declared trait property in a closure uses the accessor (follow-up to GROOVY-12598)** With #3125, the simple name of a declared instance property in a closure is resolved by the closure and reaches the accessor of the implementing instance. `this.name` in a closure still read and wrote the trait's backing field. It now uses the accessor too, as both forms do in a closure in a class that declares the property. ## Decisions taken - A plain static accessor of a trait is not overridable from the implementing class in either form; `@Virtual` is the way to make it so. This changes dynamic and `@TypeChecked` code that relied on the property form (see `COMPATIBILITY.md`, which shows the `@Virtual` migration). A `@Virtual` static is not on the trait interface, so `Trait.label` is not available after migrating. - The existing rule that `@Virtual` does not apply to a call made from a closure or lambda (`VirtualAnnotationTest.virtual_insideClosure_stillHelperBound`) is kept and is now in the guide and the javadoc. - In a lambda, a declared instance property is the trait's backing field, as in a method body. The trait transformation cannot tell whether a lambda will be statically compiled, and a statically compiled lambda has no owner to ask for the property. ## Not changed - A name (`label` or `this.label`) of a static accessor in a closure or lambda is still left for the closure to resolve, so that its delegate can supply it. When no delegate does, dynamic and `@TypeChecked` code reach the implementing class's accessor, where the rules give the trait's own. Changing that belongs with the open PRs on closures in trait code (#3138, #3144, #3150). - A statically compiled lambda in a trait method that calls an instance method of the trait still fails with a `ClassCastException` (GROOVY-12602, #3129). - `++` and `--` on a static accessor property. ## Documentation A new section of the traits guide, "Member access in trait code", states which accessor or field a name in trait code refers to: defined terms, five rules for instance members, five for static members, and a summary table for a method body, a closure and a lambda. A note names the two places above where the implementation does not follow the table yet. - The three spec tests behind the examples run as dynamic, `@TypeChecked` and `@CompileStatic` code. - `TraitMemberAccessMatrix` asserts the whole table: 7 kinds of member, 10 forms of reference, 3 modes. Cells where the implementation differs from the rule are listed with what it does; a cell that starts to follow the rule fails its test until it is removed from the list. - `COMPATIBILITY.md` has entries for the two behaviour changes. ## Testing - Root `:test`: 20,181 tests, 0 failures. Subproject tests were not run locally. - The Checkstyle and CodeNarc gates pass, and the guide builds. - Every test method of every bundled PR is present. ## `GROOVY_6_0_X` The twelve PR commits cherry-pick onto `GROOVY_6_0_X` in order without conflicts; they have not been tested there. The two behaviour changes on top are meant for master only, and #3125 (GROOVY-12598) also changes behaviour, so it is a candidate to leave out of a backport. Supersedes #3115, #3120, #3121, #3123, #3124, #3125, #3128, #3131, #3132, #3135, #3136 and #3162. -- 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]
