RFR: 8334258: Compiler erronousely allows access to instance variable in argument expression of a constructor invocation

Maurizio Cimadamore mcimadamore at openjdk.org
Thu Jun 13 21:12:11 UTC 2024


On Thu, 13 Jun 2024 21:01:14 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

> For the "flexible constructors" JEP 482 feature, the compiler allows assignments to instance fields in early construction contexts. However, when it creates this exception it is failing to check that the feature is enabled. This patch adds that check.

Marked as reviewed by mcimadamore (Reviewer).

test/langtools/tools/javac/SuperInit/EarlyAssignmentNoPreview3.java line 12:

> 10: 
> 11:     public EarlyAssignmentNoPreview3() {
> 12:         this(EarlyAssignmentNoPreview3.this.r = () -> System.out.println("hello"));

Optional suggestion: you could in principle consolidate these tests by overloading the constructor - e.g. you can have

Foo(int)
Foo(int, int)
Foo(int, int, int)

And each of those could delegate to `Foo(Runnable)`, with slightly different expressions

-------------

PR Review: https://git.openjdk.org/jdk/pull/19712#pullrequestreview-2117038880
PR Review Comment: https://git.openjdk.org/jdk/pull/19712#discussion_r1638952923


More information about the compiler-dev mailing list