[
https://issues.apache.org/jira/browse/GROOVY-12315?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18135545#comment-18135545
]
ASF GitHub Bot commented on GROOVY-12315:
-----------------------------------------
paulk-asert opened a new pull request, #3119:
URL: https://github.com/apache/groovy/pull/3119
Reworks GROOVY-12315 (flow typing of final `def` fields) after review found
that the merged change applied the field's type to reads through its property
getter, which a subclass may override, and that reads from other classes
depended on the order of the classes in the source. Four commits:
1. **Revert GROOVY-12315 pending a narrower design** — reverts the three
merged commits so the new design is a diff of its own.
2. **Read a final `def` field with the type of its value within its class**
— the field's type is still the type of its initializer and of the plain
assignments in its constructors and initializer blocks, as for a local
variable, but it is used only where the field itself is read: in the declaring
class's own method, constructor and initializer bodies, and from a closure or
lambda for a private field or a static property. A read through the getter
keeps `Object` (closures and lambdas reading a property, inner classes,
subclasses, other classes). A value of no one type blocks the flow type rather
than pinning a synthesized bound, so constructors assigning unrelated types
cannot make an overload ambiguous. About 100 lines fewer than before: the write
scan is gone (every write outside initialization is already a compile error),
as are the cross-class readiness flags.
3. **Read a final `def` field with its flow type from a closure of a final
class** — a closure or lambda in a `final` class that declares no getter of its
own reads the generated getter, which nothing can override, so it gets the flow
type too.
4. Docs for above
Tests: `bugs.Groovy12315` (48 tests) covers the reads that use the flow
type, those that don't, and the soundness cases, including a subclass
overriding the getter. The `groovy.transform.*`, `classgen.asm.sc.*`, `gls.*`,
`bugs.*` and trait suites pass. A COMPATIBILITY entry describes the
overload-selection and `instanceof` effects under `@CompileStatic`.
This tries to be less ambitious than the earlier work. 2 is the minimal step
forward. 2 plus 3 gives more - just a little harder to explain.
For `GROOVY_6_0_X` the original change has been reverted; I envisage this is
for master only.
> Type for final field should be known by flow-typing
> ---------------------------------------------------
>
> Key: GROOVY-12315
> URL: https://issues.apache.org/jira/browse/GROOVY-12315
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 6.0.0-alpha-1, 5.0.3, 4.0.31
> Reporter: Björn Kautler
> Assignee: Daniel Sun
> Priority: Major
>
> This does not compile, while the type should be clear as the field is final:
> {code:java}
> @groovy.transform.CompileStatic
> class Foo {
> final bar = ''
> def foo() {
> println(bar.length())
> }
> }
> new Foo().foo() {code}
> With a local variable it works even without {{{}final{}}}.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)