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 Apr 28, 2021
1 parent 3d8b6a1 commit c4142c6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2574,12 +2574,18 @@ object Parsers {
})
}

/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
/** TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [nl]
*/
def typeCaseClause(): CaseDef = atSpan(in.offset) {
val pat = inSepRegion(InCase) {
accept(CASE)
infixType()
in.token match {
case USCORE if in.lookahead.isArrow =>
in.skipToken()
scalaAny
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 @@ -285,7 +285,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 [nl]
TypeCaseClause ::= ‘case’ (InfixType | ‘_’) ‘=>’ Type [nl]
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 c4142c6

Please sign in to comment.