8071291: Compiler crashes trying to cast UnionType to IntersectionClassType

Compiler tests for .isCompound but then cast to a more specific Type subclass, resulting in CCE

Reviewed-by: vromero, jlahoda
This commit is contained in:
Maurizio Cimadamore 2015-02-23 13:02:37 +00:00
parent 13490088bf
commit 110a99f0dd
6 changed files with 118 additions and 51 deletions
langtools
src/jdk.compiler/share/classes/com/sun/tools/javac
test/tools/javac/multicatch/8071291

@ -477,6 +477,14 @@ public abstract class Type extends AnnoConstruct implements TypeMirror {
&& (tsym.flags() & COMPOUND) != 0; && (tsym.flags() & COMPOUND) != 0;
} }
public boolean isIntersection() {
return false;
}
public boolean isUnion() {
return false;
}
public boolean isInterface() { public boolean isInterface() {
return (tsym.flags() & INTERFACE) != 0; return (tsym.flags() & INTERFACE) != 0;
} }
@ -1079,6 +1087,11 @@ public abstract class Type extends AnnoConstruct implements TypeMirror {
return Collections.unmodifiableList(alternatives_field); return Collections.unmodifiableList(alternatives_field);
} }
@Override
public boolean isUnion() {
return true;
}
@Override @DefinedBy(Api.LANGUAGE_MODEL) @Override @DefinedBy(Api.LANGUAGE_MODEL)
public TypeKind getKind() { public TypeKind getKind() {
return TypeKind.UNION; return TypeKind.UNION;
@ -1125,6 +1138,11 @@ public abstract class Type extends AnnoConstruct implements TypeMirror {
return interfaces_field.prepend(supertype_field); return interfaces_field.prepend(supertype_field);
} }
@Override
public boolean isIntersection() {
return true;
}
public List<Type> getExplicitComponents() { public List<Type> getExplicitComponents() {
return allInterfaces ? return allInterfaces ?
interfaces_field : interfaces_field :

@ -1516,8 +1516,8 @@ public class Types {
} }
} }
if (t.isCompound() || s.isCompound()) { if (t.isIntersection() || s.isIntersection()) {
return !t.isCompound() ? return !t.isIntersection() ?
visitIntersectionType((IntersectionClassType)s, t, true) : visitIntersectionType((IntersectionClassType)s, t, true) :
visitIntersectionType((IntersectionClassType)t, s, false); visitIntersectionType((IntersectionClassType)t, s, false);
} }
@ -2245,19 +2245,28 @@ public class Types {
} }
// </editor-fold> // </editor-fold>
// <editor-fold defaultstate="collapsed" desc="makeCompoundType"> // <editor-fold defaultstate="collapsed" desc="makeIntersectionType">
/** /**
* Make a compound type from non-empty list of types. The list should be * Make an intersection type from non-empty list of types. The list should be ordered according to
* ordered according to {@link Symbol#precedes(TypeSymbol,Types)}. * {@link TypeSymbol#precedes(TypeSymbol, Types)}. Note that this might cause a symbol completion.
* Hence, this version of makeIntersectionType may not be called during a classfile read.
* *
* @param bounds the types from which the compound type is formed * @param bounds the types from which the intersection type is formed
* @param supertype is objectType if all bounds are interfaces,
* null otherwise.
*/ */
public Type makeCompoundType(List<Type> bounds) { public IntersectionClassType makeIntersectionType(List<Type> bounds) {
return makeCompoundType(bounds, bounds.head.tsym.isInterface()); return makeIntersectionType(bounds, bounds.head.tsym.isInterface());
} }
public Type makeCompoundType(List<Type> bounds, boolean allInterfaces) {
/**
* Make an intersection type from non-empty list of types. The list should be ordered according to
* {@link TypeSymbol#precedes(TypeSymbol, Types)}. This does not cause symbol completion as
* an extra parameter indicates as to whether all bounds are interfaces - in which case the
* supertype is implicitly assumed to be 'Object'.
*
* @param bounds the types from which the intersection type is formed
* @param allInterfaces are all bounds interface types?
*/
public IntersectionClassType makeIntersectionType(List<Type> bounds, boolean allInterfaces) {
Assert.check(bounds.nonEmpty()); Assert.check(bounds.nonEmpty());
Type firstExplicitBound = bounds.head; Type firstExplicitBound = bounds.head;
if (allInterfaces) { if (allInterfaces) {
@ -2270,23 +2279,13 @@ public class Types {
: names.empty, : names.empty,
null, null,
syms.noSymbol); syms.noSymbol);
bc.type = new IntersectionClassType(bounds, bc, allInterfaces); IntersectionClassType intersectionType = new IntersectionClassType(bounds, bc, allInterfaces);
bc.type = intersectionType;
bc.erasure_field = (bounds.head.hasTag(TYPEVAR)) ? bc.erasure_field = (bounds.head.hasTag(TYPEVAR)) ?
syms.objectType : // error condition, recover syms.objectType : // error condition, recover
erasure(firstExplicitBound); erasure(firstExplicitBound);
bc.members_field = WriteableScope.create(bc); bc.members_field = WriteableScope.create(bc);
return bc.type; return intersectionType;
}
/**
* A convenience wrapper for {@link #makeCompoundType(List)}; the
* arguments are converted to a list and passed to the other
* method. Note that this might cause a symbol completion.
* Hence, this version of makeCompoundType may not be called
* during a classfile read.
*/
public Type makeCompoundType(Type bound1, Type bound2) {
return makeCompoundType(List.of(bound1, bound2));
} }
// </editor-fold> // </editor-fold>
@ -2426,7 +2425,7 @@ public class Types {
private final UnaryVisitor<List<Type>> directSupertypes = new UnaryVisitor<List<Type>>() { private final UnaryVisitor<List<Type>> directSupertypes = new UnaryVisitor<List<Type>>() {
public List<Type> visitType(final Type type, final Void ignored) { public List<Type> visitType(final Type type, final Void ignored) {
if (!type.isCompound()) { if (!type.isIntersection()) {
final Type sup = supertype(type); final Type sup = supertype(type);
return (sup == Type.noType || sup == type || sup == null) return (sup == Type.noType || sup == type || sup == null)
? interfaces(type) ? interfaces(type)
@ -2480,30 +2479,32 @@ public class Types {
// <editor-fold defaultstate="collapsed" desc="setBounds"> // <editor-fold defaultstate="collapsed" desc="setBounds">
/** /**
* Set the bounds field of the given type variable to reflect a * Same as {@link Types#setBounds(TypeVar, List, boolean)}, except that third parameter is computed directly,
* (possibly multiple) list of bounds. * as follows: if all all bounds are interface types, the computed supertype is Object,otherwise
* @param t a type variable * the supertype is simply left null (in this case, the supertype is assumed to be the head of
* @param bounds the bounds, must be nonempty * the bound list passed as second argument). Note that this check might cause a symbol completion.
* @param supertype is objectType if all bounds are interfaces, * Hence, this version of setBounds may not be called during a classfile read.
* null otherwise. *
* @param t a type variable
* @param bounds the bounds, must be nonempty
*/ */
public void setBounds(TypeVar t, List<Type> bounds) { public void setBounds(TypeVar t, List<Type> bounds) {
setBounds(t, bounds, bounds.head.tsym.isInterface()); setBounds(t, bounds, bounds.head.tsym.isInterface());
} }
/** /**
* Same as {@link #setBounds(Type.TypeVar,List,Type)}, except that * Set the bounds field of the given type variable to reflect a (possibly multiple) list of bounds.
* third parameter is computed directly, as follows: if all * This does not cause symbol completion as an extra parameter indicates as to whether all bounds
* all bounds are interface types, the computed supertype is Object, * are interfaces - in which case the supertype is implicitly assumed to be 'Object'.
* otherwise the supertype is simply left null (in this case, the supertype *
* is assumed to be the head of the bound list passed as second argument). * @param t a type variable
* Note that this check might cause a symbol completion. Hence, this version of * @param bounds the bounds, must be nonempty
* setBounds may not be called during a classfile read. * @param allInterfaces are all bounds interface types?
*/ */
public void setBounds(TypeVar t, List<Type> bounds, boolean allInterfaces) { public void setBounds(TypeVar t, List<Type> bounds, boolean allInterfaces) {
t.bound = bounds.tail.isEmpty() ? t.bound = bounds.tail.isEmpty() ?
bounds.head : bounds.head :
makeCompoundType(bounds, allInterfaces); makeIntersectionType(bounds, allInterfaces);
t.rank_field = -1; t.rank_field = -1;
} }
// </editor-fold> // </editor-fold>
@ -3035,7 +3036,7 @@ public class Types {
if (st == supertype(t) && is == interfaces(t)) if (st == supertype(t) && is == interfaces(t))
return t; return t;
else else
return makeCompoundType(is.prepend(st)); return makeIntersectionType(is.prepend(st));
} }
} }
@ -3544,7 +3545,7 @@ public class Types {
else if (compound.tail.isEmpty()) else if (compound.tail.isEmpty())
return compound.head; return compound.head;
else else
return makeCompoundType(compound); return makeIntersectionType(compound);
} }
/** /**
@ -3722,8 +3723,8 @@ public class Types {
synchronized (this) { synchronized (this) {
if (arraySuperType == null) { if (arraySuperType == null) {
// JLS 10.8: all arrays implement Cloneable and Serializable. // JLS 10.8: all arrays implement Cloneable and Serializable.
arraySuperType = makeCompoundType(List.of(syms.serializableType, arraySuperType = makeIntersectionType(List.of(syms.serializableType,
syms.cloneableType), true); syms.cloneableType), true);
} }
} }
} }
@ -3789,7 +3790,7 @@ public class Types {
return glbFlattened(union(bounds, lowers), errT); return glbFlattened(union(bounds, lowers), errT);
} }
} }
return makeCompoundType(bounds); return makeIntersectionType(bounds);
} }
// </editor-fold> // </editor-fold>

@ -2355,7 +2355,7 @@ public class Attr extends JCTree.Visitor {
@Override @Override
public Type visitClassType(ClassType t, DiagnosticPosition pos) { public Type visitClassType(ClassType t, DiagnosticPosition pos) {
return t.isCompound() ? return t.isIntersection() ?
visitIntersectionClassType((IntersectionClassType)t, pos) : t; visitIntersectionClassType((IntersectionClassType)t, pos) : t;
} }
@ -2386,8 +2386,7 @@ public class Attr extends JCTree.Visitor {
} }
supertypes.append(i.tsym.type); supertypes.append(i.tsym.type);
} }
IntersectionClassType notionalIntf = IntersectionClassType notionalIntf = types.makeIntersectionType(supertypes.toList());
(IntersectionClassType)types.makeCompoundType(supertypes.toList());
notionalIntf.allparams_field = targs.toList(); notionalIntf.allparams_field = targs.toList();
notionalIntf.tsym.flags_field |= INTERFACE; notionalIntf.tsym.flags_field |= INTERFACE;
return notionalIntf.tsym; return notionalIntf.tsym;
@ -3947,7 +3946,7 @@ public class Attr extends JCTree.Visitor {
} else if (bounds.length() == 1) { } else if (bounds.length() == 1) {
return bounds.head.type; return bounds.head.type;
} else { } else {
Type owntype = types.makeCompoundType(TreeInfo.types(bounds)); Type owntype = types.makeIntersectionType(TreeInfo.types(bounds));
// ... the variable's bound is a class type flagged COMPOUND // ... the variable's bound is a class type flagged COMPOUND
// (see comment for TypeVar.bound). // (see comment for TypeVar.bound).
// In this case, generate a class tree that represents the // In this case, generate a class tree that represents the

@ -417,7 +417,7 @@ public class Infer {
List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER); List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
if (Type.containsAny(upperBounds, vars)) { if (Type.containsAny(upperBounds, vars)) {
TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner); TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null); fresh_tvar.type = new TypeVar(fresh_tvar, types.makeIntersectionType(uv.getBounds(InferenceBound.UPPER)), null);
todo.append(uv); todo.append(uv);
uv.inst = fresh_tvar.type; uv.inst = fresh_tvar.type;
} else if (upperBounds.nonEmpty()) { } else if (upperBounds.nonEmpty()) {
@ -670,7 +670,7 @@ public class Infer {
if (lubResult == syms.errType || lubResult == syms.botType) { if (lubResult == syms.errType || lubResult == syms.botType) {
return List.nil(); return List.nil();
} }
List<Type> supertypesToCheck = lubResult.isCompound() ? List<Type> supertypesToCheck = lubResult.isIntersection() ?
((IntersectionClassType)lubResult).getComponents() : ((IntersectionClassType)lubResult).getComponents() :
List.of(lubResult); List.of(lubResult);
ListBuffer<Pair<Type, Type>> commonSupertypes = new ListBuffer<>(); ListBuffer<Pair<Type, Type>> commonSupertypes = new ListBuffer<>();

@ -764,7 +764,7 @@ public class TransTypes extends TreeTranslator {
? typeCast.expr ? typeCast.expr
: newExpression; : newExpression;
} }
if (originalTarget.isCompound()) { if (originalTarget.isIntersection()) {
Type.IntersectionClassType ict = (Type.IntersectionClassType)originalTarget; Type.IntersectionClassType ict = (Type.IntersectionClassType)originalTarget;
for (Type c : ict.getExplicitComponents()) { for (Type c : ict.getExplicitComponents()) {
Type ec = erasure(c); Type ec = erasure(c);

@ -0,0 +1,49 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8071291
* @summary Compiler crashes trying to cast UnionType to IntersectionClassType
* @compile T8071291.java
*/
class T8071291 {
interface A { }
class Exception1 extends Exception implements A { }
class Exception2 extends Exception implements A { }
void test(boolean cond) {
try {
if (cond) {
throw new Exception1();
} else {
throw new Exception2();
}
}
catch (Exception1|Exception2 x) {
if (x instanceof Exception1) { }
}
}
}