Skip to content

Commit 1c57fd8

Browse files
committed
Fix infix of match
`match` should have the same precedence as alphanumeric operators. This means that `a match { ... } + 1` should give a syntax error.
1 parent 122d759 commit 1c57fd8

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -983,9 +983,15 @@ object Parsers {
983983
}
984984
else
985985
val t = reduceStack(base, top, minPrec, leftAssoc = true, in.name, isType)
986-
if !isType && in.token == MATCH then recur(matchClause(t))
986+
if !isType && in.token == MATCH then recurAtMinPrec(matchClause(t))
987987
else t
988988

989+
def recurAtMinPrec(top: Tree): Tree =
990+
if isIdent && isOperator && precedence(in.name) == minInfixPrec
991+
|| in.token == MATCH
992+
then recur(top)
993+
else top
994+
989995
recur(first)
990996
}
991997

@@ -2758,8 +2764,8 @@ object Parsers {
27582764
/** OLD: GivenTypes ::= AnnotType {‘,’ AnnotType}
27592765
* NEW: GivenTypes ::= Type {‘,’ Type}
27602766
*/
2761-
def givenTypes(newStyle: Boolean, nparams: Int, ofClass: Boolean): List[ValDef] =
2762-
val tps = commaSeparated(() => if newStyle then typ() else annotType())
2767+
def givenTypes(nparams: Int, ofClass: Boolean): List[ValDef] =
2768+
val tps = commaSeparated(typ)
27632769
var counter = nparams
27642770
def nextIdx = { counter += 1; counter }
27652771
val paramFlags = if ofClass then Private | Local | ParamAccessor else Param
@@ -2862,7 +2868,7 @@ object Parsers {
28622868
|| startParamTokens.contains(in.token)
28632869
|| isIdent && (in.name == nme.inline || in.lookaheadIn(BitSet(COLON)))
28642870
if isParams then commaSeparated(() => param())
2865-
else givenTypes(true, nparams, ofClass)
2871+
else givenTypes(nparams, ofClass)
28662872
checkVarArgsRules(clause)
28672873
clause
28682874
}

tests/neg/match-infix.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def f = 1 + 1 match {
2+
case 2 => 3
3+
} + 1 // error // error

0 commit comments

Comments
 (0)