Skip to content

Commit 53a7827

Browse files
committed
Space: dedupe into intersectUnrelatedAtomicTypes
1 parent 5a21922 commit 53a7827

File tree

1 file changed

+8
-12
lines changed
  • compiler/src/dotty/tools/dotc/transform/patmat

1 file changed

+8
-12
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,25 +187,19 @@ object SpaceEngine {
187187
else if (isSubType(tp2, tp1)) b
188188
else if (canDecompose(tp1)) tryDecompose1(tp1)
189189
else if (canDecompose(tp2)) tryDecompose2(tp2)
190-
else intersectUnrelatedAtomicTypes(tp1, tp2)
190+
else intersectUnrelatedAtomicTypes(tp1, tp2)(a)
191191
case (Typ(tp1, _), Prod(tp2, fun, ss)) =>
192192
if (isSubType(tp2, tp1)) b
193193
else if (canDecompose(tp1)) tryDecompose1(tp1)
194194
else if (isSubType(tp1, tp2)) a // problematic corner case: inheriting a case class
195-
else intersectUnrelatedAtomicTypes(tp1, tp2) match
196-
case Typ(tp, _) => Prod(tp, fun, ss)
197-
case sp => sp
195+
else intersectUnrelatedAtomicTypes(tp1, tp2)(b)
198196
case (Prod(tp1, fun, ss), Typ(tp2, _)) =>
199197
if (isSubType(tp1, tp2)) a
200198
else if (canDecompose(tp2)) tryDecompose2(tp2)
201199
else if (isSubType(tp2, tp1)) a // problematic corner case: inheriting a case class
202-
else intersectUnrelatedAtomicTypes(tp1, tp2) match
203-
case Typ(tp, _) => Prod(tp, fun, ss)
204-
case sp => sp
200+
else intersectUnrelatedAtomicTypes(tp1, tp2)(a)
205201
case (Prod(tp1, fun1, ss1), Prod(tp2, fun2, ss2)) =>
206-
if (!isSameUnapply(fun1, fun2)) intersectUnrelatedAtomicTypes(tp1, tp2) match
207-
case Typ(tp, _) => Prod(tp, fun1, ss1)
208-
case sp => sp
202+
if (!isSameUnapply(fun1, fun2)) intersectUnrelatedAtomicTypes(tp1, tp2)(a)
209203
else if (ss1.zip(ss2).exists(p => simplify(intersect(p._1, p._2)) == Empty)) Empty
210204
else Prod(tp1, fun1, ss1.zip(ss2).map((intersect _).tupled))
211205
}
@@ -323,13 +317,15 @@ object SpaceEngine {
323317
* The types should be atomic (non-decomposable) and unrelated (neither
324318
* should be a subtype of the other).
325319
*/
326-
def intersectUnrelatedAtomicTypes(tp1: Type, tp2: Type)(using Context): Space = trace(i"atomic intersection: ${AndType(tp1, tp2)}", debug) {
320+
def intersectUnrelatedAtomicTypes(tp1: Type, tp2: Type)(sp: Space)(using Context): Space = trace(i"atomic intersection: ${AndType(tp1, tp2)}", debug) {
327321
// Precondition: !isSubType(tp1, tp2) && !isSubType(tp2, tp1).
328322
if !ctx.mode.is(Mode.SafeNulls) && (tp1.isNullType || tp2.isNullType) then
329323
// Since projections of types don't include null, intersection with null is empty.
330324
Empty
331325
else
332-
val intersection = Typ(AndType(tp1, tp2), decomposed = false)
326+
val intersection = sp match
327+
case sp: Prod => sp.copy(AndType(tp1, tp2))
328+
case _ => Typ(AndType(tp1, tp2), decomposed = false)
333329
// unrelated numeric value classes can equal each other, so let's not consider type space intersection empty
334330
if tp1.classSymbol.isNumericValueClass && tp2.classSymbol.isNumericValueClass then intersection
335331
else if isPrimToBox(tp1, tp2) || isPrimToBox(tp2, tp1) then intersection

0 commit comments

Comments
 (0)