Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDecompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dotty.tools.dotc.core.Phases.Phase
class QuoteDecompiler(output: tpd.Tree => Context => Unit) extends QuoteCompiler {
override def phases: List[List[Phase]] = List(
List(new QuotedFrontend(putInClass = false)), // Create class from Expr
List(new RefreshNames),
List(new QuoteTreeOutput(output))
)

Expand Down
38 changes: 38 additions & 0 deletions compiler/src/dotty/tools/dotc/quoted/RefreshNames.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.DenotTransformers.SymTransformer
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.NameKinds.{NumberedInfo, UniqueName}
import dotty.tools.dotc.core.SymDenotations.SymDenotation
import dotty.tools.dotc.transform.MegaPhase.MiniPhase

/** Refreshes local names starting from the second use of the name. Intended for readability of the pretty printed code. */
class RefreshNames extends MiniPhase with SymTransformer {

def phaseName: String = "RefreshNames"

override def transformValDef(tree: tpd.ValDef)(implicit ctx: Context): tpd.Tree =
tpd.ValDef(tree.symbol.asTerm, tree.rhs)

override def transformDefDef(tree: tpd.DefDef)(implicit ctx: Context): tpd.Tree =
tpd.DefDef(tree.symbol.asTerm, tree.rhs)

override def transformTypeDef(tree: tpd.TypeDef)(implicit ctx: Context): tpd.Tree = {
val newTypeDef = tpd.TypeDef(tree.symbol.asType)
// keep rhs to keep `type T = ...` instead of `type T >: ... <: ...`
cpy.TypeDef(newTypeDef)(rhs = tree.rhs)
}

def transformSym(ref: SymDenotation)(implicit ctx: Context): SymDenotation = {
if (ref.is(Package) || ref.isClass || ref.owner != ctx.owner || ref.is(Label) || ref.is(Param)) ref
else {
val newName = UniqueName.fresh(ref.symbol.name.toTermName)
newName.info match {
case info: NumberedInfo if info.num == 1 => ref // Keep the first reference as is to avoid renaming if the code has no duplicated names
case _ => ref.copySymDenotation(name = if (ref.symbol.isType) newName.toTypeName else newName)
}
}
}
}
11 changes: 11 additions & 0 deletions tests/run-with-compiler/quote-run-2.check
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@
val y: scala.Double = 5.0.*(5.0)
y
})
{
val y: scala.Double = 5.0.*(5.0)
y.*({
val y$2: scala.Double = y.*(y)
y$2.*({
val y$3: scala.Double = y$2.*(y$2)
val y$4: scala.Double = y$3.*(y$3)
y$4
})
})
}
1 change: 1 addition & 0 deletions tests/run-with-compiler/quote-run-2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ object Test {
println(powerCode(1, '(5)).show)
println(powerCode(2, '(5)).show)
println(powerCode(3, '(5)).show)
println(powerCode(22, '(5)).show)
}
}