Skip to content

Fix #12260: Add underscore to match type syntax #12261

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 7 commits into from
Jan 23, 2022
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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/MatchTypeTrace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ object MatchTypeTrace:

private def caseText(tp: Type)(using Context): String = tp match
case tp: HKTypeLambda => caseText(tp.resultType)
case defn.MatchCase(any, body) if any eq defn.AnyType => i"case _ => $body"
case defn.MatchCase(pat, body) => i"case $pat => $body"
case _ => i"case $tp"

Expand Down
14 changes: 10 additions & 4 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1824,7 +1824,7 @@ object Parsers {

def typeDependingOn(location: Location): Tree =
if location.inParens then typ()
else if location.inPattern then refinedType()
else if location.inPattern then rejectWildcardType(refinedType())
else infixType()

/* ----------- EXPRESSIONS ------------------------------------------------ */
Expand Down Expand Up @@ -2610,15 +2610,21 @@ 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()
Ident(tpnme.WILDCARD).withSpan(Span(start, in.lastOffset, start))
case _ =>
rejectWildcardType(infixType())
}
}
CaseDef(pat, EmptyTree, atSpan(accept(ARROW)) {
val t = typ()
val t = rejectWildcardType(typ())
if in.token == SEMI then in.nextToken()
newLinesOptWhenFollowedBy(CASE)
t
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ class Typer extends Namer
if ctx.mode.is(Mode.Pattern) then
if name == nme.WILDCARD then
return tree.withType(pt)
if name == tpnme.WILDCARD then
return tree.withType(defn.AnyType)
if untpd.isVarPattern(tree) && name.isTermName then
return typed(desugar.patternVar(tree), pt)
else if ctx.mode.is(Mode.QuotedPattern) then
Expand Down Expand Up @@ -1548,6 +1550,11 @@ class Typer extends Namer
case defn.MatchCase(patternTp, _) => tpt.tpe frozen_=:= patternTp
case _ => false
}
case (id @ Ident(nme.WILDCARD), pt) =>
pt match {
case defn.MatchCase(patternTp, _) => defn.AnyType frozen_=:= patternTp
case _ => false
}
case _ => false
}

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
15 changes: 15 additions & 0 deletions tests/neg/12261.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type M0[X] = X match {
case ? => String // error: Unbound wildcard type
}

type M1[X] = X match {
case Any => _ // error: Unbound wildcard type
}

type M2[X] = X match {
case Any => ? // error: Unbound wildcard type
}

val a = "" match { case _: _ => () } // error: Unbound wildcard type

val b = try { } catch { case _: _ => () } // error: Unbound wildcard type
2 changes: 1 addition & 1 deletion tests/pos/9239.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ object ABug:
N match
case Zero => One
case One => One
case ? => ![--[N]] × (N)
case _ => ![--[N]] × (N)
case ? :: ? => ![--[N]] × (N)
23 changes: 23 additions & 0 deletions tests/pos/unify-wildcard-patterns.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// `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 Test1 {
type M[X] = X match { case String => Int case Any => String }
def m[X](x: X): M[X] = x match { case _: String => 1 case _ => "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" }
}

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