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

Archie Cobbs acobbs at openjdk.org
Thu Jun 13 16:59:26 UTC 2024


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.

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

Commit messages:
 - Don't require outer instance for early construction local class method ref.

Changes: https://git.openjdk.org/jdk/pull/19705/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=19705&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8334248
  Stats: 47 lines in 2 files changed: 45 ins; 1 del; 1 mod
  Patch: https://git.openjdk.org/jdk/pull/19705.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/19705/head:pull/19705

PR: https://git.openjdk.org/jdk/pull/19705


More information about the compiler-dev mailing list