8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
authorvromero
Fri, 06 Apr 2018 09:15:09 -0400
changeset 49543 2d5d75263e77
parent 49542 da62fa14a3fe
child 49544 d958597c7908
8199744: Incorrect compiler message for ReceiverParameter in inner class constructor Reviewed-by: mcimadamore
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/T8199744/IncorrectMsgQualifiedReceiverTest.java
test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.out
test/langtools/tools/javac/lambda/8131742/T8131742.out
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Apr 06 17:27:24 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Fri Apr 06 09:15:09 2018 -0400
@@ -3106,35 +3106,34 @@
             name = token.name();
             nextToken();
         } else {
-            if (allowThisIdent && !lambdaParameter) {
+            if (allowThisIdent ||
+                !lambdaParameter ||
+                LAX_IDENTIFIER.accepts(token.kind) ||
+                mods.flags != Flags.PARAMETER ||
+                mods.annotations.nonEmpty()) {
                 JCExpression pn = qualident(false);
                 if (pn.hasTag(Tag.IDENT) && ((JCIdent)pn).name != names._this) {
                     name = ((JCIdent)pn).name;
                 } else {
-                    if ((mods.flags & Flags.VARARGS) != 0) {
-                        log.error(token.pos, Errors.VarargsAndReceiver);
-                    }
-                    if (token.kind == LBRACKET) {
-                        log.error(token.pos, Errors.ArrayAndReceiver);
+                    if (allowThisIdent) {
+                        if ((mods.flags & Flags.VARARGS) != 0) {
+                            log.error(token.pos, Errors.VarargsAndReceiver);
+                        }
+                        if (token.kind == LBRACKET) {
+                            log.error(token.pos, Errors.ArrayAndReceiver);
+                        }
                     }
                     return toP(F.at(pos).ReceiverVarDef(mods, pn, type));
                 }
             } else {
-                if (!lambdaParameter ||
-                        LAX_IDENTIFIER.accepts(token.kind) ||
-                        mods.flags != Flags.PARAMETER ||
-                        mods.annotations.nonEmpty()) {
-                    name = ident();
-                } else {
-                    /** if it is a lambda parameter and the token kind is not an identifier,
-                     *  and there are no modifiers or annotations, then this means that the compiler
-                     *  supposed the lambda to be explicit but it can contain a mix of implicit,
-                     *  var or explicit parameters. So we assign the error name to the parameter name
-                     *  instead of issuing an error and analyze the lambda parameters as a whole at
-                     *  a higher level.
-                     */
-                    name = names.empty;
-                }
+                /** if it is a lambda parameter and the token kind is not an identifier,
+                 *  and there are no modifiers or annotations, then this means that the compiler
+                 *  supposed the lambda to be explicit but it can contain a mix of implicit,
+                 *  var or explicit parameters. So we assign the error name to the parameter name
+                 *  instead of issuing an error and analyze the lambda parameters as a whole at
+                 *  a higher level.
+                 */
+                name = names.empty;
             }
         }
         if ((mods.flags & Flags.VARARGS) != 0 &&
@@ -3905,7 +3904,7 @@
         JCVariableDecl lastParam;
         accept(LPAREN);
         if (token.kind != RPAREN) {
-            this.allowThisIdent = true;
+            this.allowThisIdent = !lambdaParameters;
             lastParam = formalParameter(lambdaParameters);
             if (lastParam.nameexpr != null) {
                 this.receiverParam = lastParam;
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Apr 06 17:27:24 2018 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Fri Apr 06 09:15:09 2018 -0400
@@ -2740,7 +2740,8 @@
 
 # TODO 308: make a better error message
 compiler.err.this.as.identifier=\
-    as of release 8, ''this'' is allowed as the parameter name for the receiver type only, which has to be the first parameter
+    as of release 8, ''this'' is allowed as the parameter name for the receiver type only\n\
+    which has to be the first parameter, and cannot be a lambda parameter
 
 compiler.err.receiver.parameter.not.applicable.constructor.toplevel.class=\
     receiver parameter not applicable for constructor of top-level class
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.java	Fri Apr 06 09:15:09 2018 -0400
@@ -0,0 +1,11 @@
+/*
+ * @test  /nodynamiccopyright/
+ * @bug 8199744
+ * @summary Incorrect compiler message for ReceiverParameter in inner class constructor
+ * @compile/fail/ref=IncorrectMsgQualifiedReceiverTest.out -XDrawDiagnostics IncorrectMsgQualifiedReceiverTest.java
+ */
+
+class IncorrectMsgQualifiedReceiverTest {
+    void foo(int any, IncorrectMsgQualifiedReceiverTest IncorrectMsgQualifiedReceiverTest.this) {}
+    void bar(int any, IncorrectMsgQualifiedReceiverTest IncorrectMsgQualifiedReceiverTest.this, int another) {}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.out	Fri Apr 06 09:15:09 2018 -0400
@@ -0,0 +1,3 @@
+IncorrectMsgQualifiedReceiverTest.java:9:91: compiler.err.this.as.identifier
+IncorrectMsgQualifiedReceiverTest.java:10:91: compiler.err.this.as.identifier
+2 errors
--- a/test/langtools/tools/javac/lambda/8131742/T8131742.out	Fri Apr 06 17:27:24 2018 +0530
+++ b/test/langtools/tools/javac/lambda/8131742/T8131742.out	Fri Apr 06 09:15:09 2018 -0400
@@ -1,4 +1,2 @@
-T8131742.java:8:38: compiler.err.expected3: ',', ')', '['
 T8131742.java:8:39: compiler.err.this.as.identifier
-T8131742.java:8:43: compiler.err.expected: ';'
-3 errors
+1 error