RFR: 8334248: Invalid error for early construction local class constructor method reference [v2]

Maurizio Cimadamore mcimadamore at openjdk.org
Fri Jun 14 17:53:12 UTC 2024


On Fri, 14 Jun 2024 17:06:26 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

>> The new "flexible constructors" JEP 482 specifies that a local class declared in an early construction context does not have an immediate outer instance, i.e., it has the same behavior as an anonymous class would in that situation.
>> 
>> However, the compiler still (incorrectly) tries to verify that an outer instance exists in the case of a method reference to the local class' constructor.
>> 
>> Example:
>> 
>> 
>> 
>> import java.util.function.Supplier;
>> 
>> class EarlyLocalCtorRef {
>> 
>>     EarlyLocalCtorRef() {
>>         class InnerLocal { }
>>         this(InnerLocal::new);
>>     }
>> 
>>     EarlyLocalCtorRef(Supplier<Object> s) {
>>     }
>> }
>> 
>> 
>> Expected output: Successful compilation.
>> 
>> Actual output:
>> 
>> 
>> EarlyLocalCtorRef.java:5: error: cannot reference this before supertype constructor has been called
>>         this(InnerLocal::new);
>>              ^ 
>> 
>> 
>> The fix is to not look for an implicit outer instance for classes that don't have one when encountering a constructor method reference.
>> 
>> **NOTE:** The test added here will fail until [JDK-8333313](https://bugs.openjdk.org/browse/JDK-8333313) is fixed.
>
> Archie Cobbs has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Future-proof bug fix vs. new & improved semantics of hasOuterInstance().

Consider this example:


class Outer {
    void m() { }

    class Inner {
        Inner() {
            class Foo { void g() { m(); } }
            super();
            class Bar { static void r() { new Foo(); } };
        }
    }
}


This seems very related to what's being fixed here. I'm worried that the fix might not go deep enough. E.g. what about the method-reference variant of the above:


class Outer {
    void m() { }

    class Inner {
        Inner() {
            class Foo { void g() { m(); } }
            super();
            class Bar { static void r() { Supplier<Foo> sfoo = Foo::new; } };
        }
    }
}

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

PR Comment: https://git.openjdk.org/jdk/pull/19705#issuecomment-2168497505


More information about the compiler-dev mailing list