Skip to content
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

Consider extension methods in Space isSameUnapply #18642

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,14 @@ object SpaceEngine {
* We assume that unapply methods are pure, but the same method may
* be called with different prefixes, thus behaving differently.
*/
def isSameUnapply(tp1: TermRef, tp2: TermRef)(using Context): Boolean =
def isSameUnapply(tp1: TermRef, tp2: TermRef)(using Context): Boolean = trace(i"isSameUnapply($tp1, $tp2)") {
def isStable(tp: TermRef) =
!tp.symbol.is(ExtensionMethod) // The "prefix" of an extension method may be, but the receiver isn't, so exclude
&& tp.prefix.isStable
// always assume two TypeTest[S, T].unapply are the same if they are equal in types
(tp1.prefix.isStable && tp2.prefix.isStable || tp1.symbol == defn.TypeTest_unapply)
(isStable(tp1) && isStable(tp2) || tp1.symbol == defn.TypeTest_unapply)
&& tp1 =:= tp2
}

/** Return term parameter types of the extractor `unapp`.
* Parameter types of the case class type `tp`. Adapted from `unapplyPlan` in patternMatcher */
Expand Down
19 changes: 19 additions & 0 deletions tests/pos/i18601.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//> using options -Werror
extension (sc: StringContext)
def m: StringContext = sc
def unapply(string: String): Option[String] =
val pattern = sc.parts.head
if string.length == pattern.length then Some(string) else None

class Test:
def parse(x: PartialFunction[String, String]) = x

val pf = parse {
case m"x$s" => s
case m"xx$s" => s // was: unreachable
}

// proof that the second case isn't unreachable (matches "ab")
def t1 = pf.applyOrElse("a", _ => ".") // "a"
def t2 = pf.applyOrElse("ab", _ => ".") // "ab"
def t3 = pf.applyOrElse("abc", _ => ".") // "."
26 changes: 26 additions & 0 deletions tests/pos/i18601b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//> using options -Werror

// like pos/i18601
// but with a dedicated SC class
// that made the false positive redundancy warning go away

extension (sc: StringContext)
def m: SC = SC(sc)

class SC(sc: StringContext):
def unapply(string: String): Option[String] =
val pattern = sc.parts.head
if string.length == pattern.length then Some(string) else None

class Test:
def parse(x: PartialFunction[String, String]) = x

val pf = parse {
case m"x$s" => s
case m"xx$s" => s // was: not unreachable (as a counter-example)
}

// proof that the second case isn't unreachable (matches "ab")
def t1 = pf.applyOrElse("a", _ => ".") // "a"
def t2 = pf.applyOrElse("ab", _ => ".") // "ab"
def t3 = pf.applyOrElse("abc", _ => ".") // "."
Loading