-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
260183f
Fix #10994: align typed pattern syntax to Scala 2
liufengyun 50e3a77
Fix bootstrapping
liufengyun 8275f32
Fix tests
liufengyun 7cff9dd
Fix NonNull test
liufengyun ddcecd1
Update docs/docs/internals/syntax.md
liufengyun 0bce5e5
Fix tests
dwijnand 4baaa50
Add latest test case
dwijnand 2c459cd
Only allow _numeric_ literals in typed patterns
dwijnand 27ba2e1
Copy changes from internal/syntax to reference/syntax
dwijnand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def foo = true match | ||
case (b: Boolean): Boolean => () // error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
sealed trait NatT | ||
case class Zero() extends NatT | ||
case class Succ[+N <: NatT](n: N) extends NatT | ||
|
||
type Mod2[N <: NatT] <: NatT = N match | ||
case Zero => Zero | ||
case Succ[Zero] => Succ[Zero] | ||
case Succ[Succ[predPredN]] => Mod2[predPredN] | ||
|
||
def mod2(n: NatT): NatT = n match | ||
case Zero() => Zero() | ||
case Succ(Zero()) => Succ(Zero()) | ||
case Succ(Succ(predPredN)) => mod2(predPredN) | ||
|
||
inline def inlineMod2(inline n: NatT): NatT = inline n match | ||
case Zero() => Zero() | ||
case Succ(Zero()) => Succ(Zero()) | ||
case Succ(Succ(predPredN)) => inlineMod2(predPredN) | ||
|
||
transparent inline def transparentInlineMod2(inline n: NatT): NatT = inline n match | ||
case Zero() => Zero() | ||
case Succ(Zero()) => Succ(Zero()) | ||
case Succ(Succ(predPredN)) => transparentInlineMod2(predPredN) | ||
|
||
def dependentlyTypedMod2[N <: NatT](n: N): Mod2[N] = n match // exhaustivity warning; unexpected | ||
case Zero(): Zero => Zero() // error | ||
case Succ(Zero()): Succ[Zero] => Succ(Zero()) // error | ||
case Succ(Succ(predPredN)): Succ[Succ[_]] => dependentlyTypedMod2(predPredN) // error | ||
|
||
inline def inlineDependentlyTypedMod2[N <: NatT](inline n: N): Mod2[N] = inline n match | ||
case Zero(): Zero => Zero() // error | ||
case Succ(Zero()): Succ[Zero] => Succ(Zero()) // error | ||
case Succ(Succ(predPredN)): Succ[Succ[_]] => inlineDependentlyTypedMod2(predPredN) // error | ||
|
||
transparent inline def transparentInlineDependentlyTypedMod2[N <: NatT](inline n: N): Mod2[N] = inline n match | ||
case Zero(): Zero => Zero() // error | ||
case Succ(Zero()): Succ[Zero] => Succ(Zero()) // error | ||
case Succ(Succ(predPredN)): Succ[Succ[_]] => transparentInlineDependentlyTypedMod2(predPredN) // error | ||
|
||
def foo(n: NatT): NatT = mod2(n) match | ||
case Succ(Zero()) => Zero() | ||
case _ => n | ||
|
||
inline def inlineFoo(inline n: NatT): NatT = inline inlineMod2(n) match | ||
case Succ(Zero()) => Zero() | ||
case _ => n | ||
|
||
inline def transparentInlineFoo(inline n: NatT): NatT = inline transparentInlineMod2(n) match | ||
case Succ(Zero()) => Zero() | ||
case _ => n | ||
|
||
@main def main(): Unit = | ||
println(mod2(Succ(Succ(Succ(Zero()))))) // prints Succ(Zero()), as expected | ||
println(foo(Succ(Succ(Succ(Zero()))))) // prints Zero(), as expected | ||
println(inlineMod2(Succ(Succ(Succ(Zero()))))) // prints Succ(Zero()), as expected | ||
println(inlineFoo(Succ(Succ(Succ(Zero()))))) // prints Succ(Succ(Succ(Zero()))); unexpected | ||
println(transparentInlineMod2(Succ(Succ(Succ(Zero()))))) // prints Succ(Zero()), as expected | ||
println(transparentInlineFoo(Succ(Succ(Succ(Zero()))))) // prints Zero(), as expected | ||
println(dependentlyTypedMod2(Succ(Succ(Succ(Zero()))))) // runtime error; unexpected | ||
// println(inlineDependentlyTypedMod2(Succ(Succ(Succ(Zero()))))) // doesn't compile; unexpected | ||
// println(transparentInlineDependentlyTypedMod2(Succ(Succ(Succ(Zero()))))) // doesn't compile; unexpected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.