Wed, 11 Jan 2012 18:23:24 +0000
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
Summary: Problems with Types.rewriteQuantifiers not preserving variance
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Tue Jan 03 17:18:10 2012 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Wed Jan 11 18:23:24 2012 +0000 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -3600,39 +3600,44 @@ 1.11 1.12 @Override 1.13 public Type visitCapturedType(CapturedType t, Void s) { 1.14 - Type bound = visitWildcardType(t.wildcard, null); 1.15 - return (bound.contains(t)) ? 1.16 - erasure(bound) : 1.17 - bound; 1.18 + Type w_bound = t.wildcard.type; 1.19 + Type bound = w_bound.contains(t) ? 1.20 + erasure(w_bound) : 1.21 + visit(w_bound); 1.22 + return rewriteAsWildcardType(visit(bound), t.wildcard.bound, t.wildcard.kind); 1.23 } 1.24 1.25 @Override 1.26 public Type visitTypeVar(TypeVar t, Void s) { 1.27 if (rewriteTypeVars) { 1.28 - Type bound = high ? 1.29 - (t.bound.contains(t) ? 1.30 + Type bound = t.bound.contains(t) ? 1.31 erasure(t.bound) : 1.32 - visit(t.bound)) : 1.33 - syms.botType; 1.34 - return rewriteAsWildcardType(bound, t); 1.35 + visit(t.bound); 1.36 + return rewriteAsWildcardType(bound, t, EXTENDS); 1.37 + } else { 1.38 + return t; 1.39 } 1.40 - else 1.41 - return t; 1.42 } 1.43 1.44 @Override 1.45 public Type visitWildcardType(WildcardType t, Void s) { 1.46 - Type bound = high ? t.getExtendsBound() : 1.47 - t.getSuperBound(); 1.48 - if (bound == null) 1.49 - bound = high ? syms.objectType : syms.botType; 1.50 - return rewriteAsWildcardType(visit(bound), t.bound); 1.51 + Type bound2 = visit(t.type); 1.52 + return t.type == bound2 ? t : rewriteAsWildcardType(bound2, t.bound, t.kind); 1.53 } 1.54 1.55 - private Type rewriteAsWildcardType(Type bound, TypeVar formal) { 1.56 - return high ? 1.57 - makeExtendsWildcard(B(bound), formal) : 1.58 - makeSuperWildcard(B(bound), formal); 1.59 + private Type rewriteAsWildcardType(Type bound, TypeVar formal, BoundKind bk) { 1.60 + switch (bk) { 1.61 + case EXTENDS: return high ? 1.62 + makeExtendsWildcard(B(bound), formal) : 1.63 + makeExtendsWildcard(syms.objectType, formal); 1.64 + case SUPER: return high ? 1.65 + makeSuperWildcard(syms.botType, formal) : 1.66 + makeSuperWildcard(B(bound), formal); 1.67 + case UNBOUND: return makeExtendsWildcard(syms.objectType, formal); 1.68 + default: 1.69 + Assert.error("Invalid bound kind " + bk); 1.70 + return null; 1.71 + } 1.72 } 1.73 1.74 Type B(Type t) {
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/cast/7126754/T7126754.java Wed Jan 11 18:23:24 2012 +0000 2.3 @@ -0,0 +1,14 @@ 2.4 +/* 2.5 + * @test /nodynamiccopyright/ 2.6 + * @author mcimadamore 2.7 + * @bug 7005671 2.8 + * @summary Generics compilation failure casting List<? extends Set...> to List<Set...> 2.9 + * @compile/fail/ref=T7126754.out -Xlint:unchecked -Werror -XDrawDiagnostics T7126754.java 2.10 + */ 2.11 + 2.12 +import java.util.List; 2.13 + 2.14 +class T7126754 { 2.15 + List<? extends List<? extends String>> c = null; 2.16 + List<List<? extends String>> d = (List<List<? extends String>>)c; 2.17 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/cast/7126754/T7126754.out Wed Jan 11 18:23:24 2012 +0000 3.3 @@ -0,0 +1,4 @@ 3.4 +T7126754.java:13:68: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.util.List<compiler.misc.type.captureof: 1, ? extends java.util.List<? extends java.lang.String>>, java.util.List<java.util.List<? extends java.lang.String>> 3.5 +- compiler.err.warnings.and.werror 3.6 +1 error 3.7 +1 warning