# HG changeset patch # User lana # Date 1314892451 25200 # Node ID 4c5514d422c43e23af2437001ae0595d62b6cb19 # Parent c364b317119bc5294942d08aa718b9ddef33ab1c# Parent 9566fd61b11c888b1826781bf7a757ea21950ebf Merge diff -r c364b317119b -r 4c5514d422c4 src/share/classes/com/sun/tools/javac/comp/Attr.java --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Aug 31 15:37:27 2011 -0700 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Sep 01 08:54:11 2011 -0700 @@ -1837,7 +1837,7 @@ try { constructor = rs.resolveDiamond(tree.pos(), localEnv, - clazztype.tsym.type, + clazztype, argtypes, typeargtypes); } finally { @@ -2872,8 +2872,10 @@ if (clazztype.tag == CLASS) { List formals = clazztype.tsym.type.getTypeArguments(); - - if (actuals.length() == formals.length() || actuals.length() == 0) { + if (actuals.isEmpty()) //diamond + actuals = formals; + + if (actuals.length() == formals.length()) { List a = actuals; List f = formals; while (a.nonEmpty()) { diff -r c364b317119b -r 4c5514d422c4 src/share/classes/com/sun/tools/javac/comp/Resolve.java --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Wed Aug 31 15:37:27 2011 -0700 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Thu Sep 01 08:54:11 2011 -0700 @@ -767,16 +767,13 @@ m2.erasure(types).getParameterTypes())) return ambiguityError(m1, m2); // both abstract, neither overridden; merge throws clause and result type - Symbol mostSpecific; - if (types.returnTypeSubstitutable(mt1, mt2)) - mostSpecific = m1; - else if (types.returnTypeSubstitutable(mt2, mt1)) - mostSpecific = m2; - else { + Type mst = mostSpecificReturnType(mt1, mt2); + if (mst == null) { // Theoretically, this can't happen, but it is possible // due to error recovery or mixing incompatible class files return ambiguityError(m1, m2); } + Symbol mostSpecific = mst == mt1 ? m1 : m2; List allThrown = chk.intersect(mt1.getThrownTypes(), mt2.getThrownTypes()); Type newSig = types.createMethodTypeWithThrown(mostSpecific.type, allThrown); MethodSymbol result = new MethodSymbol( @@ -859,6 +856,28 @@ } } //where + Type mostSpecificReturnType(Type mt1, Type mt2) { + Type rt1 = mt1.getReturnType(); + Type rt2 = mt2.getReturnType(); + + if (mt1.tag == FORALL && mt2.tag == FORALL) { + //if both are generic methods, adjust return type ahead of subtyping check + rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments()); + } + //first use subtyping, then return type substitutability + if (types.isSubtype(rt1, rt2)) { + return mt1; + } else if (types.isSubtype(rt2, rt1)) { + return mt2; + } else if (types.returnTypeSubstitutable(mt1, mt2)) { + return mt1; + } else if (types.returnTypeSubstitutable(mt2, mt1)) { + return mt2; + } else { + return null; + } + } + //where Symbol ambiguityError(Symbol m1, Symbol m2) { if (((m1.flags() | m2.flags()) & CLASH) != 0) { return (m1.flags() & CLASH) == 0 ? m1 : m2; diff -r c364b317119b -r 4c5514d422c4 src/share/classes/com/sun/tools/javac/parser/JavacParser.java --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Wed Aug 31 15:37:27 2011 -0700 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Thu Sep 01 08:54:11 2011 -0700 @@ -1381,8 +1381,10 @@ int oldmode = mode; mode = TYPE; boolean diamondFound = false; + int lastTypeargsPos = -1; if (S.token() == LT) { checkGenerics(); + lastTypeargsPos = S.pos(); t = typeArguments(t, true); diamondFound = (mode & DIAMOND) != 0; } @@ -1395,6 +1397,7 @@ S.nextToken(); t = toP(F.at(pos).Select(t, ident())); if (S.token() == LT) { + lastTypeargsPos = S.pos(); checkGenerics(); t = typeArguments(t, true); diamondFound = (mode & DIAMOND) != 0; @@ -1403,7 +1406,11 @@ mode = oldmode; if (S.token() == LBRACKET) { JCExpression e = arrayCreatorRest(newpos, t); - if (typeArgs != null) { + if (diamondFound) { + reportSyntaxError(lastTypeargsPos, "cannot.create.array.with.diamond"); + return toP(F.at(newpos).Erroneous(List.of(e))); + } + else if (typeArgs != null) { int pos = newpos; if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) { // note: this should always happen but we should diff -r c364b317119b -r 4c5514d422c4 src/share/classes/com/sun/tools/javac/resources/compiler.properties --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Wed Aug 31 15:37:27 2011 -0700 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Thu Sep 01 08:54:11 2011 -0700 @@ -462,6 +462,9 @@ compiler.err.cannot.create.array.with.type.arguments=\ cannot create array with type arguments +compiler.err.cannot.create.array.with.diamond=\ + cannot create array with ''<>'' + # # limits. We don't give the limits in the diagnostic because we expect # them to change, yet we want to use the same diagnostic. These are all diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/diags/examples/CannotCreateArrayWithDiamond.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/diags/examples/CannotCreateArrayWithDiamond.java Thu Sep 01 08:54:11 2011 -0700 @@ -0,0 +1,28 @@ +/* + * 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. + */ + +// key: compiler.err.cannot.create.array.with.diamond + +class CannotCreateArrayWithDiamond { + Object[] array = new Object<>[3]; +} diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/generics/diamond/7046778/DiamondAndInnerClassTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/generics/diamond/7046778/DiamondAndInnerClassTest.java Thu Sep 01 08:54:11 2011 -0700 @@ -0,0 +1,336 @@ +/* + * 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 7046778 + * @summary Project Coin: problem with diamond and member inner classes + */ + +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 DiamondAndInnerClassTest { + + static int checkCount = 0; + + enum TypeArgumentKind { + NONE(""), + STRING(""), + INTEGER(""), + DIAMOND("<>"); + + String typeargStr; + + private TypeArgumentKind(String typeargStr) { + this.typeargStr = typeargStr; + } + + boolean compatible(TypeArgumentKind that) { + switch (this) { + case NONE: return true; + case STRING: return that != INTEGER; + case INTEGER: return that != STRING; + default: throw new AssertionError("Unexpected decl kind: " + this); + } + } + + boolean compatible(ArgumentKind that) { + switch (this) { + case NONE: return true; + case STRING: return that == ArgumentKind.STRING; + case INTEGER: return that == ArgumentKind.INTEGER; + default: throw new AssertionError("Unexpected decl kind: " + this); + } + } + } + + enum ArgumentKind { + OBJECT("(Object)null"), + STRING("(String)null"), + INTEGER("(Integer)null"); + + String argStr; + + private ArgumentKind(String argStr) { + this.argStr = argStr; + } + } + + enum TypeQualifierArity { + ONE(1, "A1#TA1"), + TWO(2, "A1#TA1.A2#TA2"), + THREE(3, "A1#TA1.A2#TA2.A3#TA3"); + + 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; + } + + boolean matches(InnerClassDeclArity innerClassDeclArity) { + return n ==innerClassDeclArity.n; + } + } + + enum InnerClassDeclArity { + ONE(1, "class A1 { A1(X x1) { } #B }"), + TWO(2, "class A1 { class A2 { A2(X1 x1, X2 x2) { } #B } }"), + THREE(3, "class A1 { class A2 { class A3 { A3(X1 x1, X2 x2, X3 x3) { } #B } } }"); + + int n; + String classDeclStr; + + private InnerClassDeclArity(int n, String classDeclStr) { + this.n = n; + this.classDeclStr = classDeclStr; + } + } + + enum ArgumentListArity { + ONE(1, "(#A1)"), + TWO(2, "(#A1,#A2)"), + THREE(3, "(#A1,#A2,#A3)"); + + int n; + String argListStr; + + private ArgumentListArity(int n, String argListStr) { + this.n = n; + this.argListStr = argListStr; + } + + String getArgs(ArgumentKind... argumentKinds) { + String res = argListStr; + for (int i = 1 ; i <= argumentKinds.length ; i++) { + res = res.replace("#A" + i, argumentKinds[i-1].argStr); + } + return res; + } + + boolean matches(InnerClassDeclArity innerClassDeclArity) { + return n ==innerClassDeclArity.n; + } + } + + 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 (InnerClassDeclArity innerClassDeclArity : InnerClassDeclArity.values()) { + for (TypeQualifierArity declType : TypeQualifierArity.values()) { + if (!declType.matches(innerClassDeclArity)) continue; + for (TypeQualifierArity newClassType : TypeQualifierArity.values()) { + if (!newClassType.matches(innerClassDeclArity)) continue; + for (ArgumentListArity argList : ArgumentListArity.values()) { + if (!argList.matches(innerClassDeclArity)) continue; + for (TypeArgumentKind taDecl1 : TypeArgumentKind.values()) { + boolean isDeclRaw = taDecl1 == TypeArgumentKind.NONE; + //no diamond on decl site + if (taDecl1 == TypeArgumentKind.DIAMOND) continue; + for (TypeArgumentKind taSite1 : TypeArgumentKind.values()) { + boolean isSiteRaw = taSite1 == TypeArgumentKind.NONE; + //diamond only allowed on the last type qualifier + if (taSite1 == TypeArgumentKind.DIAMOND && + innerClassDeclArity != InnerClassDeclArity.ONE) continue; + for (ArgumentKind arg1 : ArgumentKind.values()) { + if (innerClassDeclArity == innerClassDeclArity.ONE) { + new DiamondAndInnerClassTest(innerClassDeclArity, declType, newClassType, + argList, new TypeArgumentKind[] {taDecl1}, + new TypeArgumentKind[] {taSite1}, new ArgumentKind[] {arg1}).run(comp, fm); + continue; + } + for (TypeArgumentKind taDecl2 : TypeArgumentKind.values()) { + //no rare types + if (isDeclRaw != (taDecl2 == TypeArgumentKind.NONE)) continue; + //no diamond on decl site + if (taDecl2 == TypeArgumentKind.DIAMOND) continue; + for (TypeArgumentKind taSite2 : TypeArgumentKind.values()) { + //no rare types + if (isSiteRaw != (taSite2 == TypeArgumentKind.NONE)) continue; + //diamond only allowed on the last type qualifier + if (taSite2 == TypeArgumentKind.DIAMOND && + innerClassDeclArity != InnerClassDeclArity.TWO) continue; + for (ArgumentKind arg2 : ArgumentKind.values()) { + if (innerClassDeclArity == innerClassDeclArity.TWO) { + new DiamondAndInnerClassTest(innerClassDeclArity, declType, newClassType, + argList, new TypeArgumentKind[] {taDecl1, taDecl2}, + new TypeArgumentKind[] {taSite1, taSite2}, + new ArgumentKind[] {arg1, arg2}).run(comp, fm); + continue; + } + for (TypeArgumentKind taDecl3 : TypeArgumentKind.values()) { + //no rare types + if (isDeclRaw != (taDecl3 == TypeArgumentKind.NONE)) continue; + //no diamond on decl site + if (taDecl3 == TypeArgumentKind.DIAMOND) continue; + for (TypeArgumentKind taSite3 : TypeArgumentKind.values()) { + //no rare types + if (isSiteRaw != (taSite3 == TypeArgumentKind.NONE)) continue; + //diamond only allowed on the last type qualifier + if (taSite3 == TypeArgumentKind.DIAMOND && + innerClassDeclArity != InnerClassDeclArity.THREE) continue; + for (ArgumentKind arg3 : ArgumentKind.values()) { + if (innerClassDeclArity == innerClassDeclArity.THREE) { + new DiamondAndInnerClassTest(innerClassDeclArity, declType, newClassType, + argList, new TypeArgumentKind[] {taDecl1, taDecl2, taDecl3}, + new TypeArgumentKind[] {taSite1, taSite2, taSite3}, + new ArgumentKind[] {arg1, arg2, arg3}).run(comp, fm); + continue; + } + } + } + } + } + } + } + } + } + } + } + } + } + } + System.out.println("Total check executed: " + checkCount); + } + + InnerClassDeclArity innerClassDeclArity; + TypeQualifierArity declType; + TypeQualifierArity siteType; + ArgumentListArity argList; + TypeArgumentKind[] declTypeArgumentKinds; + TypeArgumentKind[] siteTypeArgumentKinds; + ArgumentKind[] argumentKinds; + JavaSource source; + DiagnosticChecker diagChecker; + + DiamondAndInnerClassTest(InnerClassDeclArity innerClassDeclArity, + TypeQualifierArity declType, TypeQualifierArity siteType, ArgumentListArity argList, + TypeArgumentKind[] declTypeArgumentKinds, TypeArgumentKind[] siteTypeArgumentKinds, + ArgumentKind[] argumentKinds) { + this.innerClassDeclArity = innerClassDeclArity; + this.declType = declType; + this.siteType = siteType; + this.argList = argList; + this.declTypeArgumentKinds = declTypeArgumentKinds; + this.siteTypeArgumentKinds = siteTypeArgumentKinds; + this.argumentKinds = argumentKinds; + this.source = new JavaSource(); + this.diagChecker = new DiagnosticChecker(); + } + + class JavaSource extends SimpleJavaFileObject { + + String bodyTemplate = "#D res = new #S#AL;"; + + String source; + + public JavaSource() { + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); + source = innerClassDeclArity.classDeclStr.replace("#B", bodyTemplate) + .replace("#D", declType.getType(declTypeArgumentKinds)) + .replace("#S", siteType.getType(siteTypeArgumentKinds)) + .replace("#AL", argList.getArgs(argumentKinds)); + } + + @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)); + try { + ct.analyze(); + } catch (Throwable ex) { + throw new AssertionError("Error thron when compiling the following code:\n" + source.getCharContent(true)); + } + check(); + } + + void check() { + checkCount++; + + boolean errorExpected = false; + + TypeArgumentKind[] expectedArgKinds = new TypeArgumentKind[innerClassDeclArity.n]; + + for (int i = 0 ; i < innerClassDeclArity.n ; i++) { + if (!declTypeArgumentKinds[i].compatible(siteTypeArgumentKinds[i])) { + errorExpected = true; + break; + } + expectedArgKinds[i] = siteTypeArgumentKinds[i] == TypeArgumentKind.DIAMOND ? + declTypeArgumentKinds[i] : siteTypeArgumentKinds[i]; + } + + if (!errorExpected) { + for (int i = 0 ; i < innerClassDeclArity.n ; i++) { + //System.out.println("check " + expectedArgKinds[i] + " against " + argumentKinds[i]); + if (!expectedArgKinds[i].compatible(argumentKinds[i])) { + 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 { + + boolean errorFound; + + public void report(Diagnostic diagnostic) { + if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { + errorFound = true; + } + } + } +} diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/generics/diamond/7057297/T7057297.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/generics/diamond/7057297/T7057297.java Thu Sep 01 08:54:11 2011 -0700 @@ -0,0 +1,29 @@ +/* + * @test /nodynamiccopyright/ + * @bug 7057297 + * + * @summary Project Coin: diamond erroneously accepts in array initializer expressions + * @compile/fail/ref=T7057297.out T7057297.java -XDrawDiagnostics + * + */ + +class T7205797 { + + class Inner {} + + T7205797[] o1 = new T7205797<>[1]; //error + T7205797[] o2 = new T7205797<>[1][1]; //error + T7205797[] o3 = new T7205797<>[1][1][1]; //error + + T7205797[] o4 = new T7205797<>[] { }; //error + T7205797[] o5 = new T7205797<>[][] { }; //error + T7205797[] o6 = new T7205797<>[][][] { }; //error + + T7205797.Inner[] o1 = new T7205797.Inner<>[1]; //error + T7205797.Inner[] o2 = new T7205797.Inner<>[1][1]; //error + T7205797.Inner[] o3 = new T7205797.Inner<>[1][1][1]; //error + + T7205797.Inner[] o4 = new T7205797.Inner<>[] { }; //error + T7205797.Inner[] o5 = new T7205797.Inner<>[][] { }; //error + T7205797.Inner[] o6 = new T7205797.Inner<>[][][] { }; //error +} diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/generics/diamond/7057297/T7057297.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/generics/diamond/7057297/T7057297.out Thu Sep 01 08:54:11 2011 -0700 @@ -0,0 +1,13 @@ +T7057297.java:14:41: compiler.err.cannot.create.array.with.diamond +T7057297.java:15:41: compiler.err.cannot.create.array.with.diamond +T7057297.java:16:41: compiler.err.cannot.create.array.with.diamond +T7057297.java:18:41: compiler.err.cannot.create.array.with.diamond +T7057297.java:19:41: compiler.err.cannot.create.array.with.diamond +T7057297.java:20:41: compiler.err.cannot.create.array.with.diamond +T7057297.java:22:69: compiler.err.cannot.create.array.with.diamond +T7057297.java:23:69: compiler.err.cannot.create.array.with.diamond +T7057297.java:24:69: compiler.err.cannot.create.array.with.diamond +T7057297.java:26:69: compiler.err.cannot.create.array.with.diamond +T7057297.java:27:69: compiler.err.cannot.create.array.with.diamond +T7057297.java:28:69: compiler.err.cannot.create.array.with.diamond +12 errors diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/generics/diamond/neg/Neg09.out --- a/test/tools/javac/generics/diamond/neg/Neg09.out Wed Aug 31 15:37:27 2011 -0700 +++ b/test/tools/javac/generics/diamond/neg/Neg09.out Thu Sep 01 08:54:11 2011 -0700 @@ -1,5 +1,5 @@ -Neg09.java:17:34: compiler.err.cant.apply.diamond.1: Neg09.Member, (compiler.misc.diamond.and.anon.class: Neg09.Member) -Neg09.java:18:34: compiler.err.cant.apply.diamond.1: Neg09.Nested, (compiler.misc.diamond.and.anon.class: Neg09.Nested) -Neg09.java:22:39: compiler.err.cant.apply.diamond.1: Neg09.Member, (compiler.misc.diamond.and.anon.class: Neg09.Member) -Neg09.java:23:40: compiler.err.cant.apply.diamond.1: Neg09.Nested, (compiler.misc.diamond.and.anon.class: Neg09.Nested) +Neg09.java:17:34: compiler.err.cant.apply.diamond.1: Neg09.Member, (compiler.misc.diamond.and.anon.class: Neg09.Member) +Neg09.java:18:34: compiler.err.cant.apply.diamond.1: Neg09.Nested, (compiler.misc.diamond.and.anon.class: Neg09.Nested) +Neg09.java:22:39: compiler.err.cant.apply.diamond.1: Neg09.Member, (compiler.misc.diamond.and.anon.class: Neg09.Member) +Neg09.java:23:40: compiler.err.cant.apply.diamond.1: Neg09.Nested, (compiler.misc.diamond.and.anon.class: Neg09.Nested) 4 errors diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/generics/rawOverride/7062745/GenericOverrideTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/generics/rawOverride/7062745/GenericOverrideTest.java Thu Sep 01 08:54:11 2011 -0700 @@ -0,0 +1,286 @@ +/* + * 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 7062745 + * @summary Regression: difference in overload resolution when two methods are maximally specific + */ + +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 GenericOverrideTest { + + static int checkCount = 0; + + enum SignatureKind { + NON_GENERIC(""), + GENERIC(""); + + String paramStr; + + private SignatureKind(String paramStr) { + this.paramStr = paramStr; + } + } + + enum ReturnTypeKind { + LIST("List"), + ARRAYLIST("ArrayList"); + + String retStr; + + private ReturnTypeKind(String retStr) { + this.retStr = retStr; + } + + boolean moreSpecificThan(ReturnTypeKind that) { + switch (this) { + case LIST: + return that == this; + case ARRAYLIST: + return that == LIST || that == ARRAYLIST; + default: throw new AssertionError("Unexpected ret kind: " + this); + } + } + } + + enum TypeArgumentKind { + NONE(""), + UNBOUND(""), + INTEGER(""), + NUMBER(""), + TYPEVAR(""); + + String typeargStr; + + private TypeArgumentKind(String typeargStr) { + this.typeargStr = typeargStr; + } + + boolean compatibleWith(SignatureKind sig) { + switch (this) { + case TYPEVAR: return sig != SignatureKind.NON_GENERIC; + default: return true; + } + } + + boolean moreSpecificThan(TypeArgumentKind that, boolean strict) { + switch (this) { + case NONE: + return that == this || !strict; + case UNBOUND: + return that == this || that == NONE; + case INTEGER: + case NUMBER: + case TYPEVAR: + return that == this || that == NONE || that == UNBOUND; + default: throw new AssertionError("Unexpected typearg kind: " + this); + } + } + + boolean assignableTo(TypeArgumentKind that, SignatureKind sig) { + switch (this) { + case NONE: + //this case needs to workaround to javac's impl of 15.12.2.8 being too strict + //ideally should be just 'return true' (see 7067746) + return sig == SignatureKind.NON_GENERIC || that == NONE; + case UNBOUND: + return that == this || that == NONE; + case INTEGER: + case NUMBER: + return that == this || that == NONE || that == UNBOUND; + case TYPEVAR: + return true; + default: throw new AssertionError("Unexpected typearg kind: " + this); + } + } + } + + 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 (SignatureKind sig1 : SignatureKind.values()) { + for (ReturnTypeKind rt1 : ReturnTypeKind.values()) { + for (TypeArgumentKind ta1 : TypeArgumentKind.values()) { + if (!ta1.compatibleWith(sig1)) continue; + for (SignatureKind sig2 : SignatureKind.values()) { + for (ReturnTypeKind rt2 : ReturnTypeKind.values()) { + for (TypeArgumentKind ta2 : TypeArgumentKind.values()) { + if (!ta2.compatibleWith(sig2)) continue; + for (ReturnTypeKind rt3 : ReturnTypeKind.values()) { + for (TypeArgumentKind ta3 : TypeArgumentKind.values()) { + if (!ta3.compatibleWith(SignatureKind.NON_GENERIC)) continue; + new GenericOverrideTest(sig1, rt1, ta1, sig2, rt2, ta2, rt3, ta3).run(comp, fm); + } + } + } + } + } + } + } + } + System.out.println("Total check executed: " + checkCount); + } + + SignatureKind sig1, sig2; + ReturnTypeKind rt1, rt2, rt3; + TypeArgumentKind ta1, ta2, ta3; + JavaSource source; + DiagnosticChecker diagChecker; + + GenericOverrideTest(SignatureKind sig1, ReturnTypeKind rt1, TypeArgumentKind ta1, + SignatureKind sig2, ReturnTypeKind rt2, TypeArgumentKind ta2, ReturnTypeKind rt3, TypeArgumentKind ta3) { + this.sig1 = sig1; + this.sig2 = sig2; + this.rt1 = rt1; + this.rt2 = rt2; + this.rt3 = rt3; + this.ta1 = ta1; + this.ta2 = ta2; + this.ta3 = ta3; + this.source = new JavaSource(); + this.diagChecker = new DiagnosticChecker(); + } + + class JavaSource extends SimpleJavaFileObject { + + String template = "import java.util.*;\n" + + "interface A { #S1 #R1#TA1 m(); }\n" + + "interface B { #S2 #R2#TA2 m(); }\n" + + "interface AB extends A, B {}\n" + + "class Test {\n" + + " void test(AB ab) { #R3#TA3 n = ab.m(); }\n" + + "}"; + + String source; + + public JavaSource() { + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); + source = template.replace("#S1", sig1.paramStr). + replace("#S2", sig2.paramStr). + replace("#R1", rt1.retStr). + replace("#R2", rt2.retStr). + replace("#R3", rt3.retStr). + replace("#TA1", ta1.typeargStr). + replace("#TA2", ta2.typeargStr). + replace("#TA3", ta3.typeargStr); + } + + @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)); + try { + ct.analyze(); + } catch (Throwable ex) { + throw new AssertionError("Error thron when compiling the following code:\n" + source.getCharContent(true)); + } + check(); + } + + void check() { + checkCount++; + + boolean errorExpected = false; + int mostSpecific = 0; + + //first check that either |R1| <: |R2| or |R2| <: |R1| + if (rt1 != rt2) { + if (!rt1.moreSpecificThan(rt2) && + !rt2.moreSpecificThan(rt1)) { + errorExpected = true; + } else { + mostSpecific = rt1.moreSpecificThan(rt2) ? 1 : 2; + } + } + + //check that either TA1 <= TA2 or TA2 <= TA1 (unless most specific return found above is raw) + if (!errorExpected) { + if (ta1 != ta2) { + boolean useStrictCheck = ta1.moreSpecificThan(ta2, true) || ta2.moreSpecificThan(ta1, true); + if (!ta1.moreSpecificThan(ta2, useStrictCheck) && + !ta2.moreSpecificThan(ta1, useStrictCheck)) { + errorExpected = true; + } else { + int mostSpecific2 = ta1.moreSpecificThan(ta2, useStrictCheck) ? 1 : 2; + if (mostSpecific != 0 && mostSpecific2 != mostSpecific) { + errorExpected = mostSpecific == 1 ? ta1 != TypeArgumentKind.NONE : ta2 != TypeArgumentKind.NONE; + } else { + mostSpecific = mostSpecific2; + } + } + } else if (mostSpecific == 0) { + //when no signature is better than the other, an arbitrary choice + //must be made - javac always picks the second signature + mostSpecific = 2; + } + } + + //finally, check that most specific return type is compatible with expected type + if (!errorExpected) { + ReturnTypeKind msrt = mostSpecific == 1 ? rt1 : rt2; + TypeArgumentKind msta = mostSpecific == 1 ? ta1 : ta2; + SignatureKind mssig = mostSpecific == 1 ? sig1 : sig2; + + if (!msrt.moreSpecificThan(rt3) || + !msta.assignableTo(ta3, mssig)) { + errorExpected = true; + } + } + + 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 { + + boolean errorFound; + + public void report(Diagnostic diagnostic) { + if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { + errorFound = true; + } + } + } +} diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/generics/rawOverride/7062745/T7062745neg.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/generics/rawOverride/7062745/T7062745neg.java Thu Sep 01 08:54:11 2011 -0700 @@ -0,0 +1,18 @@ +/* + * @test /nodynamiccopyright/ + * @bug 7062745 + * @summary Regression: difference in overload resolution when two methods are maximally specific + * @compile/fail/ref=T7062745neg.out -XDrawDiagnostics T7062745neg.java + */ + +import java.util.*; + +class T7062745neg { + interface A { List getList(); } + interface B { ArrayList getList(); } + interface AB extends A, B {} + + void test(AB ab) { + Number n = ab.getList().get(1); + } +} diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/generics/rawOverride/7062745/T7062745neg.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/generics/rawOverride/7062745/T7062745neg.out Thu Sep 01 08:54:11 2011 -0700 @@ -0,0 +1,2 @@ +T7062745neg.java:16:36: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.Object, java.lang.Number +1 error diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/generics/rawOverride/7062745/T7062745pos.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/generics/rawOverride/7062745/T7062745pos.java Thu Sep 01 08:54:11 2011 -0700 @@ -0,0 +1,42 @@ +/* + * 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 7062745 + * @summary Regression: difference in overload resolution when two methods are maximally specific + * + * @compile T7062745pos.java + */ + +import java.util.*; + +class T7062745pos { + interface A { List getList(); } + interface B { List getList(); } + interface AB extends A, B {} + + void test(AB ab) { + Number n = ab.getList().get(1); + } +} diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/lib/CompileFail.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/lib/CompileFail.java Thu Sep 01 08:54:11 2011 -0700 @@ -0,0 +1,89 @@ +/* + * 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. + */ + +import java.io.*; +import java.util.*; + +/* + * Utility class to emulate jtreg @compile/fail, but also checking the specific + * exit code, given as the first arg. + */ +public class CompileFail { + public static void main(String... args) { + if (args.length < 2) + throw new IllegalArgumentException("insufficient args"); + int expected_rc = getReturnCode(args[0]); + + List javacArgs = new ArrayList<>(); + javacArgs.addAll(Arrays.asList( + "-bootclasspath", System.getProperty("sun.boot.class.path"), + "-d", "." + )); + + File testSrc = new File(System.getProperty("test.src")); + for (int i = 1; i < args.length; i++) { + String arg = args[i]; + if (arg.endsWith(".java")) + javacArgs.add(new File(testSrc, arg).getPath()); + else + javacArgs.add(arg); + } + + int rc = com.sun.tools.javac.Main.compile( + javacArgs.toArray(new String[javacArgs.size()])); + + if (rc != expected_rc) + throw new Error("unexpected exit code: " + rc + + ", expected: " + expected_rc); + } + + static int getReturnCode(String name) { + switch (name) { + case "OK": + return EXIT_OK; + + case "ERROR": + return EXIT_ERROR; + + case "CMDERR": + return EXIT_CMDERR; + + case "SYSERR": + return EXIT_SYSERR; + + case "ABNORMAL": + return EXIT_ABNORMAL; + + default: + throw new IllegalArgumentException(name); + } + } + + // The following is cut-n-paste from com.sun.tools.javac.main.Main + static final int + EXIT_OK = 0, // Compilation completed with no errors. + EXIT_ERROR = 1, // Completed but reported errors. + EXIT_CMDERR = 2, // Bad command-line arguments + EXIT_SYSERR = 3, // System error or resource exhaustion. + EXIT_ABNORMAL = 4; // Compiler terminated abnormally +} diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/processing/errors/TestOptionSyntaxErrors.java --- a/test/tools/javac/processing/errors/TestOptionSyntaxErrors.java Wed Aug 31 15:37:27 2011 -0700 +++ b/test/tools/javac/processing/errors/TestOptionSyntaxErrors.java Thu Sep 01 08:54:11 2011 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 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 @@ -27,14 +27,14 @@ * @summary Test that annotation processor options with illegal syntax are rejected * @author Joseph D. Darcy * @library ../../lib - * @build JavacTestingAbstractProcessor + * @build JavacTestingAbstractProcessor CompileFail * @compile TestOptionSyntaxErrors.java - * @compile/fail -A TestOptionSyntaxErrors.java - * @compile/fail -A8adOption TestOptionSyntaxErrors.java - * @compile/fail -A8adOption=1worseOption TestOptionSyntaxErrors.java - * @compile/fail -processor TestOptionSyntaxErrors -proc:only -A TestOptionSyntaxErrors.java - * @compile/fail -processor TestOptionSyntaxErrors -proc:only -A8adOption TestOptionSyntaxErrors.java - * @compile/fail -processor TestOptionSyntaxErrors -proc:only -A8adOption=1worseOption TestOptionSyntaxErrors.java + * @run main CompileFail CMDERR -A TestOptionSyntaxErrors.java + * @run main CompileFail CMDERR -A8adOption TestOptionSyntaxErrors.java + * @run main CompileFail CMDERR -A8adOption=1worseOption TestOptionSyntaxErrors.java + * @run main CompileFail CMDERR -processor TestOptionSyntaxErrors -proc:only -A TestOptionSyntaxErrors.java + * @run main CompileFail CMDERR -processor TestOptionSyntaxErrors -proc:only -A8adOption TestOptionSyntaxErrors.java + * @run main CompileFail CMDERR -processor TestOptionSyntaxErrors -proc:only -A8adOption=1worseOption TestOptionSyntaxErrors.java */ import java.util.Set; diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/processing/errors/TestReturnCode.java --- a/test/tools/javac/processing/errors/TestReturnCode.java Wed Aug 31 15:37:27 2011 -0700 +++ b/test/tools/javac/processing/errors/TestReturnCode.java Thu Sep 01 08:54:11 2011 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 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 @@ -27,25 +27,25 @@ * @summary Test that an erroneous return code results from raising an error. * @author Joseph D. Darcy * @library ../../lib - * @build JavacTestingAbstractProcessor + * @build JavacTestingAbstractProcessor CompileFail * @compile TestReturnCode.java * - * @compile -processor TestReturnCode -proc:only Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AErrorOnFirst Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AErrorOnLast Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AErrorOnLast -AErrorOnFirst Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnFirst Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnFirst -AErrorOnFirst Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnFirst -AErrorOnLast Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnFirst -AErrorOnLast -AErrorOnFirst Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnLast Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnLast -AErrorOnFirst Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnLast -AErrorOnLast Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnLast -AErrorOnLast -AErrorOnFirst Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst -AErrorOnFirst Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst -AErrorOnLast Foo.java - * @compile/fail -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst -AErrorOnLast -AErrorOnFirst Foo.java + * @compile -processor TestReturnCode -proc:only Foo.java + * @run main CompileFail ERROR -processor TestReturnCode -proc:only -AErrorOnFirst Foo.java + * @run main CompileFail ERROR -processor TestReturnCode -proc:only -AErrorOnLast Foo.java + * @run main CompileFail ERROR -processor TestReturnCode -proc:only -AErrorOnLast -AErrorOnFirst Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnFirst Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnFirst -AErrorOnFirst Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnFirst -AErrorOnLast Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnFirst -AErrorOnLast -AErrorOnFirst Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AErrorOnFirst Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AErrorOnLast Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AErrorOnLast -AErrorOnFirst Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst -AErrorOnFirst Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst -AErrorOnLast Foo.java + * @run main CompileFail SYSERR -processor TestReturnCode -proc:only -AExceptionOnLast -AExceptionOnFirst -AErrorOnLast -AErrorOnFirst Foo.java */ import java.util.Set; diff -r c364b317119b -r 4c5514d422c4 test/tools/javac/warnings/Serial.java --- a/test/tools/javac/warnings/Serial.java Wed Aug 31 15:37:27 2011 -0700 +++ b/test/tools/javac/warnings/Serial.java Thu Sep 01 08:54:11 2011 -0700 @@ -29,7 +29,6 @@ * @compile -Xlint:all Serial.java * @compile -Werror Serial.java * @compile/fail -Werror -Xlint:serial Serial.java - * @compile/fail -Werror -Xlint:all,-path T4994049/ Serial.java */ import java.io.Serializable;