Skip to content

Commit aaf79ff

Browse files
committed
Fix #8096: Change syntax of single-case try
1 parent 51b1f2c commit aaf79ff

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,7 @@ object Parsers {
18631863
* Ascription ::= `:' InfixType
18641864
* | `:' Annotation {Annotation}
18651865
* | `:' `_' `*'
1866+
* Catches ::= ‘catch’ (Expr | ExprCaseClause)
18661867
*/
18671868
val exprInParens: () => Tree = () => expr(Location.InParens)
18681869

@@ -1946,7 +1947,7 @@ object Parsers {
19461947
if in.token == CATCH then
19471948
val span = in.offset
19481949
in.nextToken()
1949-
(if in.token == CASE then Match(EmptyTree, caseClause() :: Nil)
1950+
(if in.token == CASE then Match(EmptyTree, caseClause(exprOnly = true) :: Nil)
19501951
else subExpr(),
19511952
span)
19521953
else (EmptyTree, -1)
@@ -2072,7 +2073,7 @@ object Parsers {
20722073
def matchClause(t: Tree): Match =
20732074
in.endMarkerScope(MATCH) {
20742075
atSpan(t.span.start, in.skipToken()) {
2075-
Match(t, inBracesOrIndented(caseClauses(caseClause)))
2076+
Match(t, inBracesOrIndented(caseClauses(() => caseClause())))
20762077
}
20772078
}
20782079

@@ -2366,7 +2367,7 @@ object Parsers {
23662367
def blockExpr(): Tree = atSpan(in.offset) {
23672368
val simplify = in.token == INDENT
23682369
inDefScopeBraces {
2369-
if (in.token == CASE) Match(EmptyTree, caseClauses(caseClause))
2370+
if (in.token == CASE) Match(EmptyTree, caseClauses(() => caseClause()))
23702371
else block(simplify)
23712372
}
23722373
}
@@ -2537,18 +2538,21 @@ object Parsers {
25372538
}
25382539

25392540
/** CaseClause ::= ‘case’ Pattern [Guard] `=>' Block
2541+
* ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
25402542
*/
2541-
val caseClause: () => CaseDef = () => atSpan(in.offset) {
2543+
def caseClause(exprOnly: Boolean = false): CaseDef = atSpan(in.offset) {
25422544
val (pat, grd) = inSepRegion(LPAREN, RPAREN) {
25432545
accept(CASE)
25442546
(pattern(), guard())
25452547
}
2546-
CaseDef(pat, grd, atSpan(accept(ARROW)) { block() })
2548+
CaseDef(pat, grd, atSpan(accept(ARROW)) {
2549+
if exprOnly then expr() else block()
2550+
})
25472551
}
25482552

25492553
/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
25502554
*/
2551-
val typeCaseClause: () => CaseDef = () => atSpan(in.offset) {
2555+
def typeCaseClause(): CaseDef = atSpan(in.offset) {
25522556
val pat = inSepRegion(LPAREN, RPAREN) {
25532557
accept(CASE)
25542558
infixType()

docs/docs/internals/syntax.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[
203203
| ‘inline’ InfixExpr MatchClause
204204
Ascription ::= ‘:’ InfixType Typed(expr, tp)
205205
| ‘:’ Annotation {Annotation} Typed(expr, Annotated(EmptyTree, annot)*)
206-
Catches ::= ‘catch’ (Expr | CaseClause)
206+
Catches ::= ‘catch’ (Expr | ExprCaseClause)
207207
PostfixExpr ::= InfixExpr [id] PostfixOp(expr, op)
208208
InfixExpr ::= PrefixExpr
209209
| InfixExpr id [nl] InfixExpr InfixOp(expr, op, expr)
@@ -256,7 +256,8 @@ Generator ::= [‘case’] Pattern1 ‘<-’ Expr
256256
Guard ::= ‘if’ PostfixExpr
257257
258258
CaseClauses ::= CaseClause { CaseClause } Match(EmptyTree, cases)
259-
CaseClause ::= ‘case’ (Pattern [Guard] ‘=>’ Block | INT) CaseDef(pat, guard?, block) // block starts at =>
259+
CaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Block CaseDef(pat, guard?, block) // block starts at =>
260+
ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
260261
ImplicitCaseClauses ::= ImplicitCaseClause { ImplicitCaseClause }
261262
ImplicitCaseClause ::= ‘case’ PatVar [‘:’ RefinedType] [Guard] ‘=>’ Block
262263
TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }

tests/run/i8096.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def k: String =
2+
try
3+
"WUT"
4+
catch case t =>
5+
println(s"Caught a case of $t")
6+
"OK"
7+
8+
@main def Test =
9+
assert(k == "OK")
10+

0 commit comments

Comments
 (0)