Skip to content

Commit

Permalink
Fix scala#12260: Add underscore to match type syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierBlanvillain committed Sep 22, 2021
1 parent 2784596 commit b7217f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
11 changes: 9 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2607,12 +2607,19 @@ object Parsers {
})
}

/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [semi]
/** TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
*/
def typeCaseClause(): CaseDef = atSpan(in.offset) {
val pat = inSepRegion(InCase) {
accept(CASE)
infixType()
in.token match {
case USCORE if in.lookahead.isArrow =>
val start = in.skipToken()
typeBounds().withSpan(Span(start, in.lastOffset, start))

case _ =>
infixType()
}
}
CaseDef(pat, EmptyTree, atSpan(accept(ARROW)) {
val t = typ()
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ CaseClauses ::= CaseClause { CaseClause }
CaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Block CaseDef(pat, guard?, block) // block starts at =>
ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [semi]
TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [semi]
Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
Expand Down
13 changes: 13 additions & 0 deletions tests/pos/unify-wildcard-patterns.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// `case _ => expr` in a match expression should be equivalant to
// `case _: Any => expr`. Likewise, in a match type, `case _ => T`
// should be equivalant to `case Any => T`.

object Test0 {
type M[X] = X match { case String => Int case Any => String }
def m[X](x: X): M[X] = x match { case _: String => 1 case _: Any => "s" }
}

object Test2 {
type M[X] = X match { case String => Int case _ => String }
def m[X](x: X): M[X] = x match { case _: String => 1 case _: Any => "s" }
}

0 comments on commit b7217f1

Please sign in to comment.