-
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
13
commits into
scala:main
from
mberndt123:sip-67-strict-equality-improvements
+80
−8
Open
Changes from all commits
Commits
Show all changes
13 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 51b34cb
whitespace fix
mberndt123 79fc91d
add `strictEqualityPatternMatching` in `scala.language.experimental`
mberndt123 45e188c
Merge branch 'main' into sip-67-strict-equality-improvements
mberndt123 f23deba
address review comments
mberndt123 341e927
Merge branch 'main' into sip-67-strict-equality-improvements
mberndt123 684663a
comments
mberndt123 5f62655
Merge branch 'main' into sip-67-strict-equality-improvements
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
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 |
---|---|---|
|
@@ -84,6 +84,9 @@ object Implicits: | |
def strictEquality(using Context): Boolean = | ||
ctx.mode.is(Mode.StrictEquality) || Feature.enabled(nme.strictEquality) | ||
|
||
def strictEqualityPatternMatching(using Context): Boolean = | ||
Feature.enabled(Feature.strictEqualityPatternMatching) | ||
|
||
|
||
/** A common base class of contextual implicits and of-type implicits which | ||
* represents a set of references to implicit definitions. | ||
|
@@ -1038,8 +1041,9 @@ trait Implicits: | |
* - if one of T, U is an error type, or | ||
* - if one of T, U is a subtype of the lifted version of the other, | ||
* unless strict equality is set. | ||
* - if strictEqualityPatternMatching is set and the necessary conditions are met | ||
*/ | ||
def assumedCanEqual(ltp: Type, rtp: Type)(using Context) = { | ||
def assumedCanEqual(ltp: Type, rtp: Type, leftTree: Tree = EmptyTree)(using Context): Boolean = { | ||
// Map all non-opaque abstract types to their upper bound. | ||
// This is done to check whether such types might plausibly be comparable to each other. | ||
val lift = new TypeMap { | ||
|
@@ -1062,15 +1066,23 @@ trait Implicits: | |
|
||
ltp.isError | ||
|| rtp.isError | ||
|| !strictEquality && (ltp <:< lift(rtp) || rtp <:< lift(ltp)) | ||
|| locally: | ||
if strictEquality then | ||
strictEqualityPatternMatching && | ||
(leftTree.symbol.isAllOf(Flags.EnumValue) || leftTree.symbol.isAllOf(Flags.Module | Flags.Case)) && | ||
ltp <:< lift(rtp) | ||
else | ||
ltp <:< lift(rtp) || rtp <:< lift(ltp) | ||
} | ||
|
||
/** Check that equality tests between types `ltp` and `rtp` make sense */ | ||
def checkCanEqual(ltp: Type, rtp: Type, span: Span)(using Context): Unit = | ||
if (!ctx.isAfterTyper && !assumedCanEqual(ltp, rtp)) { | ||
/** Check that equality tests between types `ltp` and `left.tpe` make sense. | ||
* `left` is required to check for the condition for language.strictEqualityPatternMatching. | ||
*/ | ||
def checkCanEqual(left: Tree, rtp: Type, span: Span)(using Context): Unit = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ltp is not part of the API anymore, so the doc comment needs to be updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I've updated the comment |
||
val ltp = left.tpe.widen | ||
if !ctx.isAfterTyper && !assumedCanEqual(ltp, rtp, left) then | ||
val res = implicitArgTree(defn.CanEqualClass.typeRef.appliedTo(ltp, rtp), span) | ||
implicits.println(i"CanEqual witness found for $ltp / $rtp: $res: ${res.tpe}") | ||
} | ||
|
||
object hasSkolem extends TreeAccumulator[Boolean]: | ||
def apply(x: Boolean, tree: Tree)(using Context): Boolean = | ||
|
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
44 changes: 44 additions & 0 deletions
compiler/test/dotty/tools/dotc/typer/SIP67Tests.scala
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,44 @@ | ||
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: | ||
|
||
private def checkNoErrors(source: String): Unit = | ||
val ctx = checkCompile("typer", source)((_, _) => ()) | ||
if ctx.reporter.hasErrors then | ||
fail("Unexpected compilation errors were reported") | ||
|
||
@Test | ||
def sip67test1: Unit = | ||
checkNoErrors: | ||
""" | ||
import scala.language.strictEquality | ||
import scala.language.experimental.strictEqualityPatternMatching | ||
enum Foo: | ||
case Bar | ||
|
||
val _ = | ||
(??? : Foo) match | ||
case Foo.Bar => | ||
""" | ||
@Test | ||
def sip67test2: Unit = | ||
checkNoErrors: | ||
""" | ||
import scala.language.strictEquality | ||
import scala.language.experimental.strictEqualityPatternMatching | ||
|
||
sealed trait Foo | ||
|
||
object Foo: | ||
case object Bar extends Foo | ||
|
||
val _ = | ||
(??? : Foo) match | ||
case Foo.Bar => | ||
""" |
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
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.