Skip to content

Fix #3182: return correct signatures for extractors #3184

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 3 commits into from
Sep 27, 2017
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
57 changes: 38 additions & 19 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,9 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
/* Whether the extractor is irrefutable */
def irrefutable(unapp: tpd.Tree): Boolean = {
// TODO: optionless patmat
unapp.tpe.widen.resultType.isRef(scalaSomeClass) ||
(unapp.symbol.is(Synthetic) && unapp.symbol.owner.linkedClass.is(Case))
unapp.tpe.widen.finalResultType.isRef(scalaSomeClass) ||
(unapp.symbol.is(Synthetic) && unapp.symbol.owner.linkedClass.is(Case)) ||
productArity(unapp.tpe.widen.finalResultType) > 0
}

/** Return the space that represents the pattern `pat`
Expand Down Expand Up @@ -451,30 +452,48 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
/** Parameter types of the case class type `tp`. Adapted from `unapplyPlan` in patternMatcher */
def signature(unapp: Type, unappSym: Symbol, argLen: Int): List[Type] = {
def caseClass = unappSym.owner.linkedClass

lazy val caseAccessors = caseClass.caseAccessors.filter(_.is(Method))

def isSyntheticScala2Unapply(sym: Symbol) =
sym.is(SyntheticCase) && sym.owner.is(Scala2x)

val mt @ MethodType(_) = unapp.widen

if (isSyntheticScala2Unapply(unappSym) && caseAccessors.length == argLen)
caseAccessors.map(_.info.asSeenFrom(mt.paramInfos.head, caseClass).widen)
else if (mt.resultType.isRef(defn.BooleanClass))
List()
else {
val isUnapplySeq = unappSym.name == nme.unapplySeq
if (isProductMatch(mt.resultType, argLen) && !isUnapplySeq) {
productSelectors(mt.resultType).take(argLen)
.map(_.info.asSeenFrom(mt.resultType, mt.resultType.classSymbol).widen)
}
// Case unapply:
// 1. return types of constructor fields if the extractor is synthesized for Scala2 case classes & length match
// 2. return Nil if unapply returns Boolean (boolean pattern)
// 3. return product selector types if unapply returns a product type (product pattern)
// 4. return product selectors of `T` where `def get: T` is a member of the return type of unapply & length match (named-based pattern)
// 5. otherwise, return `T` where `def get: T` is a member of the return type of unapply
//
// Case unapplySeq:
// 1. return the type `List[T]` where `T` is the element type of the unapplySeq return type `Seq[T]`

val sig =
if (isSyntheticScala2Unapply(unappSym) && caseAccessors.length == argLen)
caseAccessors.map(_.info.asSeenFrom(mt.paramInfos.head, caseClass).widen)
else if (mt.finalResultType.isRef(defn.BooleanClass))
List()
else {
val resTp = mt.resultType.select(nme.get).resultType.widen
if (isUnapplySeq) scalaListType.appliedTo(resTp.argTypes.head) :: Nil
else if (argLen == 0) Nil
else productSelectors(resTp).map(_.info.asSeenFrom(resTp, resTp.classSymbol).widen)
val isUnapplySeq = unappSym.name == nme.unapplySeq
if (isProductMatch(mt.finalResultType, argLen) && !isUnapplySeq) {
productSelectors(mt.finalResultType).take(argLen)
.map(_.info.asSeenFrom(mt.finalResultType, mt.resultType.classSymbol).widen)
}
else {
val resTp = mt.finalResultType.select(nme.get).finalResultType.widen
if (isUnapplySeq) scalaListType.appliedTo(resTp.argTypes.head) :: Nil
else if (argLen == 0) Nil
else if (isProductMatch(resTp, argLen))
productSelectors(resTp).map(_.info.asSeenFrom(resTp, resTp.classSymbol).widen)
else resTp :: Nil
}
}
}

debug.println(s"signature of ${unappSym.showFullName} ----> ${sig.map(_.show).mkString(", ")}")

sig
}

/** Decompose a type into subspaces -- assume the type can be decomposed */
Expand Down Expand Up @@ -705,14 +724,14 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
showType(tp) + params(tp).map(_ => "_").mkString("(", ", ", ")")
else if (decomposed) "_: " + showType(tp)
else "_"
case Prod(tp, fun, _, params, _) =>
case Prod(tp, fun, sym, params, _) =>
if (ctx.definitions.isTupleType(tp))
"(" + params.map(doShow(_)).mkString(", ") + ")"
else if (tp.isRef(scalaConsType.symbol))
if (mergeList) params.map(doShow(_, mergeList)).mkString(", ")
else params.map(doShow(_, true)).filter(_ != "Nil").mkString("List(", ", ", ")")
else
showType(tp) + params.map(doShow(_)).mkString("(", ", ", ")")
showType(sym.owner.typeRef) + params.map(doShow(_)).mkString("(", ", ", ")")
case Or(_) =>
throw new Exception("incorrect flatten result " + s)
}
Expand Down
2 changes: 0 additions & 2 deletions tests/patmat/optionless.check
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
20: Pattern Match Exhaustivity: _: Tree
24: Pattern Match Exhaustivity: _: Tree
28: Pattern Match Exhaustivity: _: Tree
4 changes: 4 additions & 0 deletions tests/patmat/t10502.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
5: Pattern Match Exhaustivity: Perhaps(None)
15: Pattern Match Exhaustivity: Nil
31: Pattern Match Exhaustivity: Multi(None, _)
44: Pattern Match Exhaustivity: Prod(None, _)
47 changes: 47 additions & 0 deletions tests/patmat/t10502.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
object Perhaps {

def unapply[A](oa: Option[A]): Some[Option[A]] = Some(oa)

Option("hello") match {
case Perhaps(Some(s)) => println(s)
}

List(Option("hello")) match {
case Perhaps(Some(s)) :: t => println(s)
case Perhaps(None ) :: t => ()
case Nil => ()
}

List(Option("hello")) match {
case Perhaps(Some(s)) :: t => println(s)
case Perhaps(None ) :: t => ()
// case Nil => ()
}

}

object Multi {
def unapply(str: String): Some[(Option[Int], Int)] = ???

"hello" match {
case Multi(Some(i), x) =>
case Multi(None, x) =>
}

"hello" match {
case Multi(Some(i), x) =>
}
}

object Prod {
def unapply(str: String): (Option[Int], Int) = ???

"hello" match {
case Prod(Some(i), x) =>
case Prod(None, x) =>
}

"hello" match {
case Prod(Some(i), x) =>
}
}