Skip to content

Commit a4be2a7

Browse files
committed
Decode quote patterns AST from TASTY
1 parent ec89a26 commit a4be2a7

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,6 @@ object Trees {
741741
* `QuotePattern`s are created the type checker when typing an `untpd.Quote` in a pattern context.
742742
*
743743
* `QuotePattern`s are checked are encoded into `unapply`s in the `staging` phase.
744-
* We also encode the tree before pickling it TASTy. Therefore we still load quote patterns as `unapply`s
745-
* as we did before the existence of `QuotePattern`.
746744
*
747745
* The `bindings` contain the list of quote pattern type variable definitions (`Bind`s) in the oreder in
748746
* which they are defined in the source.

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,9 @@ class Definitions {
853853

854854
@tu lazy val QuoteMatchingClass: ClassSymbol = requiredClass("scala.quoted.runtime.QuoteMatching")
855855
@tu lazy val QuoteMatching_ExprMatch: Symbol = QuoteMatchingClass.requiredMethod("ExprMatch")
856-
@tu lazy val QuoteMatching_ExprMatchModule: Symbol = QuoteMatchingClass.requiredClass("ExprMatchModule")
856+
@tu lazy val QuoteMatching_ExprMatch_unapply: Symbol = QuoteMatchingClass.requiredClass("ExprMatchModule").requiredMethod(nme.unapply)
857857
@tu lazy val QuoteMatching_TypeMatch: Symbol = QuoteMatchingClass.requiredMethod("TypeMatch")
858-
@tu lazy val QuoteMatching_TypeMatchModule: Symbol = QuoteMatchingClass.requiredClass("TypeMatchModule")
858+
@tu lazy val QuoteMatching_TypeMatch_unapply: Symbol = QuoteMatchingClass.requiredClass("TypeMatchModule").requiredMethod(nme.unapply)
859859
@tu lazy val QuoteMatchingModule: Symbol = requiredModule("scala.quoted.runtime.QuoteMatching")
860860
@tu lazy val QuoteMatching_KNil: Symbol = QuoteMatchingModule.requiredType("KNil")
861861
@tu lazy val QuoteMatching_KCons: Symbol = QuoteMatchingModule.requiredType("KCons")

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import Trees._
3333
import Decorators._
3434
import transform.SymUtils._
3535
import cc.{adaptFunctionTypeUnderPureFuns, adaptByNameArgUnderPureFuns}
36+
import dotty.tools.dotc.quoted.QuotePatterns
3637

3738
import dotty.tools.tasty.{TastyBuffer, TastyReader}
3839
import TastyBuffer._
@@ -1419,7 +1420,11 @@ class TreeUnpickler(reader: TastyReader,
14191420
}
14201421
val patType = readType()
14211422
val argPats = until(end)(readTree())
1422-
UnApply(fn, implicitArgs, argPats, patType)
1423+
val unapply = UnApply(fn, implicitArgs, argPats, patType)
1424+
if fn.symbol == defn.QuoteMatching_ExprMatch_unapply
1425+
|| fn.symbol == defn.QuoteMatching_TypeMatch_unapply
1426+
then QuotePatterns.decode(unapply)
1427+
else unapply
14231428
case REFINEDtpt =>
14241429
val refineCls = symAtAddr.getOrElse(start,
14251430
newRefinedClassSymbol(coordAt(start))).asClass

compiler/src/dotty/tools/dotc/quoted/QuotePatterns.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ object QuotePatterns:
6767
val quoteClass = if (quotePattern.body.isTerm) defn.QuotedExprClass else defn.QuotedTypeClass
6868

6969
val matchModule = if quotePattern.body.isTerm then defn.QuoteMatching_ExprMatch else defn.QuoteMatching_TypeMatch
70-
val unapplyFun = quotePattern.quotes.asInstance(defn.QuoteMatchingClass.typeRef).select(matchModule).select(nme.unapply)
70+
val unapplySym = if quotePattern.body.isTerm then defn.QuoteMatching_ExprMatch_unapply else defn.QuoteMatching_TypeMatch_unapply
71+
val unapplyFun = quotePattern.quotes.asInstance(defn.QuoteMatchingClass.typeRef).select(matchModule).select(unapplySym)
7172

7273
val typeBindingsTuple = tpd.hkNestedPairsTypeTree(quotePattern.bindings)
7374

@@ -203,7 +204,7 @@ object QuotePatterns:
203204
fun match
204205
// <quotes>.asInstanceOf[QuoteMatching].{ExprMatch,TypeMatch}.unapply[<typeBindings>, <resTypes>]
205206
case TypeApply(Select(Select(TypeApply(Select(quotes, _), _), _), _), typeBindings :: resTypes :: Nil) =>
206-
val bindings = kListUnroll(typeBindings)
207+
val bindings = unrollBindings(typeBindings)
207208
val addPattenSplice = new TreeMap {
208209
private val patternIterator = patterns.iterator.filter {
209210
case pat: Bind => !pat.symbol.name.is(PatMatGivenVarName)
@@ -231,6 +232,7 @@ object QuotePatterns:
231232
case body => body
232233
cpy.QuotePattern(tree)(bindings, body, quotes)
233234

234-
private def kListUnroll(tree: Tree): List[Tree] = tree match
235-
case AppliedTypeTree(kCons, head :: tail :: Nil) => head :: kListUnroll(tail)
236-
case kNil => Nil
235+
private def unrollBindings(tree: Tree)(using Context): List[Tree] = tree match
236+
case AppliedTypeTree(tupleN, bindings) if defn.isTupleClass(tupleN.symbol) => bindings // TupleN, 1 <= N <= 22
237+
case AppliedTypeTree(_, head :: tail :: Nil) => head :: unrollBindings(tail) // KCons or *:
238+
case _ => Nil // KNil or EmptyTuple

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ trait QuotesAndSplices {
480480
else ref(defn.QuotedTypeModule_of.termRef).appliedToTypeTree(shape).appliedTo(quotes)
481481

482482
val matchModule = if quoted.isTerm then defn.QuoteMatching_ExprMatch else defn.QuoteMatching_TypeMatch
483-
val unapplyFun = quotes.asInstance(defn.QuoteMatchingClass.typeRef).select(matchModule).select(nme.unapply)
483+
val unapplySym = if quoted.isTerm then defn.QuoteMatching_ExprMatch_unapply else defn.QuoteMatching_TypeMatch_unapply
484+
val unapplyFun = quotes.asInstance(defn.QuoteMatchingClass.typeRef).select(matchModule).select(unapplySym)
484485

485486
val encodedPattern = UnApply(
486487
fun = unapplyFun.appliedToTypeTrees(typeBindingsTuple :: TypeTree(patType) :: Nil),

0 commit comments

Comments
 (0)