Skip to content

Commit f7e0512

Browse files
Allow single sub case without new line
1 parent 4da6103 commit f7e0512

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,8 +2595,10 @@ object Parsers {
25952595
/** SubMatchClause ::= `match' `{' CaseClauses `}'
25962596
*/
25972597
def subMatchClause(t: Tree): SubMatch = atSpan(startOffset(t), accept(MATCH)):
2598-
val cases = inBracesOrIndented(caseClauses(() => caseClause()))
2599-
if !in.isNestedEnd then acceptStatSep() // else is sub sub match
2598+
val cases =
2599+
if in.token == CASE
2600+
then caseClause(exprOnly = true) :: Nil // single sub case without new line
2601+
else inBracesOrIndented(caseClauses(() => caseClause()))
26002602
SubMatch(t, cases)
26012603

26022604
/** `match' <<< TypeCaseClauses >>>
@@ -3109,7 +3111,9 @@ object Parsers {
31093111
}
31103112
val body =
31113113
if in.token == WITH && in.featureEnabled(Feature.subCases) then atSpan(in.skipToken()):
3112-
subMatchClause(simpleExpr(Location.ElseWhere))
3114+
val t = subMatchClause(simpleExpr(Location.ElseWhere))
3115+
if in.isStatSep then in.nextToken() // else may have been consumed by sub sub match
3116+
t
31133117
else atSpan(accept(ARROW)):
31143118
if exprOnly then
31153119
if in.indentSyntax && in.isAfterLineEnd && in.token != INDENT then
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import scala.language.experimental.subCases
2+
3+
// single sub case can be one the same line as outer case
4+
object Test:
5+
val x: Option[Option[Int]] = ???
6+
x match
7+
case Some(x2) with x2 match case Some(x3) => "aa"
8+
case Some(x2) if false with x2 match case Some(x3) if true => "aa"
9+
case Some(x2) with x2 match case Some(x3) with x2 match case Some(x3) => "bb"
10+
case Some(y2) with y2 match
11+
case Some(y3) with y3 match
12+
case 1 => "a"
13+
case 2 => "b"
14+
case Some(x2) with x2 match case Some(x3) with x3 match
15+
case 1 => "a"
16+
case 2 => "b"
17+
case None => "d"

0 commit comments

Comments
 (0)