Skip to content

Commit 6665dcf

Browse files
Keep opaques in match type scrutinee
Fixes scala#19326
1 parent 43ed9fd commit 6665dcf

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3488,7 +3488,7 @@ class MatchReducer(initctx: Context) extends TypeComparer(initctx) {
34883488
false
34893489

34903490
case MatchTypeCasePattern.AbstractTypeConstructor(tycon, argPatterns) =>
3491-
scrut.dealias match
3491+
scrut.dealiasKeepOpaques match
34923492
case scrutDealias @ AppliedType(scrutTycon, args) if scrutTycon =:= tycon =>
34933493
matchArgs(argPatterns, args, tycon.typeParams, scrutIsWidenedAbstract)
34943494
case _ =>

tests/pos/i19326.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
import OpaqueScope.Opaque
3+
4+
type MapSum[D1, D2] = D1 match
5+
case Opaque[l1] => D2 match
6+
case Opaque[l2] => Opaque[Sum[l1, l2]]
7+
8+
object OpaqueScope:
9+
opaque type Opaque[N <: NatT] = Double
10+
11+
val foo: MapSum[Opaque[Succ[Zero]], Opaque[Zero]] = 1.0
12+
end OpaqueScope
13+
14+
sealed trait NatT derives CanEqual
15+
case class Zero() extends NatT
16+
case class Succ[N <: NatT](n: N) extends NatT
17+
18+
type Sum[M <: NatT, N <: NatT] <: NatT = (M, N) match
19+
case (_, Zero) => M
20+
case (Zero, _) => N
21+
case (Succ[predM], Succ[predN]) => Succ[Succ[Sum[predM, predN]]]
22+
23+
24+
object Minimized:
25+
26+
object OpaqueScope:
27+
opaque type Opaque[N <: Any] = Double
28+
val foo: StripOpaque[Opaque[Int]] = 1
29+
30+
// Note that match types within OpaqueScope are not limited by the absence of dealiasing
31+
type NeedsRhs[D] = D match
32+
case Double => String
33+
val bar: NeedsRhs[Opaque[Float]] = "hello"
34+
35+
type StripOpaque[D] = D match
36+
case OpaqueScope.Opaque[x] => x
37+

0 commit comments

Comments
 (0)