Skip to content

Commit c9cb5df

Browse files
committed
Introduce UntypedSplice
Introduce UntypedSplice and make TreeCopiers and TreeMaps more regular to take splices into account.
1 parent 76b110b commit c9cb5df

File tree

7 files changed

+45
-12
lines changed

7 files changed

+45
-12
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import core.tasty.TreePickler.Hole
1111

1212
/** A map that applies three functions and a substitution together to a tree and
1313
* makes sure they are coordinated so that the result is well-typed. The functions are
14-
* @param typeMap A function from Type to Type that gets applied to the
14+
* @param typeMap A function from Type to Type that gets applied to the
1515
* type of every tree node and to all locally defined symbols,
1616
* followed by the substitution [substFrom := substTo].
1717
* @param treeMap A transformer that translates all encountered subtrees in
@@ -38,7 +38,7 @@ class TreeTypeMap(
3838
val oldOwners: List[Symbol] = Nil,
3939
val newOwners: List[Symbol] = Nil,
4040
val substFrom: List[Symbol] = Nil,
41-
val substTo: List[Symbol] = Nil)(implicit ctx: Context) extends tpd.TreeMap {
41+
val substTo: List[Symbol] = Nil)(implicit ctx: Context) extends tpd.TypedTreeMap {
4242
import tpd._
4343

4444
/** If `sym` is one of `oldOwners`, replace by corresponding symbol in `newOwners` */

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,9 @@ object Trees {
11481148

11491149
abstract class TreeMap(val cpy: TreeCopier = inst.cpy) {
11501150

1151+
protected def handleMoreCases(tree: Tree)(implicit ctx: Context): Tree =
1152+
if (ctx.reporter.errorsReported) tree else throw new MatchError(tree)
1153+
11511154
def transform(tree: Tree)(implicit ctx: Context): Tree = {
11521155
Stats.record(s"TreeMap.transform $getClass")
11531156
Stats.record("TreeMap.transform total")
@@ -1245,8 +1248,8 @@ object Trees {
12451248
case Thicket(trees) =>
12461249
val trees1 = transform(trees)
12471250
if (trees1 eq trees) tree else Thicket(trees1)
1248-
case _ if ctx.reporter.errorsReported =>
1249-
tree
1251+
case _ =>
1252+
handleMoreCases(tree)
12501253
}
12511254
}
12521255

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
324324
case pre: ThisType =>
325325
tp.isType ||
326326
pre.cls.isStaticOwner ||
327-
tp.symbol.isParamOrAccessor && !pre.cls.is(Trait) && ctx.owner.enclosingClass == pre.cls
327+
tp.symbol.isParamOrAccessor && !pre.cls.is(Trait) && ctx.owner.enclosingClass == pre.cls
328328
// was ctx.owner.enclosingClass.derivesFrom(pre.cls) which was not tight enough
329329
// and was spuriously triggered in case inner class would inherit from outer one
330330
// eg anonymous TypeMap inside TypeMap.andThen
@@ -471,6 +471,8 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
471471
} else foldOver(sym, tree)
472472
}
473473

474+
case class UntypedSplice(splice: untpd.Tree) extends Tree
475+
474476
override val cpy: TypedTreeCopier = // Type ascription needed to pick up any new members in TreeCopier (currently there are none)
475477
new TypedTreeCopier
476478

@@ -609,6 +611,11 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
609611
}
610612
}
611613

614+
def UntypedSplice(tree: Tree)(splice: untpd.Tree) = tree match {
615+
case tree: tpd.UntypedSplice if tree.splice `eq` splice => tree
616+
case _ => finalize(tree, tpd.UntypedSplice(splice))
617+
}
618+
612619
override def If(tree: If)(cond: Tree = tree.cond, thenp: Tree = tree.thenp, elsep: Tree = tree.elsep)(implicit ctx: Context): If =
613620
If(tree: Tree)(cond, thenp, elsep)
614621
override def Closure(tree: Closure)(env: List[Tree] = tree.env, meth: Tree = tree.meth, tpt: Tree = tree.tpt)(implicit ctx: Context): Closure =
@@ -638,6 +645,18 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
638645
Closure(tree: Tree)(env, meth, tpt)
639646
}
640647

648+
class TypedTreeMap(cpy: TypedTreeCopier = tpd.cpy) extends TreeMap(cpy) { self =>
649+
override def handleMoreCases(tree: Tree)(implicit ctx: Context) = tree match {
650+
case UntypedSplice(utree) =>
651+
val umap = new untpd.UntypedTreeMap() {
652+
override def typedMap = self.transform(_)
653+
}
654+
cpy.UntypedSplice(tree)(umap.transform(utree))
655+
case _ =>
656+
super.handleMoreCases(tree)
657+
}
658+
}
659+
641660
override def skipTransform(tree: Tree)(implicit ctx: Context) = tree.tpe.isError
642661

643662
implicit class TreeOps[ThisTree <: tpd.Tree](val tree: ThisTree) extends AnyVal {

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
2424
/** A typed subtree of an untyped tree needs to be wrapped in a TypedSlice
2525
* @param owner The current owner at the time the tree was defined
2626
*/
27-
abstract case class TypedSplice(tree: tpd.Tree)(val owner: Symbol) extends ProxyTree {
28-
def forwardTo = tree
27+
abstract case class TypedSplice(splice: tpd.Tree)(val owner: Symbol) extends ProxyTree {
28+
def forwardTo = splice
2929
}
3030

3131
object TypedSplice {
@@ -496,10 +496,16 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
496496
case tree: PatDef if (mods eq tree.mods) && (pats eq tree.pats) && (tpt eq tree.tpt) && (rhs eq tree.rhs) => tree
497497
case _ => finalize(tree, untpd.PatDef(mods, pats, tpt, rhs))
498498
}
499+
def TypedSplice(tree: Tree)(splice: tpd.Tree)(implicit ctx: Context) = tree match {
500+
case tree: TypedSplice if splice `eq` tree.splice => tree
501+
case _ => finalize(tree, untpd.TypedSplice(splice))
502+
}
499503
}
500504

501505
abstract class UntypedTreeMap(cpy: UntypedTreeCopier = untpd.cpy) extends TreeMap(cpy) {
502-
override def transform(tree: Tree)(implicit ctx: Context): Tree = tree match {
506+
protected def typedMap: tpd.Tree => tpd.Tree = identity
507+
508+
override def handleMoreCases(tree: Tree)(implicit ctx: Context): Tree = tree match {
503509
case ModuleDef(name, impl) =>
504510
cpy.ModuleDef(tree)(name, transformSub(impl))
505511
case ParsedTry(expr, handler, finalizer) =>
@@ -540,10 +546,10 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
540546
cpy.ContextBounds(tree)(transformSub(bounds), transform(cxBounds))
541547
case PatDef(mods, pats, tpt, rhs) =>
542548
cpy.PatDef(tree)(mods, transform(pats), transform(tpt), transform(rhs))
543-
case TypedSplice(_) =>
544-
tree
549+
case TypedSplice(splice) =>
550+
cpy.TypedSplice(tree)(typedMap(splice))
545551
case _ =>
546-
super.transform(tree)
552+
super.handleMoreCases(tree)
547553
}
548554
}
549555

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
446446
case TypedSplice(t) =>
447447
if (ctx.settings.YprintDebug.value) "[" ~ toText(t) ~ "]#TS#"
448448
else toText(t)
449+
case tpd.UntypedSplice(t) =>
450+
if (ctx.settings.YprintDebug.value) "[" ~ toText(t) ~ "]#US#"
451+
toText(t)
449452
case tree @ ModuleDef(name, impl) =>
450453
withEnclosingDef(tree) {
451454
modText(tree.mods, NoSymbol, keywordStr("object")) ~~ nameIdText(tree) ~ toTextTemplate(impl)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ abstract class MacroTransform extends Phase {
5959
transform(parents)(ctx.superCallContext),
6060
transformSelf(self),
6161
transformStats(impl.body, tree.symbol))
62+
case UntypedSplice(_) =>
63+
tree
6264
case _ =>
6365
super.transform(tree)
6466
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ class Typer extends Namer
16651665
}
16661666

16671667
def typedTypedSplice(tree: untpd.TypedSplice)(implicit ctx: Context): Tree =
1668-
tree.tree match {
1668+
tree.splice match {
16691669
case tree1: TypeTree => tree1 // no change owner necessary here ...
16701670
case tree1: Ident => tree1 // ... or here, since these trees cannot contain bindings
16711671
case tree1 =>

0 commit comments

Comments
 (0)