Skip to content
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

Scala 3 does not take arbitrary id in pattern @ binding #15784

Closed
som-snytt opened this issue Jul 28, 2022 · 3 comments · Fixed by #19501
Closed

Scala 3 does not take arbitrary id in pattern @ binding #15784

som-snytt opened this issue Jul 28, 2022 · 3 comments · Fixed by #19501

Comments

@som-snytt
Copy link
Contributor

Compiler version

3.1.3

Minimized code

Welcome to Scala 3.1.3 (18.0.1.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> List(42) match { case List(h, rest @ _*) => (h, rest) case _ => }
val res0: Matchable = (42,List())

scala> List(42) match { case List(h, Rest @ _*) => (h, Rest) case _ => }
-- [E006] Not Found Error: ---------------------------------------------------------------------------------------------
1 |List(42) match { case List(h, Rest @ _*) => (h, Rest) case _ => }
  |                              ^^^^
  |                              Not found: Rest
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: ---------------------------------------------------------------------------------------------
1 |List(42) match { case List(h, Rest @ _*) => (h, Rest) case _ => }
  |                                                ^^^^
  |                                                Not found: Rest
  |
  | longer explanation available when compiling with `-explain`
2 errors found

Expectation

Welcome to Scala 2.13.8 (OpenJDK 64-Bit Server VM, Java 18.0.1.1).
Type in expressions for evaluation. Or try :help.

scala> List(42) match { case List(h, rest @ _*) => (h, rest) case _ => }
val res0: Any = (42,List())

scala> List(42) match { case List(h, Rest @ _*) => (h, Rest) case _ => }
val res1: Any = (42,List())

scala> List(42) match { case List(h, Nil @ _*) => (h, Nil) case _ => }
val res2: Any = (42,List())

scala> List(42) match { case List(h, `Nil` @ _*) => (h, Nil) case _ => }
val res3: Any = (42,List())

scala> List(42) match { case List(h, `other things` @ _*) => (h, `other things`) case _ => }
val res4: Any = (42,List())

This syntax upgrade was introduced in scala/scala#4935

@som-snytt som-snytt added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 28, 2022
@WojciechMazur
Copy link
Contributor

@odersky Do you think the change introduced to Scala2 was intended and should we port it to Scala 3? In my opinion, current behavior is an improvement as we typically expected capitalized names to be a constant value.

@som-snytt
Copy link
Contributor Author

som-snytt commented Aug 4, 2022

The pre-dotty ticket was scala/bug#8044

The original use case was generated code, but the idea is that in id @ the id is just an id not a pattern. It's also useful for real people to introduce names such as type and Type.

Then x @ _: X is not simply x: X where varid is required. Scala 2 warnings also treat x @ _ syntax as special to mean don't warn if the x is unused, to treat the x as documentary.

Scala 2 also accepts backticked varid:

42 match { case `type` : Int => `type` }

as opposed to

scala> 42 match { case `Junk`: Int => Junk }
                             ^
       error: Pattern variables must start with a lower-case letter. (SLS 8.1.1.)

Test file at https://github.com/scala/scala/blob/2.13.x/test/files/pos/t8044.scala

@odersky
Copy link
Contributor

odersky commented Jul 12, 2023

This was not intentional. We should keep the Scala 2 rules here.

@nicolasstucki nicolasstucki added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label stat:needs spec labels Jul 12, 2023
@som-snytt som-snytt self-assigned this Oct 16, 2023
i10416 added a commit to i10416/dotty that referenced this issue Jan 21, 2024
close scala#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`.
@som-snytt som-snytt removed their assignment Jan 22, 2024
joroKr21 pushed a commit to joroKr21/dotty that referenced this issue Jan 23, 2024
close scala#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`.
@Kordyjan Kordyjan added this to the 3.4.1 milestone Feb 14, 2024
WojciechMazur pushed a commit that referenced this issue Jun 27, 2024
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]
WojciechMazur added a commit that referenced this issue Jun 28, 2024
#20848)

Backports #19501 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants