7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
Summary: Diamond accepts non-parameterized member inner classes with parameterized outer because of a bad check
Reviewed-by: jjg
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Fri Mar 25 15:17:52 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Mar 29 16:40:07 2011 +0100
@@ -676,7 +676,7 @@
"cant.apply.diamond.1",
t, diags.fragment("diamond.and.anon.class", t));
return types.createErrorType(t);
- } else if (!t.tsym.type.isParameterized()) {
+ } else if (t.tsym.type.getTypeArguments().isEmpty()) {
log.error(tree.clazz.pos(),
"cant.apply.diamond.1",
t, diags.fragment("diamond.non.generic", t));
--- a/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Fri Mar 25 15:17:52 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Mar 29 16:40:07 2011 +0100
@@ -971,7 +971,7 @@
if ((mode & EXPR) != 0) {
mode = EXPR;
S.nextToken();
- if (S.token() == LT) typeArgs = typeArguments();
+ if (S.token() == LT) typeArgs = typeArguments(false);
t = creator(pos, typeArgs);
typeArgs = null;
} else return illegal();
@@ -1036,7 +1036,7 @@
mode = EXPR;
int pos1 = S.pos();
S.nextToken();
- if (S.token() == LT) typeArgs = typeArguments();
+ if (S.token() == LT) typeArgs = typeArguments(false);
t = innerCreator(pos1, typeArgs, t);
typeArgs = null;
break loop;
@@ -1116,7 +1116,7 @@
mode = EXPR;
int pos2 = S.pos();
S.nextToken();
- if (S.token() == LT) typeArgs = typeArguments();
+ if (S.token() == LT) typeArgs = typeArguments(false);
t = innerCreator(pos2, typeArgs, t);
typeArgs = null;
} else {
@@ -1146,7 +1146,7 @@
} else {
int pos = S.pos();
accept(DOT);
- typeArgs = (S.token() == LT) ? typeArguments() : null;
+ typeArgs = (S.token() == LT) ? typeArguments(false) : null;
t = toP(F.at(pos).Select(t, ident()));
t = argumentsOpt(typeArgs, t);
}
@@ -1206,7 +1206,7 @@
(mode & NOPARAMS) == 0) {
mode = TYPE;
checkGenerics();
- return typeArguments(t);
+ return typeArguments(t, false);
} else {
return t;
}
@@ -1223,51 +1223,54 @@
illegal();
}
mode = useMode;
- return typeArguments();
+ return typeArguments(false);
}
return null;
}
/** TypeArguments = "<" TypeArgument {"," TypeArgument} ">"
*/
- List<JCExpression> typeArguments() {
- ListBuffer<JCExpression> args = lb();
+ List<JCExpression> typeArguments(boolean diamondAllowed) {
if (S.token() == LT) {
S.nextToken();
- if (S.token() == GT && (mode & DIAMOND) != 0) {
+ if (S.token() == GT && diamondAllowed) {
checkDiamond();
+ mode |= DIAMOND;
S.nextToken();
return List.nil();
- }
- args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
- while (S.token() == COMMA) {
- S.nextToken();
+ } else {
+ ListBuffer<JCExpression> args = ListBuffer.lb();
args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
- }
- switch (S.token()) {
- case GTGTGTEQ:
- S.token(GTGTEQ);
- break;
- case GTGTEQ:
- S.token(GTEQ);
- break;
- case GTEQ:
- S.token(EQ);
- break;
- case GTGTGT:
- S.token(GTGT);
- break;
- case GTGT:
- S.token(GT);
- break;
- default:
- accept(GT);
- break;
+ while (S.token() == COMMA) {
+ S.nextToken();
+ args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
+ }
+ switch (S.token()) {
+ case GTGTGTEQ:
+ S.token(GTGTEQ);
+ break;
+ case GTGTEQ:
+ S.token(GTEQ);
+ break;
+ case GTEQ:
+ S.token(EQ);
+ break;
+ case GTGTGT:
+ S.token(GTGT);
+ break;
+ case GTGT:
+ S.token(GT);
+ break;
+ default:
+ accept(GT);
+ break;
+ }
+ return args.toList();
}
} else {
syntaxError(S.pos(), "expected", LT);
+ return List.nil();
}
- return args.toList();
}
/** TypeArgument = Type
@@ -1303,9 +1306,9 @@
}
}
- JCTypeApply typeArguments(JCExpression t) {
+ JCTypeApply typeArguments(JCExpression t, boolean diamondAllowed) {
int pos = S.pos();
- List<JCExpression> args = typeArguments();
+ List<JCExpression> args = typeArguments(diamondAllowed);
return toP(F.at(pos).TypeApply(t, args));
}
@@ -1370,18 +1373,25 @@
}
JCExpression t = qualident();
int oldmode = mode;
- mode = TYPE | DIAMOND;
+ mode = TYPE;
+ boolean diamondFound = false;
if (S.token() == LT) {
checkGenerics();
- t = typeArguments(t);
+ t = typeArguments(t, true);
+ diamondFound = (mode & DIAMOND) != 0;
}
while (S.token() == DOT) {
+ if (diamondFound) {
+ //cannot select after a diamond
+ illegal(S.pos());
+ }
int pos = S.pos();
S.nextToken();
t = toP(F.at(pos).Select(t, ident()));
if (S.token() == LT) {
checkGenerics();
- t = typeArguments(t);
+ t = typeArguments(t, true);
+ diamondFound = (mode & DIAMOND) != 0;
}
}
mode = oldmode;
@@ -1416,9 +1426,8 @@
JCExpression t = toP(F.at(S.pos()).Ident(ident()));
if (S.token() == LT) {
int oldmode = mode;
- mode |= DIAMOND;
checkGenerics();
- t = typeArguments(t);
+ t = typeArguments(t, true);
mode = oldmode;
}
return classCreatorRest(newpos, encl, typeArgs, t);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/diamond/7030687/ParserTest.java Tue Mar 29 16:40:07 2011 +0100
@@ -0,0 +1,175 @@
+/*
+ * Copyright (c) 2011, 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.
+ */
+
+/*
+ * @test
+ * @bug 7030687
+ * @summary Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
+ */
+
+import com.sun.source.util.JavacTask;
+import java.net.URI;
+import java.util.Arrays;
+import javax.tools.Diagnostic;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
+
+public class ParserTest {
+
+ enum TypeArgumentKind {
+ NONE(""),
+ EXPLICIT("<String>"),
+ DIAMOND("<>");
+
+ String typeargStr;
+
+ private TypeArgumentKind(String typeargStr) {
+ this.typeargStr = typeargStr;
+ }
+ }
+
+ enum TypeQualifierArity {
+ ONE(1, "A1#TA1"),
+ TWO(2, "A1#TA1.A2#TA2"),
+ THREE(3, "A1#TA1.A2#TA2.A3#TA3"),
+ FOUR(4, "A1#TA1.A2#TA2.A3#TA3.A4#TA4");
+
+ int n;
+ String qualifierStr;
+
+ private TypeQualifierArity(int n, String qualifierStr) {
+ this.n = n;
+ this.qualifierStr = qualifierStr;
+ }
+
+ String getType(TypeArgumentKind... typeArgumentKinds) {
+ String res = qualifierStr;
+ for (int i = 1 ; i <= typeArgumentKinds.length ; i++) {
+ res = res.replace("#TA" + i, typeArgumentKinds[i-1].typeargStr);
+ }
+ return res;
+ }
+ }
+
+ public static void main(String... args) throws Exception {
+
+ //create default shared JavaCompiler - reused across multiple compilations
+ JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
+ StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
+
+ for (TypeQualifierArity arity : TypeQualifierArity.values()) {
+ for (TypeArgumentKind tak1 : TypeArgumentKind.values()) {
+ if (arity == TypeQualifierArity.ONE) {
+ new ParserTest(arity, tak1).run(comp, fm);
+ continue;
+ }
+ for (TypeArgumentKind tak2 : TypeArgumentKind.values()) {
+ if (arity == TypeQualifierArity.TWO) {
+ new ParserTest(arity, tak1, tak2).run(comp, fm);
+ continue;
+ }
+ for (TypeArgumentKind tak3 : TypeArgumentKind.values()) {
+ if (arity == TypeQualifierArity.THREE) {
+ new ParserTest(arity, tak1, tak2, tak3).run(comp, fm);
+ continue;
+ }
+ for (TypeArgumentKind tak4 : TypeArgumentKind.values()) {
+ new ParserTest(arity, tak1, tak2, tak3, tak4).run(comp, fm);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ TypeQualifierArity qualifierArity;
+ TypeArgumentKind[] typeArgumentKinds;
+ JavaSource source;
+ DiagnosticChecker diagChecker;
+
+ ParserTest(TypeQualifierArity qualifierArity, TypeArgumentKind... typeArgumentKinds) {
+ this.qualifierArity = qualifierArity;
+ this.typeArgumentKinds = typeArgumentKinds;
+ this.source = new JavaSource();
+ this.diagChecker = new DiagnosticChecker();
+ }
+
+ class JavaSource extends SimpleJavaFileObject {
+
+ String template = "class Test {\n" +
+ "R res = new #T();\n" +
+ "}\n";
+
+ String source;
+
+ public JavaSource() {
+ super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
+ source = template.replace("#T", qualifierArity.getType(typeArgumentKinds));
+ }
+
+ @Override
+ public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+ return source;
+ }
+ }
+
+ void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
+ JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
+ null, null, Arrays.asList(source));
+ ct.parse();
+ check();
+ }
+
+ void check() {
+
+ boolean errorExpected = false;
+
+ for (int i = 0 ; i < qualifierArity.n - 1 ; i++) {
+ if (typeArgumentKinds[i] == TypeArgumentKind.DIAMOND) {
+ errorExpected = true;
+ break;
+ }
+ }
+
+ if (errorExpected != diagChecker.errorFound) {
+ throw new Error("invalid diagnostics for source:\n" +
+ source.getCharContent(true) +
+ "\nFound error: " + diagChecker.errorFound +
+ "\nExpected error: " + errorExpected);
+ }
+ }
+
+ static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
+
+ boolean errorFound;
+
+ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
+ if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
+ errorFound = true;
+ }
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/diamond/7030687/T7030687.java Tue Mar 29 16:40:07 2011 +0100
@@ -0,0 +1,19 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 7030687
+ * @summary Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
+ * @compile/fail/ref=T7030687.out -XDrawDiagnostics T7030687.java
+ */
+
+class T7030687<X> {
+ class Member { }
+ static class Nested {}
+
+ void test() {
+ class Local {}
+
+ Member m = new Member<>();
+ Nested n = new Nested<>();
+ Local l = new Local<>();
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/diamond/7030687/T7030687.out Tue Mar 29 16:40:07 2011 +0100
@@ -0,0 +1,4 @@
+T7030687.java:15:30: compiler.err.cant.apply.diamond.1: T7030687<X>.Member, (compiler.misc.diamond.non.generic: T7030687<X>.Member)
+T7030687.java:16:30: compiler.err.cant.apply.diamond.1: T7030687.Nested, (compiler.misc.diamond.non.generic: T7030687.Nested)
+T7030687.java:17:28: compiler.err.cant.apply.diamond.1: Local, (compiler.misc.diamond.non.generic: Local)
+3 errors