summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2013-09-11 10:17:56 -0700
committerPaul Phillips <paulp@improving.org>2013-09-11 12:18:41 -0700
commit92bd4a75eb546565df903793fe52b35c9159d1d6 (patch)
tree352b66d53ff26a8df129146d71047749d0761f65 /src
parent9788e7a1f1809491154c2bcb47d3061b11c1d8a8 (diff)
downloadscala-92bd4a75eb546565df903793fe52b35c9159d1d6.tar.gz
scala-92bd4a75eb546565df903793fe52b35c9159d1d6.tar.bz2
scala-92bd4a75eb546565df903793fe52b35c9159d1d6.zip
SI-7834 Type equivalence of C.this and C.super.
Foo.this.x and Foo.super.x were roughly unrelated in the eyes of isSubType. I implemented conformance as described in the comment: This is looking for situations such as B.this.x.type <:< B.super.x.type. If it's a ThisType on the lhs and a SuperType on the right, and they originate in the same class, and the 'x' in the ThisType has in its override chain the 'x' in the SuperType, then the types conform. I think this is overly conservative but it's way ahead of where it was.
Diffstat (limited to 'src')
-rw-r--r--src/reflect/scala/reflect/internal/Types.scala14
-rw-r--r--src/reflect/scala/reflect/internal/tpe/TypeComparers.scala18
2 files changed, 19 insertions, 13 deletions
diff --git a/src/reflect/scala/reflect/internal/Types.scala b/src/reflect/scala/reflect/internal/Types.scala
index ca01a4b8e3..ad9001ca4e 100644
--- a/src/reflect/scala/reflect/internal/Types.scala
+++ b/src/reflect/scala/reflect/internal/Types.scala
@@ -4013,15 +4013,11 @@ trait Types
* type selections with the same name of equal (as determined by `=:=`) prefixes are
* considered equal in regard to `=:=`.
*/
- def beginsWithTypeVarOrIsRefined(tp: Type): Boolean = tp match {
- case SingleType(pre, sym) =>
- !(sym hasFlag PACKAGE) && beginsWithTypeVarOrIsRefined(pre)
- case tv@TypeVar(_, constr) =>
- !tv.instValid || beginsWithTypeVarOrIsRefined(constr.inst)
- case RefinedType(_, _) =>
- true
- case _ =>
- false
+ def isEligibleForPrefixUnification(tp: Type): Boolean = tp match {
+ case SingleType(pre, sym) => !(sym hasFlag PACKAGE) && isEligibleForPrefixUnification(pre)
+ case tv@TypeVar(_, constr) => !tv.instValid || isEligibleForPrefixUnification(constr.inst)
+ case RefinedType(_, _) => true
+ case _ => false
}
def isErrorOrWildcard(tp: Type) = (tp eq ErrorType) || (tp eq WildcardType)
diff --git a/src/reflect/scala/reflect/internal/tpe/TypeComparers.scala b/src/reflect/scala/reflect/internal/tpe/TypeComparers.scala
index d8b3b04d0e..152c689c56 100644
--- a/src/reflect/scala/reflect/internal/tpe/TypeComparers.scala
+++ b/src/reflect/scala/reflect/internal/tpe/TypeComparers.scala
@@ -35,8 +35,10 @@ trait TypeComparers {
private var subsametypeRecursions: Int = 0
- private def isUnifiable(pre1: Type, pre2: Type) =
- (beginsWithTypeVarOrIsRefined(pre1) || beginsWithTypeVarOrIsRefined(pre2)) && (pre1 =:= pre2)
+ private def isUnifiable(pre1: Type, pre2: Type) = (
+ (isEligibleForPrefixUnification(pre1) || isEligibleForPrefixUnification(pre2))
+ && (pre1 =:= pre2)
+ )
/** Returns true iff we are past phase specialize,
* sym1 and sym2 are two existential skolems with equal names and bounds,
@@ -64,7 +66,6 @@ trait TypeComparers {
(sym1.name == sym2.name) && isUnifiable(pre1, pre2)
)
-
def isDifferentType(tp1: Type, tp2: Type): Boolean = try {
subsametypeRecursions += 1
undoLog undo { // undo type constraints that arise from operations in this block
@@ -207,6 +208,7 @@ trait TypeComparers {
}
case _ => false
}
+
/* Those false cases certainly are ugly. There's a proposed SIP to deuglify it.
* https://docs.google.com/a/improving.org/document/d/1onPrzSqyDpHScc9PS_hpxJwa3FlPtthxw-bAuuEe8uA
*/
@@ -334,6 +336,14 @@ trait TypeComparers {
(tparams1 corresponds tparams2)(cmp) && (sub1(res1) <:< sub2(res2))
}
}
+ // This is looking for situations such as B.this.x.type <:< B.super.x.type.
+ // If it's a ThisType on the lhs and a SuperType on the right, and they originate
+ // in the same class, and the 'x' in the ThisType has in its override chain
+ // the 'x' in the SuperType, then the types conform.
+ private def isThisAndSuperSubtype(tp1: Type, tp2: Type): Boolean = (tp1, tp2) match {
+ case (SingleType(ThisType(lpre), v1), SingleType(SuperType(ThisType(rpre), _), v2)) => (lpre == rpre) && (v1.overrideChain contains v2)
+ case _ => false
+ }
// @assume tp1.isHigherKinded || tp2.isHigherKinded
def isHKSubType(tp1: Type, tp2: Type, depth: Depth): Boolean = {
@@ -359,7 +369,7 @@ trait TypeComparers {
def retry(lhs: Type, rhs: Type) = ((lhs ne tp1) || (rhs ne tp2)) && isSubType(lhs, rhs, depth)
if (isSingleType(tp1) && isSingleType(tp2) || isConstantType(tp1) && isConstantType(tp2))
- return (tp1 =:= tp2) || retry(tp1.underlying, tp2)
+ return (tp1 =:= tp2) || isThisAndSuperSubtype(tp1, tp2) || retry(tp1.underlying, tp2)
if (tp1.isHigherKinded || tp2.isHigherKinded)
return isHKSubType(tp1, tp2, depth)