Flexible constructors: dubious error
Stephan Herrmann
stephan.herrmann at berlin.de
Tue Oct 29 15:03:07 UTC 2024
Thanks, Archie,
What you say effectively confirms my understanding that the error against access
of 's' is good, the others are wrong.
What I said about not being an specification thing is that the spec does not
mandate where exactly which synthetic argument / field is to be generated.
thanks,
Stephan
Am 29.10.24 um 15:24 schrieb Archie Cobbs:
> Hi Stephan,
>
> On Tue, Oct 29, 2024 at 9:11 AM Stephan Herrmann <stephan.herrmann at berlin.de
> <mailto: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
More information about the compiler-dev
mailing list