RFR: 8193904: Uninitialized final field access and qualified this [v2]
Vicente Romero
vromero at openjdk.org
Tue Nov 29 18:18:23 UTC 2022
On Thu, 17 Nov 2022 19:10:54 GMT, Archie L. Cobbs <duke at openjdk.org> wrote:
>> If a class `MyClass` has a blank final field `foo` that has not yet been assigned, the compiler prevents you from accessing it via the expressions `foo` or `this.foo`. However, it does not prevent you from accessing it via the expression `MyClass.this.foo`.
>>
>> Here's a simple example:
>>
>> class QualifiedThis {
>> final int foo;
>> QualifiedThis() {
>> System.err.println(QualifiedThis.this.foo); // should get an error here
>> this.foo = 42;
>> }
>> }
>>
>>
>> This patch fixes that omission.
>>
>> I couldn't find an existing method that answers the question "Is this AST tree a reference to the current instance of the class I'm now compiling?" so I wrote a new one `TreeInfo.isThisReference()`.
>>
>> The question itself is a little tricky. For example, `this` alone always is, but `Foo.this` is if the class being compiled is `Foo` or any supertype of `Foo`, *except* when `Foo` is an outer class containing the class being compiled, when it instead refers to that outer instance.
>>
>> On the other hand, `Foo.super` (which, unlike `Foo.this`, can only be used to invoke a method), when `Foo` is a type and not a variable, refers to the current 'this' instance when the class being compiled is `Foo` or any supertype of `Foo`, period.
>
> Archie L. Cobbs has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains three additional commits since the last revision:
>
> - Replace any remaining uses of TreeInfo.isThisQualifier().
>
> Bug 8193904 describes how "A.this.foo" is not detected as an early reference to
> the uninitialized final variable "foo". There is also a flip-side to this bug,
> which is that "A.this.foo = ..." is not accepted as a valid assignment to the
> uninitialized final variable "foo". This commit fixes that part of the bug.
>
> Also do some minor refactoring for clarity.
> - Merge branch 'master' into JDK-8193904
> - Disallow early access to final fields via qualified 'this'.
we should push this one early on JDK 21, a bit late for 20 already
-------------
PR: https://git.openjdk.org/jdk/pull/10956
More information about the compiler-dev
mailing list