Skip to content

Commit 2de8696

Browse files
committed
Unify aliases when taking a lub.
1 parent ac2ec4f commit 2de8696

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,23 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
12641264
case tp1: RefinedType =>
12651265
tp2 match {
12661266
case tp2: RefinedType if tp1.refinedName == tp2.refinedName =>
1267-
tp1.derivedRefinedType(tp1.parent & tp2.parent, tp1.refinedName, tp1.refinedInfo & tp2.refinedInfo)
1267+
// Given two refinements `T1 { X = S1 }` and `T2 { X = S2 }`, if `S1 =:= S2`
1268+
// (possibly by instantiating type parameters), rewrite to `T1 & T2 { X = S1 }`.
1269+
// Otherwise rewrite to `T1 & T2 { X B }` where `B` is the conjunction of
1270+
// the bounds of `X` in `T1` and `T2`.
1271+
// The first rule above is contentious because it cuts the constraint set.
1272+
// But without it we would replace the two aliases by
1273+
// `T { X >: S1 | S2 <: S1 & S2 }`, which looks weird and is probably
1274+
// not what's intended.
1275+
val rinfo1 = tp1.refinedInfo
1276+
val rinfo2 = tp2.refinedInfo
1277+
val parent = tp1.parent & tp2.parent
1278+
val rinfo =
1279+
if (rinfo1.isAlias && rinfo2.isAlias && isSameType(rinfo1, rinfo2))
1280+
rinfo1
1281+
else
1282+
rinfo1 & rinfo2
1283+
tp1.derivedRefinedType(parent, tp1.refinedName, rinfo)
12681284
case _ =>
12691285
NoType
12701286
}
@@ -1528,7 +1544,7 @@ class ExplainingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
15281544
}
15291545

15301546
override def addConstraint(param: PolyParam, bound: Type, fromBelow: Boolean): Boolean =
1531-
traceIndented(i"add constraint $param ${if (fromBelow) ">:" else "<:"} $bound $frozenConstraint") {
1547+
traceIndented(i"add constraint $param ${if (fromBelow) ">:" else "<:"} $bound $frozenConstraint, constraint = ${ctx.typerState.constraint}") {
15321548
super.addConstraint(param, bound, fromBelow)
15331549
}
15341550

0 commit comments

Comments
 (0)