-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Restrict syntax of typed patterns #16150
Conversation
dwijnand
commented
Oct 7, 2022
•
edited
Loading
edited
- Fix nested named irrefutable typetest pattern causes exhaustivity warning #10994: align typed pattern syntax to Scala 2
- Fixes Erratic compiler behavior for inlined function definitions #15893
In Scala 2, a typed pattern `p: T` restricts that `p` can only be a pattern variable. In Dotty, scala#6919 allows `p` to be any pattern, in order to support pattern matching on generic number literals. This PR aligns the syntax with Scala 2 by stipulating that in a typed pattern `p: T`, either - `p` is a pattern variable, or - `p` is a number literal
The test case `tests/explicit-nulls/neg/strip.scala` specify that null unions inside intersection types should work. After changing the scrutinee type of the extractor `OrNull` it is no longer the case. Changing the scrutinee type is still justified because it agrees with the name as well as the usage in `Types.scala`. In contrast, in `Typer.scala`, the logic is more clear without using `OrNull`.
Co-authored-by: Jamie Thompson <[email protected]>
*/ | ||
def pattern1(location: Location = Location.InPattern): Tree = | ||
val p = pattern2() | ||
if in.isColon then | ||
if (isVarPattern(p) || p.isInstanceOf[Number]) && in.isColon then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code does not match the syntax: simple literals can also be strings, characters, or booleans. Also there should be tests that all simple literals can be given a type ascription.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The numeric literal tests are in your genericNumLits, BigFloat, GenericNumLits tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approve for change to syntax documents
@odersky happy to go with this, allowing only numeric literals (in addition to pattern variables)? Wouldn't want this to slip to 3.4.0. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. But I am not sure what happens with quotes and splices. Don't we need something like
$x : T
? Is that still allowed?
Reassigning to @nicolasstucki to get his input.
It is, like x match
case '{ $x: T } => // match using outer `T` in tests/pos-macros/i10050.scala. |
OK, then it's good to go. |
PR scala#16257 introduced a test for bindings in type annotations. Simultanously, scala#16150 disallowed the syntax that allowed the test to parse and compile. The test should be no longer needed.
This kind of patterns were disabled in scala#16150. Fixes scala#16367
This kind of patterns were disabled in scala#16150. Fixes scala#16367
The following snippet fails in nightly. Bisect points to this PR. Was that behaviour expected?: @main def Test =
val x: Any = 1
x match
case x @ (works: Long) => x
case x @ fails: Int => x errror [error] bisect/test.scala:5:21: '=>' expected, but ':' found
[error] case x @ fails: Int => x
[error] The snippet is based on a code found in tasty-query by Open CB |
I think the error is expected. |
Yes, that's expected. And it's just a test case (https://github.com/scalacenter/tasty-query/blob/main/test-sources/src/main/scala/simple_trees/Bind.scala) |
PR scala#16257 introduced a test for bindings in type annotations. Simultanously, scala#16150 disallowed the syntax that allowed the test to parse and compile. The test should be no longer needed.
This kind of patterns were disabled in scala#16150. Fixes scala#16367
…attern other than a variable or a number literal. This partially reverts the changes from scala#16150. This change is motivated by not breaking source compatibility for a number of projects in the Open Community Build.
…n other than a variable or a number literal. This partially reverts the changes from scala#16150. This change is motivated by not breaking source compatibility for a number of projects in the Open Community Build.
…n other than a variable or a number literal. This partially reverts the changes from scala#16150. This change is motivated by not breaking source compatibility for a number of projects in the Open Community Build.
Raise a warning instead of an error for a type ascription on a pattern other than a variable or a number literal. This partially reverts the changes from #16150. This change is motivated by not breaking source compatibility for a number of projects in the Open Community Build.
…n other than a variable or a number literal. This partially reverts the changes from #16150. This change is motivated by not breaking source compatibility for a number of projects in the Open Community Build.