Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8334488

Improve error for illegal early access from nested class

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 24
    • 23
    • tools
    • b03
    • generic
    • generic

        Consider this class:
            
            CtorPrologueBugTest {
                int x;
                CtorPrologueBugTest() {
                    class Inner {
                        { System.out.println(x); }
                    }
                    super();
                }
            }

        Prior to JDK-8194743, it resulted in this error, generated by `Attr.java`:

            CtorPrologueBugTest.java:5: error: cannot reference x before supertype constructor has been called
                        { System.out.println(x); }
                                             ^

        After JDK-8194743, it resulted in this different error, generated later in the compilation process by `Lower.java`:

            CtorPrologueBugTest.java:5: error: no enclosing instance of type CtorPrologueBugTest is in scope
                        { System.out.println(x); }
                                             ^

        There are two problems with this change in behavior:

        1. Such errors should be detected by `Attr.java`, not `Lower.java`, so that IDE's that only display errors generated by earlier compilation phases will show them.
        2. The wording change in the error message is technically incorrect: x is in scope, it's just not yet accessible.

        This change in behavior reflects an underlying logic bug, which is that Attr::visitClassDef is setting ctorPrologue = false on the containing class' environment, instead of the nested class' environment.

              mcimadamore Maurizio Cimadamore
              acobbs Archie Cobbs
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: