From d4d132d0d6936fb6dc91eae4cba0cdb2a0bcef73 Mon Sep 17 00:00:00 2001 From: vromero Date: Fri, 6 Apr 2018 09:15:09 -0400 Subject: [PATCH] 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor Reviewed-by: mcimadamore --- .../sun/tools/javac/parser/JavacParser.java | 43 +++++++++---------- .../tools/javac/resources/compiler.properties | 3 +- .../IncorrectMsgQualifiedReceiverTest.java | 11 +++++ .../IncorrectMsgQualifiedReceiverTest.out | 3 ++ .../tools/javac/lambda/8131742/T8131742.out | 4 +- 5 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.java create mode 100644 test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.out 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 index dd18927c29..05d81f9935 100644 --- 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 @@ -3106,35 +3106,34 @@ public class JavacParser implements Parser { 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 @@ public class JavacParser implements Parser { JCVariableDecl lastParam; accept(LPAREN); if (token.kind != RPAREN) { - this.allowThisIdent = true; + this.allowThisIdent = !lambdaParameters; lastParam = formalParameter(lambdaParameters); if (lastParam.nameexpr != null) { this.receiverParam = lastParam; 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 index 45aee082db..7d5891b02e 100644 --- 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 @@ -2740,7 +2740,8 @@ compiler.err.assert.as.identifier=\ # 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 diff --git a/test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.java b/test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.java new file mode 100644 index 0000000000..5ff982a927 --- /dev/null +++ b/test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.java @@ -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) {} +} diff --git a/test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.out b/test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.out new file mode 100644 index 0000000000..364a5c1b5d --- /dev/null +++ b/test/langtools/tools/javac/T8199744/IncorrectMsgQualifiedReceiverTest.out @@ -0,0 +1,3 @@ +IncorrectMsgQualifiedReceiverTest.java:9:91: compiler.err.this.as.identifier +IncorrectMsgQualifiedReceiverTest.java:10:91: compiler.err.this.as.identifier +2 errors diff --git a/test/langtools/tools/javac/lambda/8131742/T8131742.out b/test/langtools/tools/javac/lambda/8131742/T8131742.out index b8fd425543..efc3c1c97f 100644 --- a/test/langtools/tools/javac/lambda/8131742/T8131742.out +++ b/test/langtools/tools/javac/lambda/8131742/T8131742.out @@ -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 -- GitLab