8061549: Disallow _ as a one-character identifier

Underscore is no longer a one-charater identifier with -source 9

Reviewed-by: mcimadamore, jjg
This commit is contained in:
Jan Lahoda 2014-12-08 18:02:07 +01:00
parent fb05a03095
commit 59b3453845
18 changed files with 197 additions and 87 deletions

@ -209,6 +209,9 @@ public enum Source {
public boolean allowPrivateSafeVarargs() {
return compareTo(JDK1_9) >= 0;
}
public boolean allowUnderscoreIdentifier() {
return compareTo(JDK1_8) <= 0;
}
public static SourceVersion toSourceVersion(Source source) {
switch(source) {
case JDK1_2:

@ -157,6 +157,7 @@ public class JavacParser implements Parser {
this.allowIntersectionTypesInCast = source.allowIntersectionTypesInCast();
this.allowTypeAnnotations = source.allowTypeAnnotations();
this.allowAnnotationsAfterTypeParams = source.allowAnnotationsAfterTypeParams();
this.allowUnderscoreIdentifier = source.allowUnderscoreIdentifier();
this.keepDocComments = keepDocComments;
docComments = newDocCommentTable(keepDocComments, fac);
this.keepLineMap = keepLineMap;
@ -230,6 +231,10 @@ public class JavacParser implements Parser {
*/
boolean allowAnnotationsAfterTypeParams;
/** Switch: should we allow '_' as an identifier?
*/
boolean allowUnderscoreIdentifier;
/** Switch: is "this" allowed as an identifier?
* This is needed to parse receiver types.
*/
@ -595,7 +600,11 @@ public class JavacParser implements Parser {
return names.error;
}
} else if (token.kind == UNDERSCORE) {
warning(token.pos, "underscore.as.identifier");
if (allowUnderscoreIdentifier) {
warning(token.pos, "underscore.as.identifier");
} else {
error(token.pos, "underscore.as.identifier");
}
Name name = token.name();
nextToken();
return name;

@ -2276,8 +2276,10 @@ compiler.err.var.in.try.with.resources.not.supported.in.source=\
(use -source 9 or higher to enable variables in try-with-resources)
compiler.warn.underscore.as.identifier=\
''_'' used as an identifier\n\
(use of ''_'' as an identifier might not be supported in releases after Java SE 8)
as of release 9, ''_'' is a keyword, and may not be used as an identifier
compiler.err.underscore.as.identifier=\
as of release 9, ''_'' is a keyword, and may not be used as an identifier
compiler.err.underscore.as.identifier.in.lambda=\
''_'' used as an identifier\n\

@ -46,6 +46,7 @@ public class TestAnchorNames extends JavadocTester {
void test() {
javadoc("-d", "out",
"-sourcepath", testSrc,
"-source", "8", //so that '_' can be used as an identifier
"-use",
"pkg1");
checkExit(Exit.OK);

@ -0,0 +1,28 @@
/*
* Copyright (c) 2013, 2014, 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.underscore.as.identifier
class UnderscoreAsIdentifierError {
String _ = null;
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2014, 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
@ -22,7 +22,8 @@
*/
// key: compiler.warn.underscore.as.identifier
// options: -source 8 -Xlint:-options
class UnderscoreAsIdentifier {
class UnderscoreAsIdentifierWarning {
String _ = null;
}

@ -1,9 +1,10 @@
/*
* @test /nodynamiccopyright/
* @bug 8007401 8007427
* @bug 8007401 8007427 8061549
* @author sogoel
* @summary Test generation of warnings when '_' is used an identifier
* @compile/fail/ref=IdentifierTest.out -Werror -XDrawDiagnostics IdentifierTest.java
* @compile/fail/ref=IdentifierTest8.out -source 8 -Xlint:-options -Werror -XDrawDiagnostics IdentifierTest.java
* @compile/fail/ref=IdentifierTest9.out -XDrawDiagnostics IdentifierTest.java
*/
import java.util.List;

@ -1,47 +0,0 @@
IdentifierTest.java:40:11: compiler.warn.underscore.as.identifier
IdentifierTest.java:43:16: compiler.warn.underscore.as.identifier
IdentifierTest.java:44:20: compiler.warn.underscore.as.identifier
IdentifierTest.java:45:22: compiler.warn.underscore.as.identifier
IdentifierTest.java:50:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:50:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:50:23: compiler.warn.underscore.as.identifier
IdentifierTest.java:52:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:54:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:60:21: compiler.warn.underscore.as.identifier
IdentifierTest.java:61:42: compiler.warn.underscore.as.identifier
IdentifierTest.java:62:67: compiler.warn.underscore.as.identifier
IdentifierTest.java:69:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:70:14: compiler.warn.underscore.as.identifier
IdentifierTest.java:71:18: compiler.warn.underscore.as.identifier
IdentifierTest.java:76:22: compiler.warn.underscore.as.identifier
IdentifierTest.java:78:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:78:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:80:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:80:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:87:10: compiler.warn.underscore.as.identifier
IdentifierTest.java:87:38: compiler.warn.underscore.as.identifier
IdentifierTest.java:93:14: compiler.warn.underscore.as.identifier
IdentifierTest.java:100:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:100:26: compiler.warn.underscore.as.identifier
IdentifierTest.java:117:20: compiler.warn.underscore.as.identifier
IdentifierTest.java:122:10: compiler.warn.underscore.as.identifier
IdentifierTest.java:127:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:130:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:137:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:137:24: compiler.warn.underscore.as.identifier
IdentifierTest.java:137:33: compiler.warn.underscore.as.identifier
IdentifierTest.java:138:39: compiler.warn.underscore.as.identifier
IdentifierTest.java:142:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:143:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:144:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:149:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:150:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:156:16: compiler.warn.underscore.as.identifier
IdentifierTest.java:158:25: compiler.warn.underscore.as.identifier
IdentifierTest.java:167:5: compiler.warn.underscore.as.identifier
IdentifierTest.java:171:26: compiler.warn.underscore.as.identifier
IdentifierTest.java:173:19: compiler.warn.underscore.as.identifier
IdentifierTest.java:179:11: compiler.warn.underscore.as.identifier
- compiler.err.warnings.and.werror
1 error
44 warnings

@ -0,0 +1,47 @@
IdentifierTest.java:41:11: compiler.warn.underscore.as.identifier
IdentifierTest.java:44:16: compiler.warn.underscore.as.identifier
IdentifierTest.java:45:20: compiler.warn.underscore.as.identifier
IdentifierTest.java:46:22: compiler.warn.underscore.as.identifier
IdentifierTest.java:51:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:51:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:51:23: compiler.warn.underscore.as.identifier
IdentifierTest.java:53:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:55:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:61:21: compiler.warn.underscore.as.identifier
IdentifierTest.java:62:42: compiler.warn.underscore.as.identifier
IdentifierTest.java:63:67: compiler.warn.underscore.as.identifier
IdentifierTest.java:70:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:71:14: compiler.warn.underscore.as.identifier
IdentifierTest.java:72:18: compiler.warn.underscore.as.identifier
IdentifierTest.java:77:22: compiler.warn.underscore.as.identifier
IdentifierTest.java:79:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:79:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:81:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:81:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:88:10: compiler.warn.underscore.as.identifier
IdentifierTest.java:88:38: compiler.warn.underscore.as.identifier
IdentifierTest.java:94:14: compiler.warn.underscore.as.identifier
IdentifierTest.java:101:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:101:26: compiler.warn.underscore.as.identifier
IdentifierTest.java:118:20: compiler.warn.underscore.as.identifier
IdentifierTest.java:123:10: compiler.warn.underscore.as.identifier
IdentifierTest.java:128:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:131:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:138:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:138:24: compiler.warn.underscore.as.identifier
IdentifierTest.java:138:33: compiler.warn.underscore.as.identifier
IdentifierTest.java:139:39: compiler.warn.underscore.as.identifier
IdentifierTest.java:143:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:144:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:145:13: compiler.warn.underscore.as.identifier
IdentifierTest.java:150:15: compiler.warn.underscore.as.identifier
IdentifierTest.java:151:17: compiler.warn.underscore.as.identifier
IdentifierTest.java:157:16: compiler.warn.underscore.as.identifier
IdentifierTest.java:159:25: compiler.warn.underscore.as.identifier
IdentifierTest.java:168:5: compiler.warn.underscore.as.identifier
IdentifierTest.java:172:26: compiler.warn.underscore.as.identifier
IdentifierTest.java:174:19: compiler.warn.underscore.as.identifier
IdentifierTest.java:180:11: compiler.warn.underscore.as.identifier
- compiler.err.warnings.and.werror
1 error
44 warnings

@ -0,0 +1,45 @@
IdentifierTest.java:41:11: compiler.err.underscore.as.identifier
IdentifierTest.java:44:16: compiler.err.underscore.as.identifier
IdentifierTest.java:45:20: compiler.err.underscore.as.identifier
IdentifierTest.java:46:22: compiler.err.underscore.as.identifier
IdentifierTest.java:51:13: compiler.err.underscore.as.identifier
IdentifierTest.java:51:15: compiler.err.underscore.as.identifier
IdentifierTest.java:51:23: compiler.err.underscore.as.identifier
IdentifierTest.java:53:13: compiler.err.underscore.as.identifier
IdentifierTest.java:55:13: compiler.err.underscore.as.identifier
IdentifierTest.java:61:21: compiler.err.underscore.as.identifier
IdentifierTest.java:62:42: compiler.err.underscore.as.identifier
IdentifierTest.java:63:67: compiler.err.underscore.as.identifier
IdentifierTest.java:70:13: compiler.err.underscore.as.identifier
IdentifierTest.java:71:14: compiler.err.underscore.as.identifier
IdentifierTest.java:72:18: compiler.err.underscore.as.identifier
IdentifierTest.java:77:22: compiler.err.underscore.as.identifier
IdentifierTest.java:79:13: compiler.err.underscore.as.identifier
IdentifierTest.java:79:15: compiler.err.underscore.as.identifier
IdentifierTest.java:81:13: compiler.err.underscore.as.identifier
IdentifierTest.java:81:15: compiler.err.underscore.as.identifier
IdentifierTest.java:88:10: compiler.err.underscore.as.identifier
IdentifierTest.java:88:38: compiler.err.underscore.as.identifier
IdentifierTest.java:94:14: compiler.err.underscore.as.identifier
IdentifierTest.java:101:17: compiler.err.underscore.as.identifier
IdentifierTest.java:101:26: compiler.err.underscore.as.identifier
IdentifierTest.java:118:20: compiler.err.underscore.as.identifier
IdentifierTest.java:123:10: compiler.err.underscore.as.identifier
IdentifierTest.java:128:17: compiler.err.underscore.as.identifier
IdentifierTest.java:131:17: compiler.err.underscore.as.identifier
IdentifierTest.java:138:17: compiler.err.underscore.as.identifier
IdentifierTest.java:138:24: compiler.err.underscore.as.identifier
IdentifierTest.java:138:33: compiler.err.underscore.as.identifier
IdentifierTest.java:139:39: compiler.err.underscore.as.identifier
IdentifierTest.java:143:13: compiler.err.underscore.as.identifier
IdentifierTest.java:144:15: compiler.err.underscore.as.identifier
IdentifierTest.java:145:13: compiler.err.underscore.as.identifier
IdentifierTest.java:150:15: compiler.err.underscore.as.identifier
IdentifierTest.java:151:17: compiler.err.underscore.as.identifier
IdentifierTest.java:157:16: compiler.err.underscore.as.identifier
IdentifierTest.java:159:25: compiler.err.underscore.as.identifier
IdentifierTest.java:168:5: compiler.err.underscore.as.identifier
IdentifierTest.java:172:26: compiler.err.underscore.as.identifier
IdentifierTest.java:174:19: compiler.err.underscore.as.identifier
IdentifierTest.java:180:11: compiler.err.underscore.as.identifier
44 errors

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2014, 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
@ -24,7 +24,8 @@
/*
* @test
* @summary Check usages of underscore as identifier generate warnings
* @compile/fail/ref=WarnUnderscoreAsIdent.out -XDrawDiagnostics -Werror WarnUnderscoreAsIdent.java
* @compile/fail/ref=UnderscoreAsIdent8.out -source 8 -Xlint:-options -XDrawDiagnostics -Werror UnderscoreAsIdent.java
* @compile/fail/ref=UnderscoreAsIdent9.out -XDrawDiagnostics -Werror UnderscoreAsIdent.java
*/
package _._;

@ -0,0 +1,20 @@
UnderscoreAsIdent.java:30:9: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:30:11: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:32:8: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:32:10: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:34:7: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:35:12: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:36:10: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:36:19: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:38:16: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:41:18: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:41:25: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:41:33: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:44:34: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:47:9: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:49:19: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:51:9: compiler.warn.underscore.as.identifier
UnderscoreAsIdent.java:53:22: compiler.warn.underscore.as.identifier
- compiler.err.warnings.and.werror
1 error
17 warnings

@ -0,0 +1,18 @@
UnderscoreAsIdent.java:30:9: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:30:11: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:32:8: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:32:10: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:34:7: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:35:12: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:36:10: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:36:19: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:38:16: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:41:18: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:41:25: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:41:33: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:44:34: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:47:9: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:49:19: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:51:9: compiler.err.underscore.as.identifier
UnderscoreAsIdent.java:53:22: compiler.err.underscore.as.identifier
17 errors

@ -1,20 +0,0 @@
WarnUnderscoreAsIdent.java:29:9: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:29:11: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:31:8: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:31:10: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:33:7: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:34:12: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:35:10: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:35:19: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:37:16: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:40:18: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:40:25: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:40:33: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:43:34: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:46:9: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:48:19: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:50:9: compiler.warn.underscore.as.identifier
WarnUnderscoreAsIdent.java:52:22: compiler.warn.underscore.as.identifier
- compiler.err.warnings.and.werror
1 error
17 warnings

@ -265,19 +265,19 @@ public class TestDocComments extends JavacTestingAbstractProcessor {
class TestElementScanner extends ElementScanner<Void, Void> {
@Override
public Void visitExecutable(ExecutableElement e, Void _) {
public Void visitExecutable(ExecutableElement e, Void p) {
check(e);
return super.visitExecutable(e, _);
return super.visitExecutable(e, p);
}
@Override
public Void visitType(TypeElement e, Void _) {
public Void visitType(TypeElement e, Void p) {
check(e);
return super.visitType(e, _);
return super.visitType(e, p);
}
@Override
public Void visitVariable(VariableElement e, Void _) {
public Void visitVariable(VariableElement e, Void p) {
check(e);
return super.visitVariable(e, _);
return super.visitVariable(e, p);
}
}

@ -140,9 +140,9 @@ public class TreePosRoundsTest extends AbstractProcessor {
}
@Override
public Void visitVariable(VariableTree tree, Void _) {
public Void visitVariable(VariableTree tree, Void p) {
check(getCurrentPath());
return super.visitVariable(tree, _);
return super.visitVariable(tree, p);
}
void check(TreePath tp) {

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2014, 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
@ -22,5 +22,5 @@
*/
public class JavacWarning {
String _ = null; // this will cause a warning. It may be deprecated in JDK8
String _ = null; // this will cause a warning with -source 8 (this is an error as of -source 9)
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2014, 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
@ -46,6 +46,7 @@ public class Test {
File testSrc = new File(System.getProperty("test.src"));
String[] args = {
"-Xdoclint:none",
"-source", "8",
"-bootclasspath", System.getProperty("sun.boot.class.path"),
"-classpath", ".",
"-package",