-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement SIP-67: strictEquality pattern matching (fixes #22732) #23803
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
Open
mberndt123
wants to merge
6
commits into
scala:main
Choose a base branch
from
mberndt123:sip-67-strict-equality-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5778bea
Implement SIP-67: strictEquality pattern matching (Github issue #22732)
mberndt123 0a565d0
use braceless syntax in SIP67Tests
mberndt123 d433ebe
change SIP-67 implementation based on @odersky's review
mberndt123 4f7a9a3
hide SIP-67 behaviour behind experimental feature flag
mberndt123 56f11c2
remove excess parens
mberndt123 6570fb2
minor refactoring of SIP67Tests
mberndt123 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 hidden or 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 hidden or 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,43 @@ | ||
package dotty.tools.dotc.typer | ||
|
||
import dotty.tools.DottyTest | ||
import dotty.tools.dotc.core.Contexts.* | ||
|
||
import org.junit.Test | ||
import org.junit.Assert.fail | ||
|
||
class SIP67Tests extends DottyTest: | ||
|
||
@Test | ||
def sip67test1: Unit = | ||
val source = """ | ||
import scala.language.strictEquality | ||
enum Foo: | ||
case Bar | ||
|
||
val _ = | ||
(??? : Foo) match | ||
case Foo.Bar => | ||
""" | ||
val ctx = checkCompile("typer", source)((_, ctx) => ()) | ||
|
||
if ctx.reporter.hasErrors then | ||
fail("Unexpected compilation errors were reported") | ||
|
||
@Test | ||
def sip67test2: Unit = | ||
val source = """ | ||
import scala.language.strictEquality | ||
|
||
sealed trait Foo | ||
|
||
object Foo: | ||
case object Bar extends Foo | ||
|
||
val _ = | ||
(??? : Foo) match | ||
case Foo.Bar => | ||
""" | ||
val ctx = checkCompile("typer", source)((_, ctx) => ()) | ||
if ctx.reporter.hasErrors then | ||
fail("Unexpected compilation errors were reported") |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Some remarks:
The motivation for the SIP was that it makes strictEquality easier to use, but this also changes the pattern matching behavior of patterns without that import. And it breaks the law that pattern matching values is done with an
==
check. I think this goes too far, even though this is what's specified in the SIP.We cannot implement a SIP change without going through an experimenal language import.
I think it would be better to add code to
assumedCanEqual
instead. Under strictEquality and the experimental import, if the left operand (or either one) is an enum constant, and the other operand is a supertype, assume the twoCanEqual
.Uh oh!
There was an error while loading. Please reload this page.
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.
Hi @odersky,
Thank you for your review.
Regarding 1: 🤔 I'm not sure I understand. The SIP says “The semantics of pattern matching against a constant are otherwise unchanged”, meaning that it will still be equivalent to
==
, except it doesn't requireCanEqual
. If that's not what it does, then it's a bug in my implementation. But I think that's solved by moving it toassumeCanEqual
as you suggested in point 3, right?Regarding 2: ✅ I've added an experimental language import,
strictEqualityPatternMatching
, to enable this behaviour. I also tried adding aMode
for this so it can be enabled with a command line flag similar to-language:strictEquality
, but unless I'm misreading the code, the modes are stored in anInt
bitmask and all the bits are already taken 😒Regarding 3: ✅ I've moved the implementation to
assumedCanEqual
, which was a bit tricky because in that function theTree
isn't available, but I need it to know if it's acase object
orenum case
. It's also called fromSynthesizer
where there is noTree
, so I'm passing it as anOption[Tree]
now.