Invalid compiler error with early reference in nested class

XMLWordPrintable

    • Type: Bug
    • Resolution: Duplicate
    • Priority: P4
    • 24
    • Affects Version/s: 24
    • Component/s: tools
    • Environment:

      jdk-24+5-72-g5c612c230b0

    • 24
    • generic
    • generic

      The following example should compile, but does not:

      class C1 {
          String f1 = "f1";
          C1() {
              class C2 { // not early for C1
                  C2() {
                      class C3 { // early for C2
                          C3() {
                              f1.hashCode(); // compiles ok
                              class C4 { // not early for C3
                                  C4() {
                                      f1.hashCode(); // bogus error
                                  }
                              }
                          }
                      }
                      super();
                  }
              }
          }
      }

      $ javac -d classes --release 24 --enable-preview C1.java
      C1.java:11: error: no enclosing instance of type C1 is in scope
                                      f1.hashCode(); // bogus error
                                      ^
      Note: C1.java uses preview features of Java SE 24.

      Neither reference to "f1" is in the early initialization context of C1, so they should both be allowed.

            Assignee:
            Unassigned
            Reporter:
            Archie Cobbs
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: