Skip to content

Pattern sequence 'Pattern2*' is allowed by grammar, but behaves strange #11457

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

Closed
SrTobi opened this issue Feb 18, 2021 · 4 comments · Fixed by #11508
Closed

Pattern sequence 'Pattern2*' is allowed by grammar, but behaves strange #11457

SrTobi opened this issue Feb 18, 2021 · 4 comments · Fixed by #11508

Comments

@SrTobi
Copy link
Contributor

SrTobi commented Feb 18, 2021

Compiler version

3.0.0-RC2-bin-20210217-83cb8ff-NIGHTLY

Minimized code

Seq(1, 2) match
    case Seq(x, y*) => println(y) // prints List(2) which looks correct

Seq(1, 2) match
  case Seq(x, (y)*) => println(y) // parses, but gives a compiler error that y in the pattern is not found 

val y = 3
Seq(1, 2) match
  case Seq(x, (y)*) => println(y) // compiles and outputs 3, which is curious because what did (y) match exactly then?

Output

Output for

Seq(1, 2) match
  case Seq(x, (y)*) => println(y) 
[error] -- [E006] Not Found Error: /dotty-test/src/main/scala/Main.scala:8:17 
[error] 8 |    case Seq(x, (y)*) => println(y)
[error]   |                 ^
[error]   |                 Not found: y
[error] -- [E006] Not Found Error: /dotty-test/src/main/scala/Main.scala:8:33 
[error] 8 |    case Seq(x, (y)*) => println(y)
[error]   |                                 ^
[error]   |                                 Not found: y
[error] two errors found
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed

Expectation

Not 100% sure what to expect... maybe something where the pattern matched multiple times?
It just looks only half implemented to me...

@liufengyun
Copy link
Contributor

liufengyun commented Feb 18, 2021

For simplicity and consistency with Scala 2, maybe it's better to restore the following restrictions:

  • In pat: T, restrict pat to a pattern variable
  • in pat*, restrict pat to a pattern variable

There are currently no known use cases for the new features. If there is a strong need, the feature can come after 3.0, as it does not change the existing syntax.

@SrTobi
Copy link
Contributor Author

SrTobi commented Feb 18, 2021

What is supposed to happen here anyway? Why did the definition change here from Scala 2 which only allowed id/_?

@sjrd
Copy link
Member

sjrd commented Feb 18, 2021

It seems to be an accident. There is nothing except a pattern variable, or an _, that could possibly make sense in that context.

It was probably a side effect of allowing patterns on the left of : T, where @odersky argued that it made sense and should not be reverted. But for * it makes no sense.

@liufengyun
Copy link
Contributor

The PR that introduced pat*:

#11240 (comment)

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