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

Archie Cobbs acobbs at openjdk.org
Fri Jun 14 19:52:13 UTC 2024


On Fri, 14 Jun 2024 17:50:04 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> I'm worried that the fix might not go deep enough. E.g. what about the method-reference variant of the above:

A couple of random thoughts...

For me with all current patches applied, the compiler correctly handles the version of `Outer.java` with `Foo::new`:

Outer.java:10: error: invalid constructor reference
            class Bar { static void r() { Supplier<Foo> sfoo = Foo::new; } };
                                                               ^
  cannot access constructor Foo()
    an enclosing instance of type Outer.Inner is not in scope

But it throws the usual NPE on the `Foo.new()` version:

java.lang.NullPointerException: Cannot read field "sym" because "this.lvar[0]" is null
	at jdk.compiler.interim/com.sun.tools.javac.jvm.Code.emitop0(Code.java:572)
	at jdk.compiler.interim/com.sun.tools.javac.jvm.Items$SelfItem.load(Items.java:369)
	at jdk.compiler.interim/com.sun.tools.javac.jvm.Gen.visitIdent(Gen.java:2357)

So `Outer.java` doesn't seem to be a evidence that the fix in PR #19705 is incorrect. It might be of course, but if so it would be for some other reason.

I guess the question is: must we invoke `resolveImplicitThis()` when the constructed class has `NOOUTERTHIS`?

* If so, then how do you explain the counter-example (from amber-dev email) for method references when you do that (showing a bogus error being generated):

$ cat EarlyLocalTest3.java
public class EarlyLocalTest3 {

    class Test {
        Test() {
            class InnerLocal { }
            Runnable r = InnerLocal::new;
            r.run();
            super();
        }
    }
}
$ javac -enable-preview --release 24 EarlyLocalTest3.java
../test/langtools/tools/javac/SuperInit/EarlyLocalTest3.java:6: error: cannot reference this before supertype constructor has been called
            Runnable r = InnerLocal::new;
                         ^

* If not, then there must be some other cause for the bug. Regarding this possibility, note there is another bug relating to "bogus capture in static context" e.g. [JDK-8322882](https://bugs.openjdk.org/browse/JDK-8322882). So there may be a common root cause these share.

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

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


More information about the compiler-dev mailing list