Skip to content

Fix #8997: Support multiple contextual parameter blocks on unapply #9003

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
May 25, 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
13 changes: 11 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,18 @@ object PatternMatcher {
// This plan will never execute because it'll be guarded by a `NonNullTest`.
ResultPlan(tpd.Throw(tpd.nullLiteral))
else {
def applyImplicits(acc: Tree, implicits: List[Tree], mt: Type): Tree = mt match {
case mt: MethodType =>
assert(mt.isImplicitMethod || mt.isContextualMethod)
val (args, rest) = implicits.splitAt(mt.paramNames.size)
applyImplicits(acc.appliedToArgs(args), rest, mt.resultType)
case _ =>
assert(implicits.isEmpty)
acc
}
val mt @ MethodType(_) = extractor.tpe.widen
var unapp = extractor.appliedTo(ref(scrutinee).ensureConforms(mt.paramInfos.head))
if (implicits.nonEmpty) unapp = unapp.appliedToArgs(implicits)
val unapp0 = extractor.appliedTo(ref(scrutinee).ensureConforms(mt.paramInfos.head))
val unapp = applyImplicits(unapp0, implicits, mt.resultType)
unapplyPlan(unapp, args)
}
if (scrutinee.info.isNotNull || nonNull(scrutinee)) unappPlan
Expand Down
20 changes: 12 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1226,14 +1226,18 @@ trait Applications extends Compatibility {
}
val dummyArg = dummyTreeOfType(ownType)
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
def unapplyImplicits(unapp: Tree): List[Tree] = unapp match {
case Apply(Apply(unapply, `dummyArg` :: Nil), args2) => assert(args2.nonEmpty); args2
case Apply(unapply, `dummyArg` :: Nil) => Nil
case Inlined(u, _, _) => unapplyImplicits(u)
case DynamicUnapply(_) =>
ctx.error("Structural unapply is not supported", unapplyFn.sourcePos)
Nil
case _ => Nil.assertingErrorsReported
def unapplyImplicits(unapp: Tree): List[Tree] = {
val res = List.newBuilder[Tree]
def loop(unapp: Tree): Unit = unapp match {
case Apply(Apply(unapply, `dummyArg` :: Nil), args2) => assert(args2.nonEmpty); res ++= args2
case Apply(unapply, `dummyArg` :: Nil) =>
case Inlined(u, _, _) => loop(u)
case DynamicUnapply(_) => ctx.error("Structural unapply is not supported", unapplyFn.sourcePos)
case Apply(fn, args) => assert(args.nonEmpty); loop(fn); res ++= args
case _ => ().assertingErrorsReported
}
loop(unapp)
res.result()
}

var argTypes = unapplyArgs(unapplyApp.tpe, unapplyFn, args, tree.sourcePos)
Expand Down
7 changes: 7 additions & 0 deletions tests/pos/i8997.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Foo:
def unapply(n: Int)(using x: DummyImplicit)(using y: Int): Option[Int] = ???

def test =
given Int = 3
1 match
case Foo(_) =>