Fri, 20 Jun 2008 11:25:03 +0100
6294779: Problem with interface inheritance and covariant return types
Summary: Problematic overriding check when two methods defined in two distinct superinterfaces are overriden by an interface
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java Thu Jun 19 15:52:31 2008 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java Fri Jun 20 11:25:03 2008 +0100 1.3 @@ -1367,13 +1367,47 @@ 1.4 types.isSameType(rt1, rt2) || 1.5 rt1.tag >= CLASS && rt2.tag >= CLASS && 1.6 (types.covariantReturnType(rt1, rt2, Warner.noWarnings) || 1.7 - types.covariantReturnType(rt2, rt1, Warner.noWarnings)); 1.8 + types.covariantReturnType(rt2, rt1, Warner.noWarnings)) || 1.9 + checkCommonOverriderIn(s1,s2,site); 1.10 if (!compat) return s2; 1.11 } 1.12 } 1.13 } 1.14 return null; 1.15 } 1.16 + //WHERE 1.17 + boolean checkCommonOverriderIn(Symbol s1, Symbol s2, Type site) { 1.18 + Map<TypeSymbol,Type> supertypes = new HashMap<TypeSymbol,Type>(); 1.19 + Type st1 = types.memberType(site, s1); 1.20 + Type st2 = types.memberType(site, s2); 1.21 + closure(site, supertypes); 1.22 + for (Type t : supertypes.values()) { 1.23 + for (Scope.Entry e = t.tsym.members().lookup(s1.name); e.scope != null; e = e.next()) { 1.24 + Symbol s3 = e.sym; 1.25 + if (s3 == s1 || s3 == s2 || s3.kind != MTH || (s3.flags() & (BRIDGE|SYNTHETIC)) != 0) continue; 1.26 + Type st3 = types.memberType(site,s3); 1.27 + if (types.overrideEquivalent(st3, st1) && types.overrideEquivalent(st3, st2)) { 1.28 + if (s3.owner == site.tsym) { 1.29 + return true; 1.30 + } 1.31 + List<Type> tvars1 = st1.getTypeArguments(); 1.32 + List<Type> tvars2 = st2.getTypeArguments(); 1.33 + List<Type> tvars3 = st3.getTypeArguments(); 1.34 + Type rt1 = st1.getReturnType(); 1.35 + Type rt2 = st2.getReturnType(); 1.36 + Type rt13 = types.subst(st3.getReturnType(), tvars3, tvars1); 1.37 + Type rt23 = types.subst(st3.getReturnType(), tvars3, tvars2); 1.38 + boolean compat = 1.39 + rt13.tag >= CLASS && rt23.tag >= CLASS && 1.40 + (types.covariantReturnType(rt13, rt1, Warner.noWarnings) && 1.41 + types.covariantReturnType(rt23, rt2, Warner.noWarnings)); 1.42 + if (compat) 1.43 + return true; 1.44 + } 1.45 + } 1.46 + } 1.47 + return false; 1.48 + } 1.49 1.50 /** Check that a given method conforms with any method it overrides. 1.51 * @param tree The tree from which positions are extracted
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/generics/6294779/T6294779a.java Fri Jun 20 11:25:03 2008 +0100 2.3 @@ -0,0 +1,49 @@ 2.4 +/* 2.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 2.24 + * have any questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6294779 2.30 + * @summary Problem with interface inheritance and covariant return types 2.31 + * @author Maurizio Cimadamore 2.32 + * @compile T6294779a.java 2.33 + */ 2.34 + 2.35 +public class T6294779a { 2.36 + 2.37 + interface A { 2.38 + A m(); 2.39 + } 2.40 + 2.41 + interface B extends A { 2.42 + B m(); 2.43 + } 2.44 + 2.45 + interface C extends A { 2.46 + C m(); 2.47 + } 2.48 + 2.49 + interface D extends B, C { 2.50 + D m(); 2.51 + } 2.52 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/generics/6294779/T6294779b.java Fri Jun 20 11:25:03 2008 +0100 3.3 @@ -0,0 +1,49 @@ 3.4 +/* 3.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 3.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 3.24 + * have any questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 6294779 3.30 + * @summary Problem with interface inheritance and covariant return types 3.31 + * @author Maurizio Cimadamore 3.32 + * @compile T6294779b.java 3.33 + */ 3.34 + 3.35 +import java.util.*; 3.36 + 3.37 +class T6294779b { 3.38 + 3.39 + interface I1<E> { 3.40 + List<E> m(); 3.41 + } 3.42 + 3.43 + interface I2<E> { 3.44 + Queue<E> m(); 3.45 + } 3.46 + 3.47 + interface I3<E> { 3.48 + LinkedList<E> m(); 3.49 + } 3.50 + 3.51 + interface I4<E> extends I1<E>, I2<E>, I3<E> {} 3.52 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/generics/6294779/T6294779c.java Fri Jun 20 11:25:03 2008 +0100 4.3 @@ -0,0 +1,53 @@ 4.4 +/* 4.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 4.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 4.24 + * have any questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 6294779 4.30 + * @summary Problem with interface inheritance and covariant return types 4.31 + * @author Maurizio Cimadamore 4.32 + * @compile/fail T6294779c.java 4.33 + */ 4.34 + 4.35 +public class T6294779c<X> { 4.36 + 4.37 + interface A {} 4.38 + 4.39 + interface B {} 4.40 + 4.41 + interface C {} 4.42 + 4.43 + interface I1 { 4.44 + T6294779c<? extends A> get(); 4.45 + } 4.46 + 4.47 + interface I2 { 4.48 + T6294779c<? extends B> get(); 4.49 + } 4.50 + 4.51 + interface I3 { 4.52 + T6294779c<? extends C> get(); 4.53 + } 4.54 + 4.55 + interface I4 extends I1, I2, I3 {} 4.56 +}