Skip to content

Commit e236cdd

Browse files
committed
fix(#15784): ident rule for pat match was too strict
close #15784 Scala 2 allows backticked identifier and capital identifier in pattern match, but Scala 3 mistakenly prohibited them. For example, the following code is valid in Scala 2, ```scala List(42) match { case List(_, Rest @ _*) => Rest case List(_, `Rest` @ _*) => `Rest` _ => ??? } ``` whereas it resulted in `Not Found Rest` error in Scala 3. This is because the condition to detect wildcard pattern was so strict that it chose the wrong match arm; `case _ => ifExpr`.
1 parent a9a75c1 commit e236cdd

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
944944
* (x: T) to (x @ (w: T)). This is either `_` or `_*`.
945945
*/
946946
def cases(ifPat: => Tree, ifExpr: => Tree, wildName: TermName) = tree.expr match {
947-
case id: untpd.Ident if (ctx.mode is Mode.Pattern) && untpd.isVarPattern(id) =>
947+
case id: untpd.Ident if (ctx.mode is Mode.Pattern) =>
948948
if (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) ifPat
949949
else {
950950
import untpd.*

tests/neg/i15784.check

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- [E006] Not Found Error: tests/neg/i15784.scala:2:26 -----------------------------------------------------------------
2+
2 | case List(_, Rest @ `a`) => Rest // error
3+
| ^^^
4+
| Not found: a
5+
|
6+
| longer explanation available when compiling with `-explain`
7+
-- [E006] Not Found Error: tests/neg/i15784.scala:3:26 -----------------------------------------------------------------
8+
3 | case List(_, Rest @ A) => Rest // error
9+
| ^
10+
| Not found: A
11+
|
12+
| longer explanation available when compiling with `-explain`

tests/neg/i15784.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def i15784 = List(42) match
2+
case List(_, Rest @ `a`) => Rest // error
3+
case List(_, Rest @ A) => Rest // error
4+
case _ => ???

tests/pos/i15784.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def i15784 = List(42) match
2+
case List(_, rest @ _*) => rest
3+
case List(_, Rest @ _*) => Rest
4+
case List(_, `Rest` @ _*) => Rest
5+
case _ => ???
6+
7+
def i15784_auxiliary = 42 match
8+
case `type` : Int => `type`

0 commit comments

Comments
 (0)