Skip to content

Commit 741e17e

Browse files
Fix #12260: Add underscore to match type syntax
1 parent 2b37257 commit 741e17e

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,12 +2610,19 @@ object Parsers {
26102610
})
26112611
}
26122612

2613-
/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [semi]
2613+
/** TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
26142614
*/
26152615
def typeCaseClause(): CaseDef = atSpan(in.offset) {
26162616
val pat = inSepRegion(InCase) {
26172617
accept(CASE)
2618-
infixType()
2618+
in.token match {
2619+
case USCORE if in.lookahead.isArrow =>
2620+
val start = in.skipToken()
2621+
typeBounds().withSpan(Span(start, in.lastOffset, start))
2622+
2623+
case _ =>
2624+
infixType()
2625+
}
26192626
}
26202627
CaseDef(pat, EmptyTree, atSpan(accept(ARROW)) {
26212628
val t = typ()

docs/docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ CaseClauses ::= CaseClause { CaseClause }
289289
CaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Block CaseDef(pat, guard?, block) // block starts at =>
290290
ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
291291
TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
292-
TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [semi]
292+
TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
293293
294294
Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
295295
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// `case _ => expr` in a match expression should be equivalant to
2+
// `case _: Any => expr`. Likewise, in a match type, `case _ => T`
3+
// should be equivalant to `case Any => T`.
4+
5+
object Test0 {
6+
type M[X] = X match { case String => Int case Any => String }
7+
def m[X](x: X): M[X] = x match { case _: String => 1 case _: Any => "s" }
8+
}
9+
10+
object Test2 {
11+
type M[X] = X match { case String => Int case _ => String }
12+
def m[X](x: X): M[X] = x match { case _: String => 1 case _: Any => "s" }
13+
}

0 commit comments

Comments
 (0)