Skip to content

Commit 853f300

Browse files
committed
Syntax change: streamline ascriptions in match scrutinees
Allow ascriptions in match scrutinees without requiring parentheses.
1 parent 3db347f commit 853f300

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,14 +1200,15 @@ object Parsers {
12001200
* | ForExpr
12011201
* | [SimpleExpr `.'] id `=' Expr
12021202
* | SimpleExpr1 ArgumentExprs `=' Expr
1203-
* | PostfixExpr [Ascription]
1204-
* | [‘inline’] PostfixExpr `match' `{' CaseClauses `}'
1203+
* | Expr2
1204+
* | [‘inline’] Expr2 `match' `{' CaseClauses `}'
12051205
* | `implicit' `match' `{' ImplicitCaseClauses `}'
1206-
* Bindings ::= `(' [Binding {`,' Binding}] `)'
1207-
* Binding ::= (id | `_') [`:' Type]
1208-
* Ascription ::= `:' CompoundType
1209-
* | `:' Annotation {Annotation}
1210-
* | `:' `_' `*'
1206+
* Bindings ::= `(' [Binding {`,' Binding}] `)'
1207+
* Binding ::= (id | `_') [`:' Type]
1208+
* Expr2 ::= PostfixExpr [Ascription]
1209+
* Ascription ::= `:' InfixType
1210+
* | `:' Annotation {Annotation}
1211+
* | `:' `_' `*'
12111212
*/
12121213
val exprInParens: () => Tree = () => expr(Location.InParens)
12131214

@@ -1324,7 +1325,8 @@ object Parsers {
13241325
t
13251326
}
13261327
case COLON =>
1327-
ascription(t, location)
1328+
val t1 = ascription(t, location)
1329+
if (in.token == MATCH) expr1Rest(t1, location) else t1
13281330
case MATCH =>
13291331
matchExpr(t, startOffset(t), Match)
13301332
case _ =>

docs/docs/internals/syntax.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,10 @@ Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl}
196196
| ForExpr
197197
| [SimpleExpr ‘.’] id ‘=’ Expr Assign(expr, expr)
198198
| SimpleExpr1 ArgumentExprs ‘=’ Expr Assign(expr, expr)
199-
| PostfixExpr [Ascription]
200-
| [‘inline’] PostfixExpr ‘match’ ‘{’ CaseClauses ‘}’ Match(expr, cases) -- point on match
199+
| Expr2
200+
| [‘inline’] Expr2 ‘match’ ‘{’ CaseClauses ‘}’ Match(expr, cases) -- point on match
201201
| ‘implicit’ ‘match’ ‘{’ ImplicitCaseClauses ‘}’
202+
Expr2 ::= PostfixExpr [Ascription]
202203
Ascription ::= ‘:’ InfixType Typed(expr, tp)
203204
| ‘:’ Annotation {Annotation} Typed(expr, Annotated(EmptyTree, annot)*)
204205
Catches ::= ‘catch’ Expr
@@ -224,7 +225,7 @@ SimpleExpr1 ::= Literal
224225
Quoted ::= ‘'’ ‘{’ Block ‘}’
225226
| ‘'’ ‘[’ Type ‘]’
226227
ExprsInParens ::= ExprInParens {‘,’ ExprInParens}
227-
ExprInParens ::= PostfixExpr ‘:’ Type
228+
ExprInParens ::= PostfixExpr ‘:’ Type -- normal Expr allows only RefinedType here
228229
| Expr
229230
ParArgumentExprs ::= ‘(’ ExprsInParens ‘)’ exprs
230231
| ‘(’ [ExprsInParens ‘,’] PostfixExpr ‘:’ ‘_’ ‘*’ ‘)’ exprs :+ Typed(expr, Ident(wildcardStar))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
List(1: @unchecked, 2, 3): @unchecked match {
3+
case a :: as =>
4+
}
5+
}

0 commit comments

Comments
 (0)