Skip to content

Fix internal definition of Unapply #12236

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
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
28 changes: 10 additions & 18 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler

object TermTypeTest extends TypeTest[Tree, Term]:
def unapply(x: Tree): Option[Term & x.type] = x match
case _ if UnapplyTypeTest.unapply(x).isDefined => None
case _: tpd.PatternTree => None
case x: (tpd.Tree & x.type) if x.isTerm => Some(x)
case x: tpd.PatternTree => None
case x: (tpd.SeqLiteral & x.type) => Some(x)
case x: (tpd.Inlined & x.type) => Some(x)
case x: (tpd.NamedArg & x.type) => Some(x)
case _ => None
case _ => if x.isTerm then Some(x) else None
end TermTypeTest

object Term extends TermModule:
Expand Down Expand Up @@ -1424,14 +1422,12 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end extension
end BindMethods

type Unapply = tpd.UnApply | tpd.Typed // tpd.Typed containing a tpd.UnApply as expression
type Unapply = tpd.UnApply

object UnapplyTypeTest extends TypeTest[Tree, Unapply]:
def unapply(x: Tree): Option[Unapply & x.type] =
x match // keep in sync with UnapplyMethodsImpl.selfUnApply
case x: (tpd.UnApply & x.type) => Some(x)
case x: (tpd.Typed & x.type) if x.expr.isInstanceOf[tpd.UnApply] => Some(x)
case _ => None
def unapply(x: Tree): Option[Unapply & x.type] = x match
case x: (tpd.UnApply & x.type) => Some(x)
case _ => None
end UnapplyTypeTest

object Unapply extends UnapplyModule:
Expand All @@ -1443,14 +1439,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler

given UnapplyMethods: UnapplyMethods with
extension (self: Unapply)
def fun: Term = selfUnApply(self).fun
def implicits: List[Term] = selfUnApply(self).implicits
def patterns: List[Tree] = effectivePatterns(selfUnApply(self).patterns)
end extension
private def selfUnApply(self: Unapply): tpd.UnApply =
self match // keep in sync with UnapplyTypeTest
case self: tpd.UnApply => self
case self: tpd.Typed => self.expr.asInstanceOf[tpd.UnApply]
def fun: Term = self.fun
def implicits: List[Term] = self.implicits
def patterns: List[Tree] = effectivePatterns(self.patterns)
end extension
private def effectivePatterns(patterns: List[Tree]): List[Tree] =
patterns match
case patterns0 :+ dotc.ast.Trees.SeqLiteral(elems, _) => patterns0 ::: elems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@ object SourceCode {
case Closure(meth, _) =>
printTree(meth)

case _:Unapply | _:Alternatives | _:Bind =>
printPattern(tree)

case _ =>
throw new MatchError(tree.show(using Printer.TreeStructure))

Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4359,7 +4359,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
case New(tpt) =>
New.copy(tree)(transformTypeTree(tpt)(owner))
case Typed(expr, tpt) =>
Typed.copy(tree)(transformTerm(expr)(owner), transformTypeTree(tpt)(owner))
Typed.copy(tree)(/*FIXME #12222: transformTerm(expr)(owner)*/transformTree(expr)(owner).asInstanceOf[Term], transformTypeTree(tpt)(owner))
case tree: NamedArg =>
NamedArg.copy(tree)(tree.name, transformTerm(tree.value)(owner))
case Assign(lhs, rhs) =>
Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/i5161.check
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
run : Some(2)
show : scala.Tuple2.apply[scala.Option[scala.Int], scala.Option[scala.Int]](scala.Some.apply[scala.Int](1), scala.Some.apply[scala.Int](1)) match {
case scala.Tuple2(scala.Some(x), scala.Some(y)) =>
case scala.Tuple2((scala.Some(x): scala.Some[scala.Int]), (scala.Some(y): scala.Some[scala.Int])) =>
scala.Some.apply[scala.Int](x.+(y))
case _ =>
scala.None
Expand Down