Skip to content

Commit e1d53f0

Browse files
committed
Streamlined treatment of ThisType in subtype checks.
1 parent c4bed34 commit e1d53f0

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class TypeComparer(initctx: Context) extends DotClass {
195195
val saved = constraint
196196
val savedSuccessCount = successCount
197197
val savedTotalCount = totalCount
198-
if (Stats.monitored) Stats.record(s"isSubType ${tp1.getClass} <:< ${tp2.getClass}")
198+
if (Stats.monitored) Stats.record(s"isSubType ${tp1.show} <:< ${tp2.show}")
199199
try {
200200
recCount += 1
201201
/* !!! DEBUG
@@ -281,23 +281,14 @@ class TypeComparer(initctx: Context) extends DotClass {
281281
) else
282282
tp1.name == tp2.name && isSubType(tp1.prefix, tp2.prefix)
283283
) || secondTryNamed(tp1, tp2)
284+
case ThisType(cls) if cls eq tp2.symbol.moduleClass =>
285+
isSubType(cls.owner.thisType, tp2.prefix)
284286
case _ =>
285287
secondTry(tp1, tp2)
286288
}
287289
compareNamed
288290
case tp2: ProtoType =>
289291
isMatchedByProto(tp2, tp1)
290-
case tp2 @ ThisType(cls) =>
291-
def compareThis: Boolean = {
292-
if (cls is ModuleClass)
293-
tp1 match {
294-
case tp1: TermRef =>
295-
if (tp1.symbol.moduleClass == cls) return tp1.prefix <:< cls.owner.thisType
296-
case _ =>
297-
}
298-
secondTry(tp1, tp2)
299-
}
300-
compareThis
301292
case tp2: PolyParam =>
302293
def comparePolyParam = {
303294
tp2 == tp1 ||
@@ -330,20 +321,14 @@ class TypeComparer(initctx: Context) extends DotClass {
330321

331322
def secondTry(tp1: Type, tp2: Type): Boolean = tp1 match {
332323
case tp1: NamedType =>
333-
secondTryNamed(tp1, tp2)
324+
tp2 match {
325+
case ThisType(cls) if cls eq tp1.symbol.moduleClass =>
326+
isSubType(tp1.prefix, cls.owner.thisType)
327+
case _ =>
328+
secondTryNamed(tp1, tp2)
329+
}
334330
case OrType(tp11, tp12) =>
335331
isSubType(tp11, tp2) && isSubType(tp12, tp2)
336-
case tp1 @ ThisType(cls) =>
337-
def compareThis: Boolean = {
338-
if (cls is ModuleClass)
339-
tp2 match {
340-
case tp2: TermRef =>
341-
if (tp2.symbol.moduleClass == cls) return cls.owner.thisType <:< tp2.prefix
342-
case _ =>
343-
}
344-
thirdTry(tp1, tp2)
345-
}
346-
compareThis
347332
case tp1: PolyParam =>
348333
def comparePolyParam = {
349334
tp1 == tp2 ||
@@ -622,7 +607,7 @@ class TypeComparer(initctx: Context) extends DotClass {
622607
(hkArgs.length == tparams.length) && {
623608
val base = tp1.narrow
624609
(tparams, hkArgs).zipped.forall { (tparam, hkArg) =>
625-
base.memberInfo(tparam) <:< hkArg.bounds // TODO: base.memberInfo needed?
610+
isSubType(base.memberInfo(tparam), hkArg.bounds) // TODO: base.memberInfo needed?
626611
} &&
627612
(tparams, tp2.typeSymbol.typeParams).zipped.forall { (tparam, tparam2) =>
628613
tparam.variance == tparam2.variance
@@ -631,7 +616,7 @@ class TypeComparer(initctx: Context) extends DotClass {
631616
}
632617

633618
def trySetType(tr: NamedType, bounds: TypeBounds): Boolean =
634-
(bounds.lo <:< bounds.hi) &&
619+
isSubType(bounds.lo, bounds.hi) &&
635620
{ tr.symbol.changeGADTInfo(bounds); true }
636621

637622
/** A function implementing `tp1` matches `tp2`. */
@@ -1050,7 +1035,7 @@ class TypeComparer(initctx: Context) extends DotClass {
10501035
case tp1: ClassInfo =>
10511036
tp2 match {
10521037
case tp2: ClassInfo =>
1053-
(tp1.prefix <:< tp2.prefix) || (tp1.cls.owner derivesFrom tp2.cls.owner)
1038+
isSubType(tp1.prefix, tp2.prefix) || (tp1.cls.owner derivesFrom tp2.cls.owner)
10541039
case _ =>
10551040
false
10561041
}
@@ -1066,7 +1051,7 @@ class TypeComparer(initctx: Context) extends DotClass {
10661051
tp2 match {
10671052
case tp2: MethodType =>
10681053
def asGoodParams(formals1: List[Type], formals2: List[Type]) =
1069-
(formals2 corresponds formals1)(_ <:< _)
1054+
(formals2 corresponds formals1)(isSubType)
10701055
asGoodParams(tp1.paramTypes, tp2.paramTypes) &&
10711056
(!asGoodParams(tp2.paramTypes, tp1.paramTypes) ||
10721057
isAsGood(tp1.resultType, tp2.resultType))

0 commit comments

Comments
 (0)