8203813: javac accepts an illegal name as a receiver parameter name
authorbsrbnd
Thu, 14 Jun 2018 05:50:21 -0700
changeset 50568 0f807f558017
parent 50567 9ee93487d262
child 50569 60d66a249db6
8203813: javac accepts an illegal name as a receiver parameter name Reviewed-by: vromero
src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
test/langtools/tools/javac/T8203813/WrongReceiverTest.java
test/langtools/tools/javac/T8203813/WrongReceiverTest.out
test/langtools/tools/javac/diags/examples/WrongReceiver.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Thu Jun 14 09:38:31 2018 -0300
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Thu Jun 14 05:50:21 2018 -0700
@@ -3127,6 +3127,9 @@
                         if (token.kind == LBRACKET) {
                             log.error(token.pos, Errors.ArrayAndReceiver);
                         }
+                        if (pn.hasTag(Tag.SELECT) && ((JCFieldAccess)pn).name != names._this) {
+                            log.error(token.pos, Errors.WrongReceiver);
+                        }
                     }
                     return toP(F.at(pos).ReceiverVarDef(mods, pn, type));
                 }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Jun 14 09:38:31 2018 -0300
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Thu Jun 14 05:50:21 2018 -0700
@@ -677,6 +677,9 @@
 compiler.err.array.and.receiver =\
     legacy array notation not allowed on receiver parameter
 
+compiler.err.wrong.receiver =\
+    wrong receiver parameter name
+
 compiler.err.variable.not.allowed=\
     variable declaration not allowed here
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8203813/WrongReceiverTest.java	Thu Jun 14 05:50:21 2018 -0700
@@ -0,0 +1,11 @@
+/*
+ * @test  /nodynamiccopyright/
+ * @bug 8203813
+ * @summary javac accepts an illegal name as a receiver parameter name
+ * @compile/fail/ref=WrongReceiverTest.out -XDrawDiagnostics WrongReceiverTest.java
+ */
+
+public class WrongReceiverTest {
+    WrongReceiverTest wr;
+    void f(WrongReceiverTest wr.wr) {}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8203813/WrongReceiverTest.out	Thu Jun 14 05:50:21 2018 -0700
@@ -0,0 +1,2 @@
+WrongReceiverTest.java:10:35: compiler.err.wrong.receiver
+1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/diags/examples/WrongReceiver.java	Thu Jun 14 05:50:21 2018 -0700
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.wrong.receiver
+
+class WrongReceiver {
+    WrongReceiver wr;
+    void f(WrongReceiver wr.wr) {}
+}