8196074: Remove uses of loose type equality tests

Drop loose type equality check and replace usages to go throuhg the strict version

Reviewed-by: vromero
This commit is contained in:
Bernard Blaser 2018-01-24 17:24:32 +00:00 committed by Maurizio Cimadamore
parent 551a0d104f
commit cd31cb85e9
4 changed files with 21 additions and 99 deletions

View File

@ -2107,9 +2107,8 @@ public abstract class Type extends AnnoConstruct implements TypeMirror {
List<Type> prevBounds = bounds.get(ib);
if (bound == qtype) return;
for (Type b : prevBounds) {
//check for redundancy - use strict version of isSameType on tvars
//(as the standard version will lead to false positives w.r.t. clones ivars)
if (types.isSameType(b, bound2, true)) return;
//check for redundancy - do not add same bound twice
if (types.isSameType(b, bound2)) return;
}
bounds.put(ib, prevBounds.prepend(bound2));
notifyBoundChange(ib, bound2, false);

View File

@ -1291,12 +1291,9 @@ public class Types {
* lists are of different length, return false.
*/
public boolean isSameTypes(List<Type> ts, List<Type> ss) {
return isSameTypes(ts, ss, false);
}
public boolean isSameTypes(List<Type> ts, List<Type> ss, boolean strict) {
while (ts.tail != null && ss.tail != null
/*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
isSameType(ts.head, ss.head, strict)) {
isSameType(ts.head, ss.head)) {
ts = ts.tail;
ss = ss.tail;
}
@ -1325,15 +1322,15 @@ public class Types {
* Is t the same type as s?
*/
public boolean isSameType(Type t, Type s) {
return isSameType(t, s, false);
}
public boolean isSameType(Type t, Type s, boolean strict) {
return strict ?
isSameTypeStrict.visit(t, s) :
isSameTypeLoose.visit(t, s);
return isSameTypeVisitor.visit(t, s);
}
// where
abstract class SameTypeVisitor extends TypeRelation {
/**
* Type-equality relation - type variables are considered
* equals if they share the same object identity.
*/
TypeRelation isSameTypeVisitor = new TypeRelation() {
public Boolean visitType(Type t, Type s) {
if (t.equalsIgnoreMetadata(s))
@ -1350,7 +1347,7 @@ public class Types {
if (s.hasTag(TYPEVAR)) {
//type-substitution does not preserve type-var types
//check that type var symbols and bounds are indeed the same
return sameTypeVars((TypeVar)t, (TypeVar)s);
return t == s;
}
else {
//special case for s == ? super X, where upper(s) = u
@ -1365,8 +1362,6 @@ public class Types {
}
}
abstract boolean sameTypeVars(TypeVar tv1, TypeVar tv2);
@Override
public Boolean visitWildcardType(WildcardType t, Type s) {
if (!s.hasTag(WILDCARD)) {
@ -1374,7 +1369,7 @@ public class Types {
} else {
WildcardType t2 = (WildcardType)s;
return (t.kind == t2.kind || (t.isExtendsBound() && s.isExtendsBound())) &&
isSameType(t.type, t2.type, true);
isSameType(t.type, t2.type);
}
}
@ -1411,11 +1406,9 @@ public class Types {
}
return t.tsym == s.tsym
&& visit(t.getEnclosingType(), s.getEnclosingType())
&& containsTypes(t.getTypeArguments(), s.getTypeArguments());
&& containsTypeEquivalent(t.getTypeArguments(), s.getTypeArguments());
}
abstract protected boolean containsTypes(List<Type> ts1, List<Type> ts2);
@Override
public Boolean visitArrayType(ArrayType t, Type s) {
if (t == s)
@ -1471,70 +1464,6 @@ public class Types {
public Boolean visitErrorType(ErrorType t, Type s) {
return true;
}
}
/**
* Standard type-equality relation - type variables are considered
* equals if they share the same type symbol.
*/
TypeRelation isSameTypeLoose = new LooseSameTypeVisitor();
private class LooseSameTypeVisitor extends SameTypeVisitor {
/** cache of the type-variable pairs being (recursively) tested. */
private Set<TypePair> cache = new HashSet<>();
@Override
boolean sameTypeVars(TypeVar tv1, TypeVar tv2) {
return tv1.tsym == tv2.tsym && checkSameBounds(tv1, tv2);
}
@Override
protected boolean containsTypes(List<Type> ts1, List<Type> ts2) {
return containsTypeEquivalent(ts1, ts2);
}
/**
* Since type-variable bounds can be recursive, we need to protect against
* infinite loops - where the same bounds are checked over and over recursively.
*/
private boolean checkSameBounds(TypeVar tv1, TypeVar tv2) {
TypePair p = new TypePair(tv1, tv2, true);
if (cache.add(p)) {
try {
return visit(tv1.getUpperBound(), tv2.getUpperBound());
} finally {
cache.remove(p);
}
} else {
return false;
}
}
};
/**
* Strict type-equality relation - type variables are considered
* equals if they share the same object identity.
*/
TypeRelation isSameTypeStrict = new SameTypeVisitor() {
@Override
boolean sameTypeVars(TypeVar tv1, TypeVar tv2) {
return tv1 == tv2;
}
@Override
protected boolean containsTypes(List<Type> ts1, List<Type> ts2) {
return isSameTypes(ts1, ts2, true);
}
@Override
public Boolean visitWildcardType(WildcardType t, Type s) {
if (!s.hasTag(WILDCARD)) {
return false;
} else {
WildcardType t2 = (WildcardType)s;
return t.kind == t2.kind &&
isSameType(t.type, t2.type, true);
}
}
};
// </editor-fold>
@ -3848,17 +3777,11 @@ public class Types {
// where
class TypePair {
final Type t1;
final Type t2;
boolean strict;
final Type t2;;
TypePair(Type t1, Type t2) {
this(t1, t2, false);
}
TypePair(Type t1, Type t2, boolean strict) {
this.t1 = t1;
this.t2 = t2;
this.strict = strict;
}
@Override
public int hashCode() {
@ -3869,8 +3792,8 @@ public class Types {
if (!(obj instanceof TypePair))
return false;
TypePair typePair = (TypePair)obj;
return isSameType(t1, typePair.t1, strict)
&& isSameType(t2, typePair.t2, strict);
return isSameType(t1, typePair.t1)
&& isSameType(t2, typePair.t2);
}
}
Set<TypePair> mergeCache = new HashSet<>();
@ -4460,7 +4383,7 @@ public class Types {
Type tmpLower = Si.lower.hasTag(UNDETVAR) ? ((UndetVar)Si.lower).qtype : Si.lower;
if (!Si.bound.hasTag(ERROR) &&
!Si.lower.hasTag(ERROR) &&
isSameType(tmpBound, tmpLower, false)) {
isSameType(tmpBound, tmpLower)) {
currentS.head = Si.bound;
}
}

View File

@ -565,7 +565,7 @@ public class Infer {
List<Type> argtypes) {
final Type restype;
if (spMethod == null || types.isSameType(spMethod.getReturnType(), syms.objectType, true)) {
if (spMethod == null || types.isSameType(spMethod.getReturnType(), syms.objectType)) {
// The return type of the polymorphic signature is polymorphic,
// and is computed from the enclosing tree E, as follows:
// if E is a cast, then use the target type of the cast expression
@ -1227,8 +1227,8 @@ public class Infer {
} else {
IncorporationBinaryOp that = (IncorporationBinaryOp)o;
return opKind == that.opKind &&
types.isSameType(op1, that.op1, true) &&
types.isSameType(op2, that.op2, true);
types.isSameType(op1, that.op1) &&
types.isSameType(op2, that.op2);
}
}

View File

@ -788,7 +788,7 @@ public class TransTypes extends TreeTranslator {
JCTypeCast typeCast = newExpression.hasTag(Tag.TYPECAST)
? (JCTypeCast) newExpression
: null;
tree.expr = typeCast != null && types.isSameType(typeCast.type, originalTarget, true)
tree.expr = typeCast != null && types.isSameType(typeCast.type, originalTarget)
? typeCast.expr
: newExpression;
}