Skip to content

Commit 3b86dc9

Browse files
Handle partial functions with sub cases
1 parent 3e89455 commit 3b86dc9

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

compiler/src/dotty/tools/dotc/transform/ExpandSAMs.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ class ExpandSAMs extends MiniPhase:
180180

181181
def isDefinedAtRhs(paramRefss: List[List[Tree]])(using Context) =
182182
val tru = Literal(Constant(true))
183-
def translateCase(cdef: CaseDef) = cpy.CaseDef(cdef)(body = tru)
183+
def translateCase(cdef: CaseDef): CaseDef =
184+
val body1 = cdef.body match
185+
case b: SubMatch => cpy.Match(b)(b.selector, b.cases.map(translateCase))
186+
case _ => tru
187+
cpy.CaseDef(cdef)(body = body1)
184188
val paramRef = paramRefss.head.head
185189
val defaultValue = Literal(Constant(false))
186190
translateMatch(isDefinedAtFn)(paramRef.symbol, pfRHS.cases.map(translateCase), defaultValue)

tests/run/pf-sub-cases.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.language.experimental.matchWithSubCases
2+
3+
val pf: PartialFunction[Option[Option[Int]], String] =
4+
case Some(x2) with x2 match
5+
case Some(x3) with x3 match
6+
case 1 => "a"
7+
case 2 => "b"
8+
case Some(None) => "c"
9+
10+
@main def Test =
11+
assert(pf(Some(Some(2))) == "b")
12+
assert(pf(Some(None)) == "c")
13+
assert(!pf.isDefinedAt(None))
14+
assert(!pf.isDefinedAt(Some(Some(3))))

0 commit comments

Comments
 (0)