Skip to content

Commit f571469

Browse files
oderskymichelou
authored andcommitted
Use onGadtBounds more widely
1 parent f2d86ea commit f571469

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

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

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
169169
private inline def inFrozenGadtAndConstraint[T](inline op: T): T =
170170
inFrozenGadtIf(true)(inFrozenConstraint(op))
171171

172-
extension (tp: TypeRef)
172+
extension (sym: Symbol)
173173
private inline def onGadtBounds(inline op: TypeBounds => Boolean): Boolean =
174-
val bounds = gadtBounds(tp.symbol)
174+
val bounds = gadtBounds(sym)
175175
bounds != null && op(bounds)
176176

177177
protected def isSubType(tp1: Type, tp2: Type, a: ApproxState): Boolean = {
@@ -490,11 +490,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
490490

491491
def thirdTryNamed(tp2: NamedType): Boolean = tp2.info match {
492492
case info2: TypeBounds =>
493-
def compareGADT: Boolean = {
494-
val gbounds2 = gadtBounds(tp2.symbol)
495-
(gbounds2 != null) &&
496-
(isSubTypeWhenFrozen(tp1, gbounds2.lo) ||
497-
(tp1 match {
493+
def compareGADT: Boolean =
494+
tp2.symbol.onGadtBounds(gbounds2 =>
495+
isSubTypeWhenFrozen(tp1, gbounds2.lo)
496+
|| tp1.match
498497
case tp1: NamedType if ctx.gadt.contains(tp1.symbol) =>
499498
// Note: since we approximate constrained types only with their non-param bounds,
500499
// we need to manually handle the case when we're comparing two constrained types,
@@ -503,10 +502,9 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
503502
// comparing two constrained types, and that case will be handled here first.
504503
ctx.gadt.isLess(tp1.symbol, tp2.symbol) && GADTusage(tp1.symbol) && GADTusage(tp2.symbol)
505504
case _ => false
506-
}) ||
507-
narrowGADTBounds(tp2, tp1, approx, isUpper = false)) &&
508-
{ isBottom(tp1) || GADTusage(tp2.symbol) }
509-
}
505+
|| narrowGADTBounds(tp2, tp1, approx, isUpper = false))
506+
&& (isBottom(tp1) || GADTusage(tp2.symbol))
507+
510508
isSubApproxHi(tp1, info2.lo) || compareGADT || tryLiftedToThis2 || fourthTry
511509

512510
case _ =>
@@ -756,13 +754,12 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
756754
case tp1: TypeRef =>
757755
tp1.info match {
758756
case TypeBounds(_, hi1) =>
759-
def compareGADT = {
760-
val gbounds1 = gadtBounds(tp1.symbol)
761-
(gbounds1 != null) &&
762-
(isSubTypeWhenFrozen(gbounds1.hi, tp2) ||
763-
narrowGADTBounds(tp1, tp2, approx, isUpper = true)) &&
764-
{ tp2.isAny || GADTusage(tp1.symbol) }
765-
}
757+
def compareGADT =
758+
tp1.symbol.onGadtBounds(gbounds1 =>
759+
isSubTypeWhenFrozen(gbounds1.hi, tp2)
760+
|| narrowGADTBounds(tp1, tp2, approx, isUpper = true))
761+
&& (tp2.isAny || GADTusage(tp1.symbol))
762+
766763
isSubType(hi1, tp2, approx.addLow) || compareGADT || tryLiftedToThis1
767764
case _ =>
768765
def isNullable(tp: Type): Boolean = tp.widenDealias match {
@@ -1038,17 +1035,12 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
10381035

10391036
var touchedGADTs = false
10401037
var gadtIsInstantiated = false
1041-
def byGadtBounds(sym: Symbol, tp: Type, fromAbove: Boolean): Boolean = {
1042-
touchedGADTs = true
1043-
val b = gadtBounds(sym)
1044-
def boundsDescr = if b == null then "null" else b.show
1045-
b != null && inFrozenGadt {
1046-
if fromAbove then isSubType(b.hi, tp) else isSubType(tp, b.lo)
1047-
} && {
1048-
gadtIsInstantiated = b.isInstanceOf[TypeAlias]
1049-
true
1050-
}
1051-
}
1038+
1039+
extension (sym: Symbol)
1040+
inline def byGadtBounds(inline op: TypeBounds => Boolean): Boolean =
1041+
touchedGADTs = true
1042+
sym.onGadtBounds(
1043+
b => op(b) && { gadtIsInstantiated = b.isInstanceOf[TypeAlias]; true })
10521044

10531045
def byGadtOrdering: Boolean =
10541046
ctx.gadt.contains(tycon1sym)
@@ -1057,8 +1049,8 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
10571049

10581050
val res = (
10591051
tycon1sym == tycon2sym && isSubPrefix(tycon1.prefix, tycon2.prefix)
1060-
|| byGadtBounds(tycon1sym, tycon2, fromAbove = true)
1061-
|| byGadtBounds(tycon2sym, tycon1, fromAbove = false)
1052+
|| tycon1sym.byGadtBounds(b => isSubTypeWhenFrozen(b.hi, tycon2))
1053+
|| tycon2sym.byGadtBounds(b => isSubTypeWhenFrozen(tycon1, b.lo))
10621054
|| byGadtOrdering
10631055
) && {
10641056
// There are two cases in which we can assume injectivity.
@@ -1712,7 +1704,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
17121704
case tp: AndType => true
17131705
case OrType(tp1, tp2) => containsAnd(tp1) || containsAnd(tp2)
17141706
case tp: TypeParamRef => containsAnd(bounds(tp).hi)
1715-
case tp: TypeRef => containsAnd(tp.info.hiBound) || tp.onGadtBounds(gbounds => containsAnd(gbounds.hi))
1707+
case tp: TypeRef => containsAnd(tp.info.hiBound) || tp.symbol.onGadtBounds(gbounds => containsAnd(gbounds.hi))
17161708
case tp: TypeProxy => containsAnd(tp.superType)
17171709
case _ => false
17181710

0 commit comments

Comments
 (0)