Flexible constructors: dubious error

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Tue Oct 29 15:22:38 UTC 2024


What Archie says - there is going to be a spec clarification and 
associated compiler enhancement to deal with the situation you describe.

Maurizio

On 29/10/2024 14:24, Archie Cobbs wrote:
> Hi Stephan,
>
> On Tue, Oct 29, 2024 at 9:11 AM Stephan Herrmann 
> <stephan.herrmann at berlin.de> wrote:
>
>     Am 29.10.24 um 14:11 schrieb Olexandr Rotan:
>     > Local classes, as well as inner ones (don't confuse with nested)
>     implicitly take
>     > parents "this" as a constructor argument
>
>     They *may* take this argument *if needed*.
>
>     But this is more an implementation detail and not the level at
>     which the
>     specification defines this, is it?
>
>
> This is a specification thing.
>
> Here is what is "supposed" to happen: Both "Local" classes in the 
> example are declared prior to super(). These classes are therefore 
> within an early construction context of "Inner" and therefore do not 
> have access to the current instance of "Inner" - but this access would 
> be required for the expression "s" which is short-hand for "Inner.this.s".
>
> So the reference to "s" should generate an error, which happens with 
> the current 24 build:
>
> X.java:14: error: cannot reference s before supertype constructor has 
> been called
> System.out.println(s);
>        ^
>
> FYI there is still an outstanding bug and associated PR 
> <https://github.com/openjdk/jdk/pull/21410> related to this situation, 
> so with the current 24 build you also get these (bogus) errors:
>
>     X.java:7: error: cannot reference this before supertype 
> constructor has been called
>                                 new Local() {};
>                                             ^
>     X.java:12: error: cannot reference this before supertype 
> constructor has been called
>                                 new Local() {
>                                             ^
>
> Those errors are bogus, because under the latest "flexible 
> constructors" spec, local classes declared in an early construction 
> context of class C are treated the same as anonymous classes in that 
> situation - they do not have any outer instance of type C - so you can 
> declare and instantiate them freely.
>
> -Archie
>
> -- 
> Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20241029/ce14921d/attachment-0001.htm>


More information about the compiler-dev mailing list