Tue, 20 Jan 2009 17:49:49 +0000
6557199: Fails to reject bad override of generic method
Summary: Javac does not correctly implement JLS3 8.4.5
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Tue Jan 20 17:49:09 2009 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Tue Jan 20 17:49:49 2009 +0000 1.3 @@ -2933,32 +2933,16 @@ 1.4 * Language Specification, Third Ed. (8.4.5)</a> 1.5 */ 1.6 public boolean returnTypeSubstitutable(Type r1, Type r2) { 1.7 + return returnTypeSubstitutable(r1, r2, Warner.noWarnings); 1.8 + } 1.9 + //where 1.10 + public boolean returnTypeSubstitutable(Type r1, Type r2, Warner warner) { 1.11 if (hasSameArgs(r1, r2)) 1.12 - return resultSubtype(r1, r2, Warner.noWarnings); 1.13 + return resultSubtype(r1, r2, warner); 1.14 else 1.15 return covariantReturnType(r1.getReturnType(), 1.16 - erasure(r2.getReturnType()), 1.17 - Warner.noWarnings); 1.18 - } 1.19 - 1.20 - public boolean returnTypeSubstitutable(Type r1, 1.21 - Type r2, Type r2res, 1.22 - Warner warner) { 1.23 - if (isSameType(r1.getReturnType(), r2res)) 1.24 - return true; 1.25 - if (r1.getReturnType().isPrimitive() || r2res.isPrimitive()) 1.26 - return false; 1.27 - 1.28 - if (hasSameArgs(r1, r2)) 1.29 - return covariantReturnType(r1.getReturnType(), r2res, warner); 1.30 - if (!source.allowCovariantReturns()) 1.31 - return false; 1.32 - if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner)) 1.33 - return true; 1.34 - if (!isSubtype(r1.getReturnType(), erasure(r2res))) 1.35 - return false; 1.36 - warner.warnUnchecked(); 1.37 - return true; 1.38 + r2.getReturnType(), 1.39 + warner); 1.40 } 1.41 1.42 /** 1.43 @@ -2966,12 +2950,24 @@ 1.44 * method that returns s? 1.45 */ 1.46 public boolean covariantReturnType(Type t, Type s, Warner warner) { 1.47 - return 1.48 - isSameType(t, s) || 1.49 - source.allowCovariantReturns() && 1.50 + //are return types identical? 1.51 + if (isSameType(t, s)) 1.52 + return true; 1.53 + //if t and s are both reference types... 1.54 + else if(source.allowCovariantReturns() && 1.55 !t.isPrimitive() && 1.56 - !s.isPrimitive() && 1.57 - isAssignable(t, s, warner); 1.58 + !s.isPrimitive()) { 1.59 + //check that t is some unchecked subtype of s 1.60 + if (isSubtypeUnchecked(t, s, warner)) 1.61 + return true; 1.62 + //otherwise check that t = |s| 1.63 + else if (isSameType(t, erasure(s))) { 1.64 + warner.warnUnchecked(); 1.65 + return true; 1.66 + } 1.67 + } 1.68 + //otherwise t is not return type substitutable for s 1.69 + return false; 1.70 } 1.71 // </editor-fold> 1.72
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Jan 20 17:49:09 2009 +0000 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Jan 20 17:49:49 2009 +0000 2.3 @@ -1163,7 +1163,7 @@ 2.4 2.5 overrideWarner.warned = false; 2.6 boolean resultTypesOK = 2.7 - types.returnTypeSubstitutable(mt, ot, otres, overrideWarner); 2.8 + types.covariantReturnType(mtres, otres, overrideWarner); 2.9 if (!resultTypesOK) { 2.10 if (!source.allowCovariantReturns() && 2.11 m.owner != origin &&
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/generics/rawOverride/6557199/T6557199.java Tue Jan 20 17:49:49 2009 +0000 3.3 @@ -0,0 +1,44 @@ 3.4 +/* 3.5 + * Copyright 2009 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 6557199 3.30 + * @summary Fails to reject bad override of generic method 3.31 + * @author Maurizio Cimadamore 3.32 + * @compile/fail/ref=T6557199.out T6557199.java -XDrawDiagnostics 3.33 + */ 3.34 + 3.35 +class T6557199 { 3.36 + static class X<S> { 3.37 + public static <U> X<U> test() { 3.38 + return null; 3.39 + } 3.40 + } 3.41 + 3.42 + static class B extends X<B> { 3.43 + public static B test() { 3.44 + return null; 3.45 + } 3.46 + } 3.47 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/generics/rawOverride/6557199/T6557199.out Tue Jan 20 17:49:49 2009 +0000 4.3 @@ -0,0 +1,2 @@ 4.4 +T6557199.java:40:25: compiler.err.prob.found.req: (- compiler.misc.override.incompatible.ret: (- compiler.misc.cant.override: test(), T6557199.B, <U>test(), T6557199.X)), T6557199.B, T6557199.X<U> 4.5 +1 error