changeset 57511:21dacaafdac5

8234445: spurious error message for record constructors with receiver parameters Reviewed-by: mcimadamore
author vromero
date Thu, 19 Dec 2019 11:46:35 -0500
parents 4b10f3b74fcc
children 6803658b0051
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java test/langtools/tools/javac/records/RecordCompilationTests.java
diffstat 2 files changed, 51 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Thu Dec 19 17:02:11 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Thu Dec 19 11:46:35 2019 -0500
@@ -884,6 +884,24 @@
                 completing = prevCompleting;
             }
         }
+
+        void enterThisAndSuper(ClassSymbol sym, Env<AttrContext> env) {
+            ClassType ct = (ClassType)sym.type;
+            // enter symbols for 'this' into current scope.
+            VarSymbol thisSym =
+                    new VarSymbol(FINAL | HASINIT, names._this, sym.type, sym);
+            thisSym.pos = Position.FIRSTPOS;
+            env.info.scope.enter(thisSym);
+            // if this is a class, enter symbol for 'super' into current scope.
+            if ((sym.flags_field & INTERFACE) == 0 &&
+                    ct.supertype_field.hasTag(CLASS)) {
+                VarSymbol superSym =
+                        new VarSymbol(FINAL | HASINIT, names._super,
+                                ct.supertype_field, sym);
+                superSym.pos = Position.FIRSTPOS;
+                env.info.scope.enter(superSym);
+            }
+        }
     }
 
     private final class RecordPhase extends AbstractMembersPhase {
@@ -902,6 +920,9 @@
                 for (JCVariableDecl field : fields) {
                     sym.getRecordComponent(field.sym, true);
                 }
+
+                enterThisAndSuper(sym, env);
+
                 // lets enter all constructors
                 for (JCTree def : tree.defs) {
                     if (TreeInfo.isConstructor(def)) {
@@ -932,19 +953,8 @@
                 JCTree constrDef = defaultConstructor(make.at(tree.pos), helper);
                 tree.defs = tree.defs.prepend(constrDef);
             }
-            // enter symbols for 'this' into current scope.
-            VarSymbol thisSym =
-                new VarSymbol(FINAL | HASINIT, names._this, sym.type, sym);
-            thisSym.pos = Position.FIRSTPOS;
-            env.info.scope.enter(thisSym);
-            // if this is a class, enter symbol for 'super' into current scope.
-            if ((sym.flags_field & INTERFACE) == 0 &&
-                    ct.supertype_field.hasTag(CLASS)) {
-                VarSymbol superSym =
-                    new VarSymbol(FINAL | HASINIT, names._super,
-                                  ct.supertype_field, sym);
-                superSym.pos = Position.FIRSTPOS;
-                env.info.scope.enter(superSym);
+            if (!sym.isRecord()) {
+                enterThisAndSuper(sym, env);
             }
 
             if (!tree.typarams.isEmpty()) {
--- a/test/langtools/tools/javac/records/RecordCompilationTests.java	Thu Dec 19 17:02:11 2019 +0100
+++ b/test/langtools/tools/javac/records/RecordCompilationTests.java	Thu Dec 19 11:46:35 2019 -0500
@@ -469,4 +469,32 @@
                 }
                 """);
     }
+
+    public void testReceiverParameter() {
+        assertFail("compiler.err.receiver.parameter.not.applicable.constructor.toplevel.class",
+                """
+                record R(int i) {
+                    public R(R this, int i) {
+                        this.i = i;
+                    }
+                }
+                """);
+        assertFail("compiler.err.non-static.cant.be.ref",
+                """
+                class Outer {
+                    record R(int i) {
+                        public R(Outer Outer.this, int i) {
+                            this.i = i;
+                        }
+                    }
+                }
+                """);
+        assertOK(
+                """
+                record R(int i) {
+                    void m(R this) {}
+                    public int i(R this) { return i; }
+                }
+                """);
+    }
 }