Skip to content

Commit aa897e4

Browse files
authored
Fix/dont match on unit (#468)
2 parents 3ea4d3f + d493127 commit aa897e4

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

effekt/shared/src/main/scala/effekt/core/PatternMatchingCompiler.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ object PatternMatchingCompiler {
124124
}
125125

126126
// (3a) Match on a literal
127-
def splitOnLiteral(lit: Literal, equals: (Pure, Pure) => Pure) = {
127+
def splitOnLiteral(lit: Literal, equals: (Pure, Pure) => Pure): core.Stmt = {
128128
// the different literal values that we match on
129129
val variants: List[core.Literal] = normalized.collect {
130130
case Clause(Split(Pattern.Literal(lit, _), _, _), _, _) => lit
@@ -148,6 +148,10 @@ object PatternMatchingCompiler {
148148
variants.foreach { v => addClause(v, c) }
149149
}
150150

151+
// special case matching on ()
152+
val unit: Literal = Literal((), core.Type.TUnit)
153+
if (lit == unit) return compile(clausesFor.getOrElse(unit, Nil))
154+
151155
// (4) assemble syntax tree for the pattern match
152156
variants.foldRight(compile(defaults)) {
153157
case (lit, elsStmt) =>

effekt/shared/src/main/scala/effekt/core/Tree.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,12 @@ object normal {
400400
clauses.collectFirst { case (tag, lit) if tag == ctorTag => lit }
401401
.map(body => app(body, Nil, vargs, Nil))
402402
.orElse { default }.getOrElse { sys error "Pattern not exhaustive. This should not happen" }
403-
case other =>
404-
Match(scrutinee, clauses, default)
403+
case other => (clauses, default) match {
404+
// Unit-like types: there is only one case and it is just a tag.
405+
// sc match { case Unit() => body } ==> body
406+
case ((id, lit) :: Nil, None) if lit.vparams.isEmpty => lit.body
407+
case _ => Match(scrutinee, clauses, default)
408+
}
405409
}
406410

407411

0 commit comments

Comments
 (0)