8203813: javac accepts an illegal name as a receiver parameter name

Reviewed-by: vromero
This commit is contained in:
Bernard Blaser 2018-06-14 05:50:21 -07:00
parent 02db4b67cc
commit c97262c785
5 changed files with 48 additions and 0 deletions

View File

@ -3127,6 +3127,9 @@ public class JavacParser implements Parser {
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));
}

View File

@ -677,6 +677,9 @@ compiler.err.varargs.must.be.last =\
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

View File

@ -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) {}
}

View File

@ -0,0 +1,2 @@
WrongReceiverTest.java:10:35: compiler.err.wrong.receiver
1 error

View File

@ -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) {}
}