Skip to content

Commit 8216c5a

Browse files
committed
Inline type aliases in the tree
Removes alsiases added for spliced implicit quoted.Type added in the quotes.
1 parent 2a34f06 commit 8216c5a

File tree

5 files changed

+44
-35
lines changed

5 files changed

+44
-35
lines changed

compiler/src/dotty/tools/dotc/quoted/TreeCleaner.scala

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,43 @@ import dotty.tools.dotc.ast.Trees._
44
import dotty.tools.dotc.ast.tpd
55
import dotty.tools.dotc.core.Contexts._
66
import dotty.tools.dotc.core.Constants._
7+
import dotty.tools.dotc.core.Symbols._
8+
import dotty.tools.dotc.core.Types._
79

810
/** Clean up quote artifacts from the tree to make it simpler to read.
911
* - Flattens block and remove blocks with not statements
12+
* - Inline type aliases in the tree
1013
*/
1114
class TreeCleaner extends tpd.TreeMap {
1215
import tpd._
1316

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+
}
2745
}
2846
}

tests/run-with-compiler/i3823-c.check

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{
2-
type T = Int
3-
{
4-
val z: T = 2
5-
()
6-
}
2+
val z: Int = 2
3+
()
74
}

tests/run-with-compiler/i3876-b.check

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
6
22
{
33
val x$1: Int = 3
4-
{
5-
def f(x: Int): Int = x.+(x)
6-
f(x$1)
7-
}
4+
def f(x: Int): Int = x.+(x)
5+
f(x$1)
86
}

tests/run-with-compiler/i3876-c.check

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
6
22
{
33
val x$1: Int = 3
4-
{
5-
val f:
6-
Function1[Int, Int]
7-
{
8-
def apply(x: Int): Int
9-
}
10-
=
4+
val f:
5+
Function1[Int, Int]
116
{
12-
(x: Int) => x.+(x)
7+
def apply(x: Int): Int
138
}
14-
(f: (x: Int) => Int).apply(x$1)
15-
}
9+
=
10+
{
11+
(x: Int) => x.+(x)
12+
}
13+
(f: (x: Int) => Int).apply(x$1)
1614
}

tests/run-with-compiler/i3876.check

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
6
22
{
33
val x$1: Int = 3
4-
{
5-
x$1.+(x$1)
6-
}
4+
x$1.+(x$1)
75
}

0 commit comments

Comments
 (0)