Skip to content

Fix #8758: strengthen the precondition for comparing extractors #8789

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

Merged
merged 1 commit into from
Apr 24, 2020
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
18 changes: 12 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,13 @@ trait SpaceLogic {
/** Is `tp1` a subtype of `tp2`? */
def isSubType(tp1: Type, tp2: Type): Boolean

/** Is `tp1` the same type as `tp2`? */
def isEqualType(tp1: Type, tp2: Type): Boolean
/** Whether we may assume the two Unapply the same?
* That is, given the same parameter, returns the same result.
*
* This is more general than purity, as the same `unapply` method may
* take different prefix, thus behaves differently.
Comment on lines +82 to +86
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit confused by this description, here's a proposed slightly different version:

Suggested change
/** Whether we may assume the two Unapply the same?
* That is, given the same parameter, returns the same result.
*
* This is more general than purity, as the same `unapply` method may
* take different prefix, thus behaves differently.
/** True if we can assume that the two unapply methods are the same.
* That is, given the same parameter, they return the same result.
*
* 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): Boolean

/** Return a space containing the values of both types.
*
Expand Down Expand Up @@ -179,7 +184,7 @@ trait SpaceLogic {
// approximation: a type can never be fully matched by a partial extractor
full && isSubType(tp1, tp2) && isSubspace(Prod(tp2, fun, signature(fun, tp2, ss.length).map(Typ(_, false)), full), b)
case (Prod(_, fun1, ss1, _), Prod(_, fun2, ss2, _)) =>
isEqualType(fun1, fun2) && ss1.zip(ss2).forall((isSubspace _).tupled)
isSameUnapply(fun1, fun2) && ss1.zip(ss2).forall((isSubspace _).tupled)
}
}

Expand Down Expand Up @@ -217,7 +222,7 @@ trait SpaceLogic {
else if (canDecompose(tp2)) tryDecompose2(tp2)
else Empty
case (Prod(tp1, fun1, ss1, full), Prod(tp2, fun2, ss2, _)) =>
if (!isEqualType(fun1, fun2)) Empty
if (!isSameUnapply(fun1, fun2)) Empty
else if (ss1.zip(ss2).exists(p => simplify(intersect(p._1, p._2)) == Empty)) Empty
else Prod(tp1, fun1, ss1.zip(ss2).map((intersect _).tupled), full)
}
Expand Down Expand Up @@ -257,7 +262,7 @@ trait SpaceLogic {
case (Typ(tp1, _), Prod(tp2, _, _, false)) =>
a // approximation
case (Prod(tp1, fun1, ss1, full), Prod(tp2, fun2, ss2, _)) =>
if (!isEqualType(fun1, fun2)) a
if (!isSameUnapply(fun1, fun2)) a
else if (ss1.zip(ss2).exists(p => simplify(intersect(p._1, p._2)) == Empty)) a
else if (ss1.zip(ss2).forall((isSubspace _).tupled)) Empty
else
Expand Down Expand Up @@ -499,7 +504,8 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
res
}

def isEqualType(tp1: Type, tp2: Type): Boolean = tp1 =:= tp2
def isSameUnapply(tp1: TermRef, tp2: TermRef): Boolean =
tp1.prefix.isStable && tp2.prefix.isStable && tp1 =:= tp2

/** Parameter types of the case class type `tp`. Adapted from `unapplyPlan` in patternMatcher */
def signature(unapp: TermRef, scrutineeTp: Type, argLen: Int): List[Type] = {
Expand Down
5 changes: 5 additions & 0 deletions tests/pos-special/fatal-warnings/i8758.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test = "?johndoe" match {
case s":$name" => println(s":name $name")
case s"{$name}" => println(s"{name} $name")
case s"?$pos" => println(s"pos $pos")
}