Skip to content

Fix #8096: Change syntax of single-case try #8121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,7 @@ object Parsers {
* Ascription ::= `:' InfixType
* | `:' Annotation {Annotation}
* | `:' `_' `*'
* Catches ::= ‘catch’ (Expr | ExprCaseClause)
*/
val exprInParens: () => Tree = () => expr(Location.InParens)

Expand Down Expand Up @@ -1946,7 +1947,7 @@ object Parsers {
if in.token == CATCH then
val span = in.offset
in.nextToken()
(if in.token == CASE then Match(EmptyTree, caseClause() :: Nil)
(if in.token == CASE then Match(EmptyTree, caseClause(exprOnly = true) :: Nil)
else subExpr(),
span)
else (EmptyTree, -1)
Expand Down Expand Up @@ -2072,7 +2073,7 @@ object Parsers {
def matchClause(t: Tree): Match =
in.endMarkerScope(MATCH) {
atSpan(t.span.start, in.skipToken()) {
Match(t, inBracesOrIndented(caseClauses(caseClause)))
Match(t, inBracesOrIndented(caseClauses(() => caseClause())))
}
}

Expand Down Expand Up @@ -2366,7 +2367,7 @@ object Parsers {
def blockExpr(): Tree = atSpan(in.offset) {
val simplify = in.token == INDENT
inDefScopeBraces {
if (in.token == CASE) Match(EmptyTree, caseClauses(caseClause))
if (in.token == CASE) Match(EmptyTree, caseClauses(() => caseClause()))
else block(simplify)
}
}
Expand Down Expand Up @@ -2537,18 +2538,21 @@ object Parsers {
}

/** CaseClause ::= ‘case’ Pattern [Guard] `=>' Block
* ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
*/
val caseClause: () => CaseDef = () => atSpan(in.offset) {
def caseClause(exprOnly: Boolean = false): CaseDef = atSpan(in.offset) {
val (pat, grd) = inSepRegion(LPAREN, RPAREN) {
accept(CASE)
(pattern(), guard())
}
CaseDef(pat, grd, atSpan(accept(ARROW)) { block() })
CaseDef(pat, grd, atSpan(accept(ARROW)) {
if exprOnly then expr() else block()
})
}

/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
*/
val typeCaseClause: () => CaseDef = () => atSpan(in.offset) {
def typeCaseClause(): CaseDef = atSpan(in.offset) {
val pat = inSepRegion(LPAREN, RPAREN) {
accept(CASE)
infixType()
Expand Down
5 changes: 3 additions & 2 deletions docs/docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[
| ‘inline’ InfixExpr MatchClause
Ascription ::= ‘:’ InfixType Typed(expr, tp)
| ‘:’ Annotation {Annotation} Typed(expr, Annotated(EmptyTree, annot)*)
Catches ::= ‘catch’ (Expr | CaseClause)
Catches ::= ‘catch’ (Expr | ExprCaseClause)
PostfixExpr ::= InfixExpr [id] PostfixOp(expr, op)
InfixExpr ::= PrefixExpr
| InfixExpr id [nl] InfixExpr InfixOp(expr, op, expr)
Expand Down Expand Up @@ -256,7 +256,8 @@ Generator ::= [‘case’] Pattern1 ‘<-’ Expr
Guard ::= ‘if’ PostfixExpr

CaseClauses ::= CaseClause { CaseClause } Match(EmptyTree, cases)
CaseClause ::= ‘case’ (Pattern [Guard] ‘=>’ Block | INT) CaseDef(pat, guard?, block) // block starts at =>
CaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Block CaseDef(pat, guard?, block) // block starts at =>
ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
ImplicitCaseClauses ::= ImplicitCaseClause { ImplicitCaseClause }
ImplicitCaseClause ::= ‘case’ PatVar [‘:’ RefinedType] [Guard] ‘=>’ Block
TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
Expand Down
10 changes: 10 additions & 0 deletions tests/run/i8096.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def k: String =
try
"WUT"
catch case t =>
println(s"Caught a case of $t")
"OK"

@main def Test =
assert(k == "OK")