Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit 4f5dbd8

Browse files
committed
Print type keyword when the type of an object should be printed
I would prefer to fix this somewhere in the printing logic and not in the construction logic of the tree but I didn't find a way to differentiate the Ident trees correctly in the printer. For object X { def x = 0 } the Ident trees of `X.type` and `X.x` seem to have the same shape at the beginning. Fixes #1002233
1 parent afafa18 commit 4f5dbd8

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

org.scala-refactoring.library/src/main/scala/scala/tools/refactoring/transformation/TreeFactory.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ trait TreeFactory {
6464

6565
def mkReturn(s: List[Symbol]): Tree = s match {
6666
case Nil => EmptyTree
67-
case x :: Nil => Ident(x) setType x.tpe
67+
case x :: Nil =>
68+
val ident = if (x.isModuleClass) Ident(newTermName(s"${x.name}.type")) else Ident(x)
69+
ident setType x.tpe
6870
case xs =>
6971
typer.typed(gen.mkTuple(xs map (s => Ident(s) setType s.tpe))) match {
7072
case t: Apply =>
@@ -114,7 +116,7 @@ trait TreeFactory {
114116
}
115117

116118
if (mods != NoMods) valOrVarDef setSymbol NoSymbol.newValue(name, newFlags = mods.flags) else valOrVarDef
117-
}
119+
}
118120

119121
def mkParam(name: String, tpe: Type, defaultVal: Tree = EmptyTree): ValDef = {
120122
ValDef(Modifiers(Flags.PARAM), newTermName(name), TypeTree(tpe), defaultVal)

org.scala-refactoring.library/src/test/scala/scala/tools/refactoring/tests/sourcegen/ReusingPrinterTest.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,23 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
223223
d.copy(tpt = newTpt) replaces d
224224
}}}
225225

226+
@Test
227+
def add_type_keyword_to_return_type_when_it_represents_an_object() = """
228+
package add_type_keyword_to_return_type_when_it_represents_an_object
229+
object X {
230+
def o = X
231+
}
232+
""" becomes """
233+
package add_type_keyword_to_return_type_when_it_represents_an_object
234+
object X {
235+
def o: X.type = X
236+
}
237+
""" after topdown { matchingChildren { transform {
238+
case d @ DefDef(_, _, _, _, tpt: TypeTree, _) =>
239+
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol))
240+
d.copy(tpt = newTpt) replaces d
241+
}}}
242+
226243
@Test
227244
def add_override_flag() = """
228245
package add_override_flag

0 commit comments

Comments
 (0)