Skip to content

Commit 2b99679

Browse files
committed
clean up
1 parent 775d0da commit 2b99679

File tree

1 file changed

+37
-61
lines changed
  • compiler/src/dotty/tools/dotc/transform/patmat

1 file changed

+37
-61
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -427,20 +427,6 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
427427
}
428428
}
429429

430-
/* Erase a type binding according to erasure semantics in pattern matching */
431-
def erase(tp: Type): Type = {
432-
tp match {
433-
case tp @ AppliedType(tycon, args) => erase(tp.superType)
434-
if (tycon.isRef(defn.ArrayClass)) tp.derivedAppliedType(tycon, args.map(erase))
435-
else tp.derivedAppliedType(tycon, args.map(t => WildcardType(TypeBounds.empty)))
436-
case OrType(tp1, tp2) =>
437-
OrType(erase(tp1), erase(tp2))
438-
case AndType(tp1, tp2) =>
439-
AndType(erase(tp1), erase(tp2))
440-
case _ => tp
441-
}
442-
}
443-
444430
/** Is `tp1` a subtype of `tp2`? */
445431
def isSubType(tp1: Type, tp2: Type): Boolean = {
446432
val res = tp1 <:< tp2
@@ -544,14 +530,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
544530

545531
val childTp = if (child.isTerm) child.termRef else child.typeRef
546532

547-
var resTp = instantiate(childTp, parent)(ctx.fresh.setNewTyperState)
548-
549-
// try expose references to type parameters in the parent type
550-
//
551-
// tests/patmat/t9657.scala
552-
// tests/patmat/gadt.scala LOC#50
553-
//
554-
// if (!resTp.exists) resTp = instantiate(childTp, expose(parent))(ctx.fresh.setNewTyperState)
533+
var resTp = instantiate(childTp, expose(parent))(ctx.fresh.setNewTyperState)
555534

556535
if (!resTp.exists) {
557536
debug.println(s"[refine] unqualified child ousted: ${childTp.show} !< ${parent.show}")
@@ -586,9 +565,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
586565
val tvars = tp1.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds) }
587566
val protoTp1 = thisTypeMap(tp1.appliedTo(tvars))
588567

589-
// try expose references to type parameters in the parent type
590-
// tests/patmat/t9657.scala
591-
// tests/patmat/gadt.scala LOC#50
568+
// replace type parameter references with fresh type vars or bounds
592569
val typeParamMap = new TypeMap {
593570
def apply(t: Type): Type = t match {
594571
case tp: TypeRef if tp.underlying.isInstanceOf[TypeBounds] =>
@@ -599,7 +576,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
599576
newTypeVar(bound)
600577
case tp: RefinedType if tp.refinedInfo.isInstanceOf[TypeBounds] =>
601578
// Ideally, we would expect type inference can do the job
602-
// tp.derivedRefinedType(tp.parent, tp.refinedName, TypeAlias(newTypeVar(tp.refinedInfo.bounds)))
579+
// tests/patmat/t9657.scala
603580
expose(tp)
604581
case _ =>
605582
mapOver(t)
@@ -753,51 +730,50 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
753730
}
754731

755732

756-
/** Expose type to eliminate reference to type parameters
733+
/** Eliminate reference to type parameters in refinements
757734
*
758735
* A <: X :> Y B <: U :> V M { type T <: A :> B } ~~> M { type T <: X :> V }
759736
*/
760-
def expose(tp: Type, up: Boolean = true): Type = {
761-
tp match {
762-
case tp: AppliedType =>
763-
tp.derivedAppliedType(expose(tp.tycon, up), tp.args.map(expose(_, up)))
737+
def expose(tp: Type, refineCtx: Boolean = false, up: Boolean = true): Type = tp match {
738+
case tp: AppliedType =>
739+
tp.derivedAppliedType(expose(tp.tycon, refineCtx, up), tp.args.map(expose(_, refineCtx, up)))
764740

765-
case tp: TypeAlias =>
766-
val hi = expose(tp.alias, true)
767-
val lo = expose(tp.alias, false)
741+
case tp: TypeAlias =>
742+
val hi = expose(tp.alias, refineCtx, up)
743+
val lo = expose(tp.alias, refineCtx, up)
768744

769-
if (hi =:= lo)
770-
tp.derivedTypeAlias(hi)
771-
else
772-
tp.derivedTypeBounds(lo, hi)
773-
774-
case tp @ TypeBounds(lo, hi) =>
775-
tp.derivedTypeBounds(expose(lo, false), expose(hi, true))
776-
777-
case tp: RefinedType =>
778-
tp.derivedRefinedType(
779-
expose(tp.parent),
780-
tp.refinedName,
781-
expose(tp.refinedInfo)
782-
)
783-
case tp: TypeProxy =>
784-
tp.underlying match {
785-
case TypeBounds(lo, hi) =>
786-
expose(if (up) hi else lo, up)
787-
case _ =>
788-
tp
789-
}
745+
if (hi =:= lo)
746+
tp.derivedTypeAlias(hi)
747+
else
748+
tp.derivedTypeBounds(lo, hi)
790749

791-
case OrType(tp1, tp2) =>
792-
OrType(expose(tp1, up), expose(tp2, up))
750+
case tp @ TypeBounds(lo, hi) =>
751+
tp.derivedTypeBounds(expose(lo, refineCtx, false), expose(hi, refineCtx, true))
793752

794-
case AndType(tp1, tp2) =>
795-
AndType(expose(tp1, up), expose(tp2, up))
753+
case tp: RefinedType =>
754+
tp.derivedRefinedType(
755+
expose(tp.parent),
756+
tp.refinedName,
757+
expose(tp.refinedInfo, true, up)
758+
)
759+
case tp: TypeProxy if refineCtx =>
760+
tp.underlying match {
761+
case TypeBounds(lo, hi) =>
762+
expose(if (up) hi else lo, refineCtx, up)
763+
case _ =>
764+
tp
765+
}
796766

797-
case _ => tp
798-
}
767+
case OrType(tp1, tp2) =>
768+
OrType(expose(tp1, refineCtx, up), expose(tp2, refineCtx, up))
769+
770+
case AndType(tp1, tp2) =>
771+
AndType(expose(tp1, refineCtx, up), expose(tp2, refineCtx, up))
772+
773+
case _ => tp
799774
}
800775

776+
801777
def checkExhaustivity(_match: Match): Unit = {
802778
val Match(sel, cases) = _match
803779
val selTyp = sel.tpe.widen.dealias

0 commit comments

Comments
 (0)