@@ -4,25 +4,43 @@ import dotty.tools.dotc.ast.Trees._
4
4
import dotty .tools .dotc .ast .tpd
5
5
import dotty .tools .dotc .core .Contexts ._
6
6
import dotty .tools .dotc .core .Constants ._
7
+ import dotty .tools .dotc .core .Symbols ._
8
+ import dotty .tools .dotc .core .Types ._
7
9
8
10
/** Clean up quote artifacts from the tree to make it simpler to read.
9
11
* - Flattens block and remove blocks with not statements
12
+ * - Inline type aliases in the tree
10
13
*/
11
14
class TreeCleaner extends tpd.TreeMap {
12
15
import tpd ._
13
16
14
- override def transform (tree : Tree )(implicit ctx : Context ): Tree = super .transform(tree) match {
15
- case Block (Nil , expr1) => expr1
16
- case Block (stats1, expr1) =>
17
- val flatStats = stats1.flatMap {
18
- case Block (stats2, expr2) => stats2 ::: expr2 :: Nil
19
- case Literal (Constant (())) => Nil
20
- case stat => stat :: Nil
21
- }
22
- expr1 match {
23
- case Block (stats3, expr3) => Block (flatStats ::: stats3, expr3)
24
- case expr3 => Block (flatStats, expr3)
25
- }
26
- case tree1 => tree1
17
+ /** List of symbols and their types for type aliases `type T = U` */
18
+ private [this ] var aliasesSyms : List [Symbol ] = Nil
19
+ private [this ] var aliasesTypes : List [Type ] = Nil
20
+
21
+ override def transform (tree : Tree )(implicit ctx : Context ): Tree = {
22
+ val tree0 = tree match {
23
+ case TypeDef (_, TypeBoundsTree (lo, hi)) if lo == hi =>
24
+ aliasesSyms = tree.symbol :: aliasesSyms
25
+ aliasesTypes = lo.tpe :: aliasesTypes
26
+ Literal (Constant (()))
27
+ case _ => tree
28
+ }
29
+
30
+ super .transform(tree0) match {
31
+ case Block (Nil , expr1) => expr1
32
+ case Block (stats1, expr1) =>
33
+ val flatStats = stats1.flatMap {
34
+ case Block (stats2, expr2) => stats2 ::: expr2 :: Nil
35
+ case Literal (Constant (())) => Nil
36
+ case stat => stat :: Nil
37
+ }
38
+ expr1 match {
39
+ case Block (stats3, expr3) => Block (flatStats ::: stats3, expr3)
40
+ case expr3 => Block (flatStats, expr3)
41
+ }
42
+ case tree1 : TypeTree => TypeTree (tree1.tpe.subst(aliasesSyms, aliasesTypes))
43
+ case tree1 => tree1
44
+ }
27
45
}
28
46
}
0 commit comments