Skip to content

Commit 938d3ae

Browse files
committed
Split Quoted extractor into QuotedExpr and QuotedTypeOf
1 parent c2f6362 commit 938d3ae

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
10271027
}
10281028

10291029
/** Extractors for quotes */
1030-
object Quoted {
1030+
object QuotedExpr {
10311031
/** Extracts the content of a quoted tree.
10321032
* The result can be the contents of a term or type quote, which
10331033
* will return a term or type tree respectively.
@@ -1036,7 +1036,16 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
10361036
if tree.symbol == defn.QuotedRuntime_exprQuote then
10371037
// quoted.runtime.Expr.quote[T](<body>)
10381038
Some(tree.args.head)
1039-
else if tree.symbol == defn.QuotedTypeModule_of then
1039+
else None
1040+
}
1041+
1042+
object QuotedTypeOf {
1043+
/** Extracts the content of a quoted tree.
1044+
* The result can be the contents of a term or type quote, which
1045+
* will return a term or type tree respectively.
1046+
*/
1047+
def unapply(tree: tpd.Apply)(using Context): Option[tpd.Tree] =
1048+
if tree.symbol == defn.QuotedTypeModule_of then
10401049
// quoted.Type.of[<body>](quotes)
10411050
val TypeApply(_, body :: _) = tree.fun: @unchecked
10421051
Some(body)

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ class Inliner(val call: tpd.Tree)(using Context):
817817
override def typedApply(tree: untpd.Apply, pt: Type)(using Context): Tree =
818818
def cancelQuotes(tree: Tree): Tree =
819819
tree match
820-
case Quoted(SplicedExpr(inner)) => inner
820+
case QuotedExpr(SplicedExpr(inner)) => inner
821821
case _ => tree
822822
val locked = ctx.typerState.ownedVars
823823
val res = cancelQuotes(constToLiteral(BetaReduce(super.typedApply(tree, pt)))) match {
@@ -1067,7 +1067,11 @@ class Inliner(val call: tpd.Tree)(using Context):
10671067
else tree match {
10681068
case tree: RefTree if tree.isTerm && tree.symbol.isDefinedInCurrentRun && !tree.symbol.isLocal =>
10691069
foldOver(tree.symbol :: syms, tree)
1070-
case Quoted(body) =>
1070+
case QuotedExpr(body) =>
1071+
level += 1
1072+
try apply(syms, body)
1073+
finally level -= 1
1074+
case QuotedTypeOf(body) =>
10711075
level += 1
10721076
try apply(syms, body)
10731077
finally level -= 1

compiler/src/dotty/tools/dotc/staging/TreeMapWithStages.scala

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,17 @@ abstract class TreeMapWithStages extends TreeMapWithImplicits {
4949
}
5050

5151
tree match {
52-
case Apply(Select(Quoted(quotedTree), _), _) if quotedTree.isType =>
53-
dropEmptyBlocks(quotedTree) match
54-
case SplicedType(t) =>
55-
// Optimization: `quoted.Type.of[x.Underlying]` --> `x`
56-
transform(t)
57-
case _ =>
58-
super.transform(tree)
52+
case Apply(Select(QuotedTypeOf(SplicedType(t)), _), _) =>
53+
// Optimization: `quoted.Type.of[x.Underlying]` --> `x`
54+
transform(t)
55+
56+
case tree @ QuotedTypeOf(quotedTree) =>
57+
val old = inQuoteOrSplice
58+
inQuoteOrSplice = true
59+
try transformQuotation(quotedTree, tree)
60+
finally inQuoteOrSplice = old
5961

60-
case tree @ Quoted(quotedTree) =>
62+
case tree @ QuotedExpr(quotedTree) =>
6163
val old = inQuoteOrSplice
6264
inQuoteOrSplice = true
6365
try dropEmptyBlocks(quotedTree) match {
@@ -73,7 +75,7 @@ abstract class TreeMapWithStages extends TreeMapWithImplicits {
7375
val old = inQuoteOrSplice
7476
inQuoteOrSplice = true
7577
try dropEmptyBlocks(splicedTree) match {
76-
case Quoted(t) =>
78+
case QuotedExpr(t) =>
7779
// Optimization: `${ 'x }` --> `x`
7880
transform(t)
7981
case _ => transformSplice(splicedTree, tree)

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ object Splicer {
4444
* See: `Staging`
4545
*/
4646
def splice(tree: Tree, splicePos: SrcPos, spliceExpansionPos: SrcPos, classLoader: ClassLoader)(using Context): Tree = tree match {
47-
case Quoted(quotedTree) => quotedTree
47+
case QuotedExpr(quotedTree) => quotedTree
4848
case _ =>
4949
val macroOwner = newSymbol(ctx.owner, nme.MACROkw, Macro | Synthetic, defn.AnyType, coord = tree.span)
5050
try
@@ -136,7 +136,7 @@ object Splicer {
136136
* See: `Staging`
137137
*/
138138
def checkValidMacroBody(tree: Tree)(using Context): Unit = tree match {
139-
case Quoted(_) => // ok
139+
case QuotedExpr(_) => // ok
140140
case _ =>
141141
type Env = Set[Symbol]
142142

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ object SpaceEngine {
312312
def isIrrefutableQuotedPattern(unapp: tpd.Tree, implicits: List[tpd.Tree], pt: Type)(using Context): Boolean = {
313313
implicits.headOption match
314314
// pattern '{ $x: T }
315-
case Some(tpd.Apply(tpd.Select(tpd.Quoted(tpd.TypeApply(fn, List(tpt))), nme.apply), _))
315+
case Some(tpd.Apply(tpd.Select(tpd.QuotedExpr(tpd.TypeApply(fn, List(tpt))), nme.apply), _))
316316
if unapp.symbol.owner.eq(defn.QuoteMatching_ExprMatchModule)
317317
&& fn.symbol.eq(defn.QuotedRuntimePatterns_patternHole) =>
318318
pt <:< defn.QuotedExprClass.typeRef.appliedTo(tpt.tpe)

0 commit comments

Comments
 (0)