Skip to content

Commit f20ef43

Browse files
i10416WojciechMazur
authored andcommitted
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`. [Cherry-picked e236cdd]
1 parent 2296b60 commit f20ef43

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
@@ -934,7 +934,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
934934
* (x: T) to (x @ (w: T)). This is either `_` or `_*`.
935935
*/
936936
def cases(ifPat: => Tree, ifExpr: => Tree, wildName: TermName) = tree.expr match {
937-
case id: untpd.Ident if (ctx.mode is Mode.Pattern) && untpd.isVarPattern(id) =>
937+
case id: untpd.Ident if (ctx.mode is Mode.Pattern) =>
938938
if (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) ifPat
939939
else {
940940
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)