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

Javac rejects receiver parameter in constructor of local class in early construction context

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 25
    • tools
    • None

      The following program fails to compile:

      ```
      class Outer1 {
         int x;

         void m() {
            class Outer2 {
                int y;

                Outer2() {
                    class Local {
                        Local(Outer2 Outer2.this) { }
                        void m() {
                            int z = x;
                        }
                    }
                    super();
                }
            }
         }
      }
      ```

      This uses the obscure "receiver parameter" syntax -- described in JLS 8.4. There's two flavor of the syntax -- one is for regular method declaration and another one (the one in this example) has to do with the "enclosing this" parameter in an inner class constructor.


      Turns out that when we are in a local class that is in the prologue of another class, that syntax is unusable -- the above example will fail with:

      ```
      error: cannot reference this before supertype constructor has been called
                        Local(Outer2 Outer2.this) { }
                                           ^
      1 error
      ```

      This is a spurious error, which is probably triggered by the fact that javac interpres `Outer.this` as if it was an enclosing this access expression (while this is a simple parameter name).

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

              Created:
              Updated: