Skip to content

Commit 14e8847

Browse files
committed
Drop special single-case syntax for match
- A match usually has more than one case anyway - That way, we increase the precedence of `match`, since what follows is always atomic.
1 parent d6b1762 commit 14e8847

File tree

4 files changed

+14
-24
lines changed

4 files changed

+14
-24
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,20 +1951,20 @@ object Parsers {
19511951
mkIf(cond, thenp, elsep)
19521952
}
19531953

1954-
/** `match' (`{' CaseClauses `}' | CaseClause)
1954+
/** `match' `{' CaseClauses `}'
19551955
*/
19561956
def matchExpr(t: Tree, start: Offset, mkMatch: (Tree, List[CaseDef]) => Match) =
19571957
in.endMarkerScope(MATCH) {
19581958
atSpan(start, in.skipToken()) {
1959-
mkMatch(t, casesExpr(caseClause))
1959+
mkMatch(t, inBracesOrIndented(caseClauses(caseClause)))
19601960
}
19611961
}
19621962

1963-
/** `match' (`{' TypeCaseClauses `}' | TypeCaseClause)
1963+
/** `match' `{' TypeCaseClauses `}'
19641964
*/
19651965
def matchType(t: Tree): MatchTypeTree =
19661966
atSpan(t.span.start, accept(MATCH)) {
1967-
MatchTypeTree(EmptyTree, t, casesExpr(typeCaseClause))
1967+
MatchTypeTree(EmptyTree, t, inBracesOrIndented(caseClauses(typeCaseClause)))
19681968
}
19691969

19701970
/** FunParams ::= Bindings
@@ -2395,10 +2395,6 @@ object Parsers {
23952395
}
23962396
}
23972397

2398-
def casesExpr(clause: () => CaseDef): List[CaseDef] =
2399-
if in.token == CASE then clause() :: Nil
2400-
else inBracesOrIndented(caseClauses(clause))
2401-
24022398
/** CaseClauses ::= CaseClause {CaseClause}
24032399
* TypeCaseClauses ::= TypeCaseClause {TypeCaseClause}
24042400
*/

docs/docs/internals/syntax.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ FunArgTypes ::= InfixType
147147
| ‘(’ [ ‘[given]’ FunArgType {‘,’ FunArgType } ] ‘)’
148148
| ‘(’ ‘[given]’ TypedFunParam {‘,’ TypedFunParam } ‘)’
149149
TypedFunParam ::= id ‘:’ Type
150-
MatchType ::= InfixType `match`
151-
(‘{’ TypeCaseClauses ‘}’ | TypeCaseClause)
150+
MatchType ::= InfixType `match` ‘{’ TypeCaseClauses ‘}’
152151
InfixType ::= RefinedType {id [nl] RefinedType} InfixOp(t1, op, t2)
153152
RefinedType ::= WithType {[nl | ‘with’] Refinement} RefinedTypeTree(t, ds)
154153
WithType ::= AnnotType {‘with’ AnnotType} (deprecated)
@@ -199,8 +198,7 @@ Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl}
199198
| [SimpleExpr ‘.’] id ‘=’ Expr Assign(expr, expr)
200199
| SimpleExpr1 ArgumentExprs ‘=’ Expr Assign(expr, expr)
201200
| Expr2
202-
| [‘inline’] Expr2 ‘match’
203-
(‘{’ CaseClauses ‘}’ | CaseClause) Match(expr, cases) -- point on match
201+
| [‘inline’] Expr2 ‘match’ ‘{’ CaseClauses ‘}’ Match(expr, cases) -- point on match
204202
Expr2 ::= PostfixExpr [Ascription]
205203
Ascription ::= ‘:’ InfixType Typed(expr, tp)
206204
| ‘:’ Annotation {Annotation} Typed(expr, Annotated(EmptyTree, annot)*)

tests/pos/i6997c.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import scala.quoted.{_, given}, scala.quoted.matching._
44

55
inline def mcr(x: => Any): Any = ${mcrImpl('x)}
66

7-
def mcrImpl(body: Expr[Any])(given ctx: QuoteContext): Expr[Any] = {
8-
body match case '{$x: $t} =>
9-
'{
10-
val tmp: $t = $x
11-
println(tmp)
12-
tmp
13-
}
14-
}
7+
def mcrImpl(body: Expr[Any])(given ctx: QuoteContext): Expr[Any] =
8+
body match
9+
case '{$x: $t} =>
10+
'{
11+
val tmp: $t = $x
12+
println(tmp)
13+
tmp
14+
}

tests/pos/single-case.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
object test with
22

3-
type T[X] = X match case Int => X
4-
53
try
64
println("hi")
75
catch case ex: java.io.IOException => println("ho")
86

9-
1 match case x: Int => println(x)
10-
117

0 commit comments

Comments
 (0)