# HG changeset patch # User mcimadamore # Date 1298032129 0 # Node ID 21758b2bba405aaaecde657886960f4be8df0e82 # Parent be3e5581ea251b2035f71ef509f68f80c19adc5b 7020043: Project Coin: diamond allowed on non-generic type Summary: Diamond oerator should be disallowed on non-generic class types (i.e. String) Reviewed-by: jjg diff -r be3e5581ea25 -r 21758b2bba40 langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java --- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Feb 16 10:27:00 2011 -0800 +++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Fri Feb 18 12:28:49 2011 +0000 @@ -1584,6 +1584,11 @@ if (!TreeInfo.isDiamond(tree)) { clazztype = chk.checkClassType( tree.clazz.pos(), clazztype, true); + } else if (!clazztype.isErroneous() && + !clazztype.tsym.type.isParameterized()) { + log.error(tree.clazz.pos(), + "cant.apply.diamond.1", + clazztype, diags.fragment("diamond.non.generic", clazztype)); } chk.validate(clazz, localEnv); if (tree.encl != null) { @@ -1609,7 +1614,7 @@ List argtypes = attribArgs(tree.args, localEnv); List typeargtypes = attribTypes(tree.typeargs, localEnv); - if (TreeInfo.isDiamond(tree)) { + if (TreeInfo.isDiamond(tree) && clazztype.tsym.type.isParameterized()) { clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes); clazz.type = clazztype; } else if (allowDiamondFinder && diff -r be3e5581ea25 -r 21758b2bba40 langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties --- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Wed Feb 16 10:27:00 2011 -0800 +++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Fri Feb 18 12:28:49 2011 +0000 @@ -1584,6 +1584,10 @@ compiler.misc.diamond=\ {0}<> +# 0: type +compiler.misc.diamond.non.generic=\ + cannot use ''<>'' with non-generic class {0} + # 0: list of type, 1: message segment compiler.misc.diamond.invalid.arg=\ type argument {0} inferred for {1} is not allowed in this context diff -r be3e5581ea25 -r 21758b2bba40 langtools/test/tools/javac/generics/diamond/neg/Neg12.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/generics/diamond/neg/Neg12.java Fri Feb 18 12:28:49 2011 +0000 @@ -0,0 +1,15 @@ +/* + * @test /nodynamiccopyright/ + * @bug 7020043 + * + * @summary Project Coin: diamond allowed on non-generic type + * @author Rémi Forax + * @compile/fail/ref=Neg12.out Neg12.java -XDrawDiagnostics + * + */ + +class DiamondRaw { + public static void main(String[] args) { + String s = new String<>("foo"); + } +} diff -r be3e5581ea25 -r 21758b2bba40 langtools/test/tools/javac/generics/diamond/neg/Neg12.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/generics/diamond/neg/Neg12.out Fri Feb 18 12:28:49 2011 +0000 @@ -0,0 +1,2 @@ +Neg12.java:13:27: compiler.err.cant.apply.diamond.1: java.lang.String, (compiler.misc.diamond.non.generic: java.lang.String) +1 error