Skip to content

Commit c54cd3e

Browse files
committed
Extend retyping to more copy methods.
We now retype basically everything except leave nodes and definitions. This provides for more robust tree copying and transformation. It also flushed out errors in SuperAccessors (fixed by a hack, awaiting systematic phase change there) and UnCurryTreeTransform. Uncurry is disabled for now, will be fixed shortly.
1 parent 53ab5f0 commit c54cd3e

File tree

4 files changed

+76
-27
lines changed

4 files changed

+76
-27
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class Compiler {
3232
new TypeTestsCasts,
3333
new InterceptedMethods,
3434
new Literalize),
35-
List(new Erasure),
36-
List(new UncurryTreeTransform
37-
/* , new Constructors */)
35+
List(new Erasure)//,
36+
//List(new UncurryTreeTransform
37+
// /* , new Constructors */)
3838
)
3939

4040
var runId = 1

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ object Trees {
914914

915915
/** A class for copying trees. The copy methods avid creating a new tree
916916
* If all arguments stay the same.
917-
917+
*
918918
* Note: Some of the copy methods take a context.
919919
* These are exactly those methods that are overridden in TypedTreeCopier
920920
* so that they selectively retype themselves. Retyping needs a context.
@@ -948,35 +948,35 @@ object Trees {
948948
case tree: Super if (qual eq tree.qual) && (mix == tree.mix) => tree
949949
case _ => finalize(tree, untpd.Super(qual, mix))
950950
}
951-
def Apply(tree: Tree)(fun: Tree, args: List[Tree]): Apply = tree match {
951+
def Apply(tree: Tree)(fun: Tree, args: List[Tree])(implicit ctx: Context): Apply = tree match {
952952
case tree: Apply if (fun eq tree.fun) && (args eq tree.args) => tree
953953
case _ => finalize(tree, untpd.Apply(fun, args))
954954
}
955-
def TypeApply(tree: Tree)(fun: Tree, args: List[Tree]): TypeApply = tree match {
955+
def TypeApply(tree: Tree)(fun: Tree, args: List[Tree])(implicit ctx: Context): TypeApply = tree match {
956956
case tree: TypeApply if (fun eq tree.fun) && (args eq tree.args) => tree
957957
case _ => finalize(tree, untpd.TypeApply(fun, args))
958958
}
959-
def Literal(tree: Tree)(const: Constant): Literal = tree match {
959+
def Literal(tree: Tree)(const: Constant)(implicit ctx: Context): Literal = tree match {
960960
case tree: Literal if (const == tree.const) => tree
961961
case _ => finalize(tree, untpd.Literal(const))
962962
}
963-
def New(tree: Tree)(tpt: Tree): New = tree match {
963+
def New(tree: Tree)(tpt: Tree)(implicit ctx: Context): New = tree match {
964964
case tree: New if (tpt eq tree.tpt) => tree
965965
case _ => finalize(tree, untpd.New(tpt))
966966
}
967967
def Pair(tree: Tree)(left: Tree, right: Tree)(implicit ctx: Context): Pair = tree match {
968968
case tree: Pair if (left eq tree.left) && (right eq tree.right) => tree
969969
case _ => finalize(tree, untpd.Pair(left, right))
970970
}
971-
def Typed(tree: Tree)(expr: Tree, tpt: Tree): Typed = tree match {
971+
def Typed(tree: Tree)(expr: Tree, tpt: Tree)(implicit ctx: Context): Typed = tree match {
972972
case tree: Typed if (expr eq tree.expr) && (tpt eq tree.tpt) => tree
973973
case _ => finalize(tree, untpd.Typed(expr, tpt))
974974
}
975-
def NamedArg(tree: Tree)(name: Name, arg: Tree): NamedArg = tree match {
975+
def NamedArg(tree: Tree)(name: Name, arg: Tree)(implicit ctx: Context): NamedArg = tree match {
976976
case tree: NamedArg if (name == tree.name) && (arg eq tree.arg) => tree
977977
case _ => finalize(tree, untpd.NamedArg(name, arg))
978978
}
979-
def Assign(tree: Tree)(lhs: Tree, rhs: Tree): Assign = tree match {
979+
def Assign(tree: Tree)(lhs: Tree, rhs: Tree)(implicit ctx: Context): Assign = tree match {
980980
case tree: Assign if (lhs eq tree.lhs) && (rhs eq tree.rhs) => tree
981981
case _ => finalize(tree, untpd.Assign(lhs, rhs))
982982
}
@@ -988,7 +988,7 @@ object Trees {
988988
case tree: If if (cond eq tree.cond) && (thenp eq tree.thenp) && (elsep eq tree.elsep) => tree
989989
case _ => finalize(tree, untpd.If(cond, thenp, elsep))
990990
}
991-
def Closure(tree: Tree)(env: List[Tree], meth: Tree, tpt: Tree): Closure = tree match {
991+
def Closure(tree: Tree)(env: List[Tree], meth: Tree, tpt: Tree)(implicit ctx: Context): Closure = tree match {
992992
case tree: Closure if (env eq tree.env) && (meth eq tree.meth) && (tpt eq tree.tpt) => tree
993993
case _ => finalize(tree, untpd.Closure(env, meth, tpt))
994994
}
@@ -1000,15 +1000,15 @@ object Trees {
10001000
case tree: CaseDef if (pat eq tree.pat) && (guard eq tree.guard) && (body eq tree.body) => tree
10011001
case _ => finalize(tree, untpd.CaseDef(pat, guard, body))
10021002
}
1003-
def Return(tree: Tree)(expr: Tree, from: Tree): Return = tree match {
1003+
def Return(tree: Tree)(expr: Tree, from: Tree)(implicit ctx: Context): Return = tree match {
10041004
case tree: Return if (expr eq tree.expr) && (from eq tree.from) => tree
10051005
case _ => finalize(tree, untpd.Return(expr, from))
10061006
}
10071007
def Try(tree: Tree)(expr: Tree, handler: Tree, finalizer: Tree)(implicit ctx: Context): Try = tree match {
10081008
case tree: Try if (expr eq tree.expr) && (handler eq tree.handler) && (finalizer eq tree.finalizer) => tree
10091009
case _ => finalize(tree, untpd.Try(expr, handler, finalizer))
10101010
}
1011-
def Throw(tree: Tree)(expr: Tree): Throw = tree match {
1011+
def Throw(tree: Tree)(expr: Tree)(implicit ctx: Context): Throw = tree match {
10121012
case tree: Throw if (expr eq tree.expr) => tree
10131013
case _ => finalize(tree, untpd.Throw(expr))
10141014
}
@@ -1104,7 +1104,7 @@ object Trees {
11041104
// is of the same class as the copy. We only include trees with more than 2 elements here.
11051105
def If(tree: If)(cond: Tree = tree.cond, thenp: Tree = tree.thenp, elsep: Tree = tree.elsep)(implicit ctx: Context): If =
11061106
If(tree: Tree)(cond, thenp, elsep)
1107-
def Closure(tree: Closure)(env: List[Tree] = tree.env, meth: Tree = tree.meth, tpt: Tree = tree.tpt): Closure =
1107+
def Closure(tree: Closure)(env: List[Tree] = tree.env, meth: Tree = tree.meth, tpt: Tree = tree.tpt)(implicit ctx: Context): Closure =
11081108
Closure(tree: Tree)(env, meth, tpt)
11091109
def CaseDef(tree: CaseDef)(pat: Tree = tree.pat, guard: Tree = tree.guard, body: Tree = tree.body)(implicit ctx: Context): CaseDef =
11101110
CaseDef(tree: Tree)(pat, guard, body)

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

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,39 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
337337
def postProcess(tree: Tree, copied: untpd.Tree): copied.ThisTree[Type] =
338338
copied.withTypeUnchecked(tree.tpe)
339339

340+
override def Select(tree: Tree)(qualifier: Tree, name: Name)(implicit ctx: Context): Select = {
341+
val tree1 = untpd.cpy.Select(tree)(qualifier, name)
342+
tree match {
343+
case tree: Select if (qualifier.tpe eq tree.qualifier.tpe) => tree1.withTypeUnchecked(tree.tpe)
344+
case _ => tree.tpe match {
345+
case tpe: NamedType => tree1.withType(tpe.derivedSelect(qualifier.tpe))
346+
case _ => tree1.withTypeUnchecked(tree.tpe)
347+
}
348+
}
349+
}
350+
351+
override def Apply(tree: Tree)(fun: Tree, args: List[Tree])(implicit ctx: Context): Apply = {
352+
val tree1 = untpd.cpy.Apply(tree)(fun, args)
353+
tree match {
354+
case tree: Apply if (fun.tpe.widen eq tree.fun.tpe.widen) && sameTypes(args, tree.args) => tree1.withTypeUnchecked(tree.tpe)
355+
case _ => ta.assignType(tree1, fun, args)
356+
}
357+
}
358+
359+
override def TypeApply(tree: Tree)(fun: Tree, args: List[Tree])(implicit ctx: Context): TypeApply = {
360+
val tree1 = untpd.cpy.TypeApply(tree)(fun, args)
361+
tree match {
362+
case tree: TypeApply if (fun.tpe.widen eq tree.fun.tpe.widen) && sameTypes(args, tree.args) => tree1.withTypeUnchecked(tree.tpe)
363+
case _ => ta.assignType(tree1, fun, args)
364+
}
365+
}
366+
367+
override def Literal(tree: Tree)(const: Constant)(implicit ctx: Context): Literal =
368+
ta.assignType(untpd.cpy.Literal(tree)(const))
369+
370+
override def New(tree: Tree)(tpt: Tree)(implicit ctx: Context): New =
371+
ta.assignType(untpd.cpy.New(tree)(tpt), tpt)
372+
340373
override def Pair(tree: Tree)(left: Tree, right: Tree)(implicit ctx: Context): Pair = {
341374
val tree1 = untpd.cpy.Pair(tree)(left, right)
342375
tree match {
@@ -345,6 +378,15 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
345378
}
346379
}
347380

381+
override def Typed(tree: Tree)(expr: Tree, tpt: Tree)(implicit ctx: Context): Typed =
382+
ta.assignType(untpd.cpy.Typed(tree)(expr, tpt), tpt)
383+
384+
override def NamedArg(tree: Tree)(name: Name, arg: Tree)(implicit ctx: Context): NamedArg =
385+
ta.assignType(untpd.cpy.NamedArg(tree)(name, arg), arg)
386+
387+
override def Assign(tree: Tree)(lhs: Tree, rhs: Tree)(implicit ctx: Context): Assign =
388+
ta.assignType(untpd.cpy.Assign(tree)(lhs, rhs))
389+
348390
override def Block(tree: Tree)(stats: List[Tree], expr: Tree)(implicit ctx: Context): Block = {
349391
val tree1 = untpd.cpy.Block(tree)(stats, expr)
350392
tree match {
@@ -361,6 +403,14 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
361403
}
362404
}
363405

406+
override def Closure(tree: Tree)(env: List[Tree], meth: Tree, tpt: Tree)(implicit ctx: Context): Closure = {
407+
val tree1 = untpd.cpy.Closure(tree)(env, meth, tpt)
408+
tree match {
409+
case tree: Closure if (meth.tpe.widen eq tree.meth.tpe.widen) && (tpt eq tree.tpt) => tree1.withTypeUnchecked(tree.tpe)
410+
case _ => ta.assignType(tree1, meth, tpt)
411+
}
412+
}
413+
364414
override def Match(tree: Tree)(selector: Tree, cases: List[CaseDef])(implicit ctx: Context): Match = {
365415
val tree1 = untpd.cpy.Match(tree)(selector, cases)
366416
tree match {
@@ -377,6 +427,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
377427
}
378428
}
379429

430+
override def Return(tree: Tree)(expr: Tree, from: Tree)(implicit ctx: Context): Return =
431+
ta.assignType(untpd.cpy.Return(tree)(expr, from))
432+
380433
override def Try(tree: Tree)(expr: Tree, handler: Tree, finalizer: Tree)(implicit ctx: Context): Try = {
381434
val tree1 = untpd.cpy.Try(tree)(expr, handler, finalizer)
382435
tree match {
@@ -385,6 +438,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
385438
}
386439
}
387440

441+
override def Throw(tree: Tree)(expr: Tree)(implicit ctx: Context): Throw =
442+
ta.assignType(untpd.cpy.Throw(tree)(expr))
443+
388444
override def SeqLiteral(tree: Tree)(elems: List[Tree])(implicit ctx: Context): SeqLiteral = {
389445
val tree1 = untpd.cpy.SeqLiteral(tree)(elems)
390446
tree match {
@@ -401,19 +457,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
401457
}
402458
}
403459

404-
override def Select(tree: Tree)(qualifier: Tree, name: Name)(implicit ctx: Context): Select = {
405-
val tree1 = untpd.cpy.Select(tree)(qualifier, name)
406-
tree match {
407-
case tree: Select if (qualifier.tpe eq tree.qualifier.tpe) => tree1.withTypeUnchecked(tree.tpe)
408-
case _ => tree.tpe match {
409-
case tpe: NamedType => tree1.withType(tpe.derivedSelect(qualifier.tpe))
410-
case _ => tree1.withTypeUnchecked(tree.tpe)
411-
}
412-
}
413-
}
414-
415460
override def If(tree: If)(cond: Tree = tree.cond, thenp: Tree = tree.thenp, elsep: Tree = tree.elsep)(implicit ctx: Context): If =
416461
If(tree: Tree)(cond, thenp, elsep)
462+
override def Closure(tree: Closure)(env: List[Tree] = tree.env, meth: Tree = tree.meth, tpt: Tree = tree.tpt)(implicit ctx: Context): Closure =
463+
Closure(tree: Tree)(env, meth, tpt)
417464
override def CaseDef(tree: CaseDef)(pat: Tree = tree.pat, guard: Tree = tree.guard, body: Tree = tree.body)(implicit ctx: Context): CaseDef =
418465
CaseDef(tree: Tree)(pat, guard, body)
419466
override def Try(tree: Try)(expr: Tree = tree.expr, handler: Tree = tree.handler, finalizer: Tree = tree.finalizer)(implicit ctx: Context): Try =

src/dotty/tools/dotc/transform/SuperAccessors.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ class SuperAccessors extends MacroTransform with IdentityDenotTransformer { this
391391

392392
case Apply(fn, args) =>
393393
val MethodType(_, formals) = fn.tpe.widen
394-
cpy.Apply(tree)(transform(fn), transformArgs(formals, args))
394+
ctx.atPhase(thisTransformer.next) { implicit ctx =>
395+
cpy.Apply(tree)(transform(fn), transformArgs(formals, args))
396+
}
395397

396398
case _ =>
397399
super.transform(tree)

0 commit comments

Comments
 (0)