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

Archie Cobbs acobbs at openjdk.org
Fri Jun 14 17:06: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.

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().

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/19705/files
  - new: https://git.openjdk.org/jdk/pull/19705/files/18491c44..0e7496b9

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=19705&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=19705&range=00-01

  Stats: 4 lines in 1 file changed: 3 ins; 0 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