提交 3dfecceb 编写于 作者: M mcimadamore

6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly

Summary: Javac doesn't conform to JLS 4.8 - all the supertypes of a raw type must be erased
Reviewed-by: jjg
上级 0957126b
...@@ -640,6 +640,10 @@ public class Type implements PrimitiveType { ...@@ -640,6 +640,10 @@ public class Type implements PrimitiveType {
return typarams_field; return typarams_field;
} }
public boolean hasErasedSupertypes() {
return isRaw();
}
public Type getEnclosingType() { public Type getEnclosingType() {
return outer_field; return outer_field;
} }
...@@ -711,6 +715,17 @@ public class Type implements PrimitiveType { ...@@ -711,6 +715,17 @@ public class Type implements PrimitiveType {
} }
} }
public static class ErasedClassType extends ClassType {
public ErasedClassType(Type outer, TypeSymbol tsym) {
super(outer, List.<Type>nil(), tsym);
}
@Override
public boolean hasErasedSupertypes() {
return true;
}
}
public static class ArrayType extends Type public static class ArrayType extends Type
implements javax.lang.model.type.ArrayType { implements javax.lang.model.type.ArrayType {
......
...@@ -1499,47 +1499,68 @@ public class Types { ...@@ -1499,47 +1499,68 @@ public class Types {
* type parameters in t are deleted. * type parameters in t are deleted.
*/ */
public Type erasure(Type t) { public Type erasure(Type t) {
return erasure(t, false);
}
//where
private Type erasure(Type t, boolean recurse) {
if (t.tag <= lastBaseTag) if (t.tag <= lastBaseTag)
return t; /* fast special case */ return t; /* fast special case */
else else
return erasure.visit(t); return erasure.visit(t, recurse);
} }
// where // where
private UnaryVisitor<Type> erasure = new UnaryVisitor<Type>() { private SimpleVisitor<Type, Boolean> erasure = new SimpleVisitor<Type, Boolean>() {
public Type visitType(Type t, Void ignored) { public Type visitType(Type t, Boolean recurse) {
if (t.tag <= lastBaseTag) if (t.tag <= lastBaseTag)
return t; /*fast special case*/ return t; /*fast special case*/
else else
return t.map(erasureFun); return t.map(recurse ? erasureRecFun : erasureFun);
} }
@Override @Override
public Type visitWildcardType(WildcardType t, Void ignored) { public Type visitWildcardType(WildcardType t, Boolean recurse) {
return erasure(upperBound(t)); return erasure(upperBound(t), recurse);
} }
@Override @Override
public Type visitClassType(ClassType t, Void ignored) { public Type visitClassType(ClassType t, Boolean recurse) {
return t.tsym.erasure(Types.this); Type erased = t.tsym.erasure(Types.this);
if (recurse) {
erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym);
}
return erased;
} }
@Override @Override
public Type visitTypeVar(TypeVar t, Void ignored) { public Type visitTypeVar(TypeVar t, Boolean recurse) {
return erasure(t.bound); return erasure(t.bound, recurse);
} }
@Override @Override
public Type visitErrorType(ErrorType t, Void ignored) { public Type visitErrorType(ErrorType t, Boolean recurse) {
return t; return t;
} }
}; };
private Mapping erasureFun = new Mapping ("erasure") { private Mapping erasureFun = new Mapping ("erasure") {
public Type apply(Type t) { return erasure(t); } public Type apply(Type t) { return erasure(t); }
}; };
private Mapping erasureRecFun = new Mapping ("erasureRecursive") {
public Type apply(Type t) { return erasureRecursive(t); }
};
public List<Type> erasure(List<Type> ts) { public List<Type> erasure(List<Type> ts) {
return Type.map(ts, erasureFun); return Type.map(ts, erasureFun);
} }
public Type erasureRecursive(Type t) {
return erasure(t, true);
}
public List<Type> erasureRecursive(List<Type> ts) {
return Type.map(ts, erasureRecFun);
}
// </editor-fold> // </editor-fold>
// <editor-fold defaultstate="collapsed" desc="makeCompoundType"> // <editor-fold defaultstate="collapsed" desc="makeCompoundType">
...@@ -1626,15 +1647,14 @@ public class Types { ...@@ -1626,15 +1647,14 @@ public class Types {
if (t.supertype_field == null) { if (t.supertype_field == null) {
List<Type> actuals = classBound(t).allparams(); List<Type> actuals = classBound(t).allparams();
List<Type> formals = t.tsym.type.allparams(); List<Type> formals = t.tsym.type.allparams();
if (actuals.isEmpty()) { if (t.hasErasedSupertypes()) {
if (formals.isEmpty()) t.supertype_field = erasureRecursive(supertype);
// Should not happen. See comments below in interfaces } else if (formals.nonEmpty()) {
t.supertype_field = supertype;
else
t.supertype_field = erasure(supertype);
} else {
t.supertype_field = subst(supertype, formals, actuals); t.supertype_field = subst(supertype, formals, actuals);
} }
else {
t.supertype_field = supertype;
}
} }
} }
return t.supertype_field; return t.supertype_field;
...@@ -1708,18 +1728,15 @@ public class Types { ...@@ -1708,18 +1728,15 @@ public class Types {
assert t != t.tsym.type : t.toString(); assert t != t.tsym.type : t.toString();
List<Type> actuals = t.allparams(); List<Type> actuals = t.allparams();
List<Type> formals = t.tsym.type.allparams(); List<Type> formals = t.tsym.type.allparams();
if (actuals.isEmpty()) { if (t.hasErasedSupertypes()) {
if (formals.isEmpty()) { t.interfaces_field = erasureRecursive(interfaces);
// In this case t is not generic (nor raw). } else if (formals.nonEmpty()) {
// So this should not happen.
t.interfaces_field = interfaces;
} else {
t.interfaces_field = erasure(interfaces);
}
} else {
t.interfaces_field = t.interfaces_field =
upperBounds(subst(interfaces, formals, actuals)); upperBounds(subst(interfaces, formals, actuals));
} }
else {
t.interfaces_field = interfaces;
}
} }
} }
return t.interfaces_field; return t.interfaces_field;
......
/*
* Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6559182
* @summary Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
* @author Maurizio Cimadamore
*
* @compile Casting5.java
*/
class Casting5 {
static interface Super<P> {}
static class Y implements Super<Integer>{}
static interface X extends Super<Double>{}
static class S<L> extends Y {}
static interface T<L> extends X {}
public static void main(String... args) {
S s = null; // same if I use S<Byte>
T t = null; // same if I use T<Byte>
t = (T) s;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册