Thu, 03 Mar 2011 17:34:58 +0000
7022054: Invalid compiler error on covariant overriding methods with the same erasure
Summary: Rules for method clash use notion of subsignature, which is sometimes too strict and incompatible with JDK 6
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Thu Mar 03 17:32:35 2011 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Thu Mar 03 17:34:58 2011 +0000 1.3 @@ -1992,7 +1992,11 @@ 1.4 * @return true if t is a sub signature of s. 1.5 */ 1.6 public boolean isSubSignature(Type t, Type s) { 1.7 - return hasSameArgs(t, s) || hasSameArgs(t, erasure(s)); 1.8 + return isSubSignature(t, s, true); 1.9 + } 1.10 + 1.11 + public boolean isSubSignature(Type t, Type s, boolean strict) { 1.12 + return hasSameArgs(t, s, strict) || hasSameArgs(t, erasure(s), strict); 1.13 } 1.14 1.15 /** 1.16 @@ -2129,10 +2133,24 @@ 1.17 * where correspondence is by position in the type parameter list. 1.18 */ 1.19 public boolean hasSameArgs(Type t, Type s) { 1.20 + return hasSameArgs(t, s, true); 1.21 + } 1.22 + 1.23 + public boolean hasSameArgs(Type t, Type s, boolean strict) { 1.24 + return hasSameArgs(t, s, strict ? hasSameArgs_strict : hasSameArgs_nonstrict); 1.25 + } 1.26 + 1.27 + private boolean hasSameArgs(Type t, Type s, TypeRelation hasSameArgs) { 1.28 return hasSameArgs.visit(t, s); 1.29 } 1.30 // where 1.31 - private TypeRelation hasSameArgs = new TypeRelation() { 1.32 + private class HasSameArgs extends TypeRelation { 1.33 + 1.34 + boolean strict; 1.35 + 1.36 + public HasSameArgs(boolean strict) { 1.37 + this.strict = strict; 1.38 + } 1.39 1.40 public Boolean visitType(Type t, Type s) { 1.41 throw new AssertionError(); 1.42 @@ -2147,7 +2165,7 @@ 1.43 @Override 1.44 public Boolean visitForAll(ForAll t, Type s) { 1.45 if (s.tag != FORALL) 1.46 - return false; 1.47 + return strict ? false : visitMethodType(t.asMethodType(), s); 1.48 1.49 ForAll forAll = (ForAll)s; 1.50 return hasSameBounds(t, forAll) 1.51 @@ -2159,6 +2177,10 @@ 1.52 return false; 1.53 } 1.54 }; 1.55 + 1.56 + TypeRelation hasSameArgs_strict = new HasSameArgs(true); 1.57 + TypeRelation hasSameArgs_nonstrict = new HasSameArgs(false); 1.58 + 1.59 // </editor-fold> 1.60 1.61 // <editor-fold defaultstate="collapsed" desc="subst">
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java Thu Mar 03 17:32:35 2011 +0000 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java Thu Mar 03 17:34:58 2011 +0000 2.3 @@ -2114,7 +2114,7 @@ 2.4 if (s1 == s2 || !sym.overrides(s2, site.tsym, types, false)) continue; 2.5 //if (i) the signature of 'sym' is not a subsignature of m1 (seen as 2.6 //a member of 'site') and (ii) m1 has the same erasure as m2, issue an error 2.7 - if (!types.isSubSignature(sym.type, types.memberType(site, s1)) && 2.8 + if (!types.isSubSignature(sym.type, types.memberType(site, s1), false) && 2.9 types.hasSameArgs(s1.erasure(types), s2.erasure(types))) { 2.10 sym.flags_field |= CLASH; 2.11 String key = s2 == sym ? 2.12 @@ -2146,7 +2146,7 @@ 2.13 for (Symbol s : types.membersClosure(site).getElementsByName(sym.name, cf)) { 2.14 //if (i) the signature of 'sym' is not a subsignature of m1 (seen as 2.15 //a member of 'site') and (ii) 'sym' has the same erasure as m1, issue an error 2.16 - if (!types.isSubSignature(sym.type, types.memberType(site, s)) && 2.17 + if (!types.isSubSignature(sym.type, types.memberType(site, s), false) && 2.18 types.hasSameArgs(s.erasure(types), sym.erasure(types))) { 2.19 log.error(pos, 2.20 "name.clash.same.erasure.no.hide", 2.21 @@ -2667,7 +2667,7 @@ 2.22 if ((sym.flags() & VARARGS) != (e.sym.flags() & VARARGS)) { 2.23 varargsDuplicateError(pos, sym, e.sym); 2.24 return true; 2.25 - } else if (sym.kind == MTH && !hasSameSignature(sym.type, e.sym.type)) { 2.26 + } else if (sym.kind == MTH && !types.hasSameArgs(sym.type, e.sym.type, false)) { 2.27 duplicateErasureError(pos, sym, e.sym); 2.28 sym.flags_field |= CLASH; 2.29 return true; 2.30 @@ -2679,15 +2679,6 @@ 2.31 } 2.32 return true; 2.33 } 2.34 - //where 2.35 - boolean hasSameSignature(Type mt1, Type mt2) { 2.36 - if (mt1.tag == FORALL && mt2.tag == FORALL) { 2.37 - ForAll fa1 = (ForAll)mt1; 2.38 - ForAll fa2 = (ForAll)mt2; 2.39 - mt2 = types.subst(fa2, fa2.tvars, fa1.tvars); 2.40 - } 2.41 - return types.hasSameArgs(mt1.asMethodType(), mt2.asMethodType()); 2.42 - } 2.43 2.44 /** Report duplicate declaration error. 2.45 */
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/generics/7022054/T7022054neg1.java Thu Mar 03 17:34:58 2011 +0000 3.3 @@ -0,0 +1,17 @@ 3.4 +/* 3.5 + * @test /nodynamiccopyright/ 3.6 + * @bug 7022054 3.7 + * 3.8 + * @summary Invalid compiler error on covariant overriding methods with the same erasure 3.9 + * @compile/fail/ref=T7022054neg1.out -XDrawDiagnostics T7022054neg1.java 3.10 + * 3.11 + */ 3.12 + 3.13 +class T7022054neg1 { 3.14 + static class A { 3.15 + A m(String s) { return null; } 3.16 + } 3.17 + static class B extends A { 3.18 + <X extends String> A m(X s) { return null; } 3.19 + } 3.20 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/generics/7022054/T7022054neg1.out Thu Mar 03 17:34:58 2011 +0000 4.3 @@ -0,0 +1,2 @@ 4.4 +T7022054neg1.java:15:30: compiler.err.name.clash.same.erasure.no.override: <X>m(X), T7022054neg1.B, m(java.lang.String), T7022054neg1.A, <X>m(X), T7022054neg1.B 4.5 +1 error
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/generics/7022054/T7022054neg2.java Thu Mar 03 17:34:58 2011 +0000 5.3 @@ -0,0 +1,17 @@ 5.4 +/* 5.5 + * @test /nodynamiccopyright/ 5.6 + * @bug 7022054 5.7 + * 5.8 + * @summary Invalid compiler error on covariant overriding methods with the same erasure 5.9 + * @compile/fail/ref=T7022054neg2.out -XDrawDiagnostics T7022054neg2.java 5.10 + * 5.11 + */ 5.12 + 5.13 +class T7022054neg2 { 5.14 + static class A { 5.15 + static A m(String s) { return null; } 5.16 + } 5.17 + static class B extends A { 5.18 + static <X extends String> A m(X s) { return null; } 5.19 + } 5.20 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/generics/7022054/T7022054neg2.out Thu Mar 03 17:34:58 2011 +0000 6.3 @@ -0,0 +1,2 @@ 6.4 +T7022054neg2.java:15:37: compiler.err.name.clash.same.erasure.no.hide: <X>m(X), T7022054neg2.B, m(java.lang.String), T7022054neg2.A 6.5 +1 error
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/generics/7022054/T7022054pos1.java Thu Mar 03 17:34:58 2011 +0000 7.3 @@ -0,0 +1,40 @@ 7.4 +/* 7.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. 7.11 + * 7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.15 + * version 2 for more details (a copy is included in the LICENSE file that 7.16 + * accompanied this code). 7.17 + * 7.18 + * You should have received a copy of the GNU General Public License version 7.19 + * 2 along with this work; if not, write to the Free Software Foundation, 7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.21 + * 7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.23 + * or visit www.oracle.com if you need additional information or have any 7.24 + * questions. 7.25 + */ 7.26 + 7.27 +/* 7.28 + * @test 7.29 + * @bug 7022054 7.30 + * 7.31 + * @summary Invalid compiler error on covariant overriding methods with the same erasure 7.32 + * @compile T7022054pos1.java 7.33 + * 7.34 + */ 7.35 + 7.36 +class T7022054pos1 { 7.37 + static class A { 7.38 + A m(String s) { return null; } 7.39 + } 7.40 + static class B extends A { 7.41 + <X extends B> X m(String s) { return null; } 7.42 + } 7.43 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/generics/7022054/T7022054pos2.java Thu Mar 03 17:34:58 2011 +0000 8.3 @@ -0,0 +1,40 @@ 8.4 +/* 8.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. 8.11 + * 8.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.15 + * version 2 for more details (a copy is included in the LICENSE file that 8.16 + * accompanied this code). 8.17 + * 8.18 + * You should have received a copy of the GNU General Public License version 8.19 + * 2 along with this work; if not, write to the Free Software Foundation, 8.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.21 + * 8.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.23 + * or visit www.oracle.com if you need additional information or have any 8.24 + * questions. 8.25 + */ 8.26 + 8.27 +/* 8.28 + * @test 8.29 + * @bug 7022054 8.30 + * 8.31 + * @summary Invalid compiler error on covariant overriding methods with the same erasure 8.32 + * @compile T7022054pos2.java 8.33 + * 8.34 + */ 8.35 + 8.36 +class T7022054pos2 { 8.37 + static class A { 8.38 + static A m(String s) { return null; } 8.39 + } 8.40 + static class B extends A { 8.41 + static <X extends B> X m(String s) { return null; } 8.42 + } 8.43 +}