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

Archie Cobbs acobbs at openjdk.org
Thu Jun 13 21:50:15 UTC 2024


On Thu, 13 Jun 2024 21:09:36 GMT, Maurizio Cimadamore <mcimadamore 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.
>
> 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

I tried that, but you only get one "feature disabled" error per file so it doesn't work to put multiple violations in the same file.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/19712#discussion_r1638984883


More information about the compiler-dev mailing list