Skip to content

Commit 2548ef0

Browse files
Merge pull request #4717 from dotty-staging/quote-show-refresh-names
Refresh names inExpr.show
2 parents 385082b + a694387 commit 2548ef0

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dotty.tools.dotc.core.Phases.Phase
88
class QuoteDecompiler(output: tpd.Tree => Context => Unit) extends QuoteCompiler {
99
override def phases: List[List[Phase]] = List(
1010
List(new QuotedFrontend(putInClass = false)), // Create class from Expr
11+
List(new RefreshNames),
1112
List(new QuoteTreeOutput(output))
1213
)
1314

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dotty.tools.dotc.quoted
2+
3+
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.core.Contexts.Context
5+
import dotty.tools.dotc.core.DenotTransformers.SymTransformer
6+
import dotty.tools.dotc.core.Flags._
7+
import dotty.tools.dotc.core.NameKinds.{NumberedInfo, UniqueName}
8+
import dotty.tools.dotc.core.SymDenotations.SymDenotation
9+
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
10+
11+
/** Refreshes local names starting from the second use of the name. Intended for readability of the pretty printed code. */
12+
class RefreshNames extends MiniPhase with SymTransformer {
13+
14+
def phaseName: String = "RefreshNames"
15+
16+
override def transformValDef(tree: tpd.ValDef)(implicit ctx: Context): tpd.Tree =
17+
tpd.ValDef(tree.symbol.asTerm, tree.rhs)
18+
19+
override def transformDefDef(tree: tpd.DefDef)(implicit ctx: Context): tpd.Tree =
20+
tpd.DefDef(tree.symbol.asTerm, tree.rhs)
21+
22+
override def transformTypeDef(tree: tpd.TypeDef)(implicit ctx: Context): tpd.Tree = {
23+
val newTypeDef = tpd.TypeDef(tree.symbol.asType)
24+
// keep rhs to keep `type T = ...` instead of `type T >: ... <: ...`
25+
cpy.TypeDef(newTypeDef)(rhs = tree.rhs)
26+
}
27+
28+
def transformSym(ref: SymDenotation)(implicit ctx: Context): SymDenotation = {
29+
if (ref.is(Package) || ref.isClass || ref.owner != ctx.owner || ref.is(Label) || ref.is(Param)) ref
30+
else {
31+
val newName = UniqueName.fresh(ref.symbol.name.toTermName)
32+
newName.info match {
33+
case info: NumberedInfo if info.num == 1 => ref // Keep the first reference as is to avoid renaming if the code has no duplicated names
34+
case _ => ref.copySymDenotation(name = if (ref.symbol.isType) newName.toTypeName else newName)
35+
}
36+
}
37+
}
38+
}

tests/run-with-compiler/quote-run-2.check

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@
88
val y: scala.Double = 5.0.*(5.0)
99
y
1010
})
11+
{
12+
val y: scala.Double = 5.0.*(5.0)
13+
y.*({
14+
val y$2: scala.Double = y.*(y)
15+
y$2.*({
16+
val y$3: scala.Double = y$2.*(y$2)
17+
val y$4: scala.Double = y$3.*(y$3)
18+
y$4
19+
})
20+
})
21+
}

tests/run-with-compiler/quote-run-2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ object Test {
1717
println(powerCode(1, '(5)).show)
1818
println(powerCode(2, '(5)).show)
1919
println(powerCode(3, '(5)).show)
20+
println(powerCode(22, '(5)).show)
2021
}
2122
}

0 commit comments

Comments
 (0)