RFR JDK-8203813: javac accepts an illegal name as a receiver parameter name

B. Blaser bsrbnd at gmail.com
Sat Jun 9 18:02:50 UTC 2018


Hi,

As noted in the JBS issue, the following example should be rejected
because the receiver parameter name is invalid:

class C {
    C c;
    void foo(C c.c) {
    }
}

The suggested fix is to make the parser conform to the receiver
parameter syntax (JLS10 §8.4.1):

ReceiverParameter:
    {Annotation} UnannType [Identifier .] this

Tier1 is OK.

Any feedback is welcome,
Bernard


Bug: https://bugs.openjdk.java.net/browse/JDK-8203813
Suggested fix:

diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
@@ -3126,6 +3126,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));
                 }
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
@@ -677,6 +677,9 @@
 compiler.err.array.and.receiver =\
     legacy array notation not allowed on receiver parameter

+compiler.err.wrong.receiver =\
+    Wrong receiver parameter
+
 compiler.err.variable.not.allowed=\
     variable declaration not allowed here

diff --git a/test/langtools/tools/javac/diags/examples/WrongReceiver.java
b/test/langtools/tools/javac/diags/examples/WrongReceiver.java
new file mode 100644
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/WrongReceiver.java
@@ -0,0 +1,6 @@
+// key: compiler.err.wrong.receiver
+
+class WrongReceiver {
+    WrongReceiver wr;
+    void f(WrongReceiver wr.wr);
+}


More information about the compiler-dev mailing list