Skip to content

Commit 40621e8

Browse files
committed
Remove logic from quote reification
1 parent af2e4b0 commit 40621e8

File tree

1 file changed

+18
-66
lines changed

1 file changed

+18
-66
lines changed

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

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,7 @@ class Staging extends MacroTransformWithImplicits {
4242
override def phaseName: String = Staging.name
4343

4444
override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = {
45-
tree match {
46-
case tree: RefTree if !ctx.inInlineMethod =>
47-
// assert(!tree.symbol.isQuote)
48-
// assert(!tree.symbol.isSplice) // TODO widen ~ type references at stage 0?
49-
// assert(tree.symbol != defn.QuotedExpr_~)
50-
case tree: Select if tree.symbol == defn.QuotedExpr_~ =>
51-
// assert(Splicer.canBeSpliced(tree.qualifier))
52-
case _ =>
53-
}
45+
// TODO check PCP
5446
}
5547

5648
override def run(implicit ctx: Context): Unit =
@@ -62,21 +54,6 @@ class Staging extends MacroTransformWithImplicits {
6254
private class LevelInfo {
6355
/** A map from locally defined symbols to the staging levels of their definitions */
6456
val levelOf = new mutable.HashMap[Symbol, Int]
65-
66-
/** Register a reference defined in a quote but used in another quote nested in a splice.
67-
* Returns a version of the reference that needs to be used in its place.
68-
* '{
69-
* val x = ???
70-
* { ... '{ ... x ... } ... }.unary_~
71-
* }
72-
* Eta expanding the `x` in `{ ... '{ ... x ... } ... }.unary_~` will return a `x$1.unary_~` for which the `x$1`
73-
* be created by some outer reifier.
74-
*
75-
* This transformation is only applied to definitions at staging level 1.
76-
*
77-
* See `isCaptured`
78-
*/
79-
val capturers = new mutable.HashMap[Symbol, Tree => Tree]
8057
}
8158

8259
/** The main transformer class
@@ -263,8 +240,6 @@ class Staging extends MacroTransformWithImplicits {
263240
else i"${sym.name}.this"
264241
if (!isThis && sym.maybeOwner.isType && !sym.is(Param))
265242
check(sym.owner, sym.owner.thisType, pos)
266-
else if (level == 1 && sym.isType && sym.is(Param) && sym.owner.isInlineMethod && !outer.isRoot)
267-
importedTags(sym.typeRef) = capturers(sym)(ref(sym))
268243
else if (sym.exists && !sym.isStaticOwner && !levelOK(sym))
269244
for (errMsg <- tryHeal(tp, pos))
270245
ctx.error(em"""access to $symStr from wrong staging level:
@@ -347,23 +322,18 @@ class Staging extends MacroTransformWithImplicits {
347322
if (isType) ref(defn.QuotedType_apply).appliedToType(body1.tpe.widen)
348323
else ref(defn.QuotedExpr_apply).appliedToType(body1.tpe.widen).appliedTo(body1)
349324
}
350-
else body match {
351-
case body: RefTree if isCaptured(body.symbol, level + 1) =>
352-
// Optimization: avoid the full conversion when capturing `x`
353-
// in '{ x } to '{ x$1.unary_~ } and go directly to `x$1`
354-
capturers(body.symbol)(body)
355-
case _=>
356-
val (body1, splices) = nested(isQuote = true).split(body)
357-
if (level == 0 && !ctx.inInlineMethod) {
358-
quote match {
359-
case quote: Apply => cpy.Apply(quote)(quote.fun, body1 :: Nil)
360-
case quote: TypeApply => cpy.TypeApply(quote)(quote.fun, body1 :: Nil)
361-
}
362-
}
363-
else {
364-
// In top-level splice in an inline def. Keep the tree as it is, it will be transformed at inline site.
365-
body
325+
else {
326+
val body1 = nested(isQuote = true).transformAndAddTags(body)
327+
if (level == 0 && !ctx.inInlineMethod) {
328+
quote match {
329+
case quote: Apply => cpy.Apply(quote)(quote.fun, body1 :: Nil)
330+
case quote: TypeApply => cpy.TypeApply(quote)(quote.fun, body1 :: Nil)
366331
}
332+
}
333+
else {
334+
// In top-level splice in an inline def. Keep the tree as it is, it will be transformed at inline site.
335+
body
336+
}
367337
}
368338
}
369339

@@ -387,7 +357,7 @@ class Staging extends MacroTransformWithImplicits {
387357
splice
388358
}
389359
else if (Splicer.canBeSpliced(splice.qualifier)) { // level 0 inside an inline definition
390-
nested(isQuote = false).split(splice.qualifier) // Just check PCP
360+
nested(isQuote = false).transform(splice.qualifier) // Just check PCP
391361
splice
392362
}
393363
else { // level 0 inside an inline definition
@@ -396,24 +366,10 @@ class Staging extends MacroTransformWithImplicits {
396366
}
397367
}
398368

399-
/** Returns true if this tree will be captured by `makeLambda`. Checks phase consistency and presence of capturer. */
400-
private def isCaptured(sym: Symbol, level: Int)(implicit ctx: Context): Boolean =
401-
level == 1 && levelOf.get(sym).contains(1) && capturers.contains(sym)
402-
403-
/** Transform `tree` and return the resulting tree and all `embedded` quotes
404-
* or splices as a pair, after performing the `addTags` transform.
405-
*/
406-
private def split(tree: Tree)(implicit ctx: Context): (Tree, List[Tree]) = {
407-
val tree1 = if (inQuote) addTags(transform(tree)) else transform(tree)
408-
(tree1, embedded.getTrees)
409-
}
410-
411-
/** Register `body` as an `embedded` quote or splice
412-
* and return a hole with `splices` as arguments and the given type `tpe`.
413-
*/
414-
private def makeHole(body: Tree, splices: List[Tree], tpe: Type)(implicit ctx: Context): Hole = {
415-
val idx = embedded.addTree(body, NoSymbol)
416-
Hole(idx, splices).withType(tpe).asInstanceOf[Hole]
369+
/** Transform `tree` followed by `addTags` transform. */
370+
private def transformAndAddTags(tree: Tree)(implicit ctx: Context): Tree = {
371+
assert(inQuote)
372+
addTags(transform(tree))
417373
}
418374

419375
override def transform(tree: Tree)(implicit ctx: Context): Tree =
@@ -435,14 +391,11 @@ class Staging extends MacroTransformWithImplicits {
435391
splice(tree)
436392
case tree: RefTree if tree.symbol.is(Inline) && tree.symbol.is(Param) =>
437393
tree
438-
case tree: RefTree if isCaptured(tree.symbol, level) =>
439-
val t = capturers(tree.symbol).apply(tree)
440-
splice(t.select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
441394
case Block(stats, _) =>
442395
val last = enteredSyms
443396
stats.foreach(markDef)
444397
mapOverTree(last)
445-
case CaseDef(pat, guard, body) =>
398+
case CaseDef(pat, _, _) =>
446399
val last = enteredSyms
447400
// mark all bindings
448401
new TreeTraverser {
@@ -498,7 +451,6 @@ class Staging extends MacroTransformWithImplicits {
498451
}
499452

500453
object Staging {
501-
import tpd._
502454

503455
val name: String = "staging"
504456

0 commit comments

Comments
 (0)