From 70eb9074f7a65ff2f7de0ee5c125eca732c34fb8 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 20 Jun 2018 16:00:14 +0200 Subject: [PATCH 1/3] Do not print package prefix for object `this` references --- library/src/scala/tasty/util/ShowSourceCode.scala | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index d57cb8233f2c..8ec4ab40b8da 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -853,10 +853,18 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty printType(tp) case Type.ThisType(tp) => - printType(tp) tp match { - case Type.SymRef(cdef @ ClassDef(_, _, _, _, _), _) if !cdef.flags.isObject => this += ".this" - case _ => this + case Type.SymRef(cdef @ ClassDef(name, _, _, _, _), prefix) if !cdef.flags.isObject => + def printPrefix(prefix: TypeOrBounds): Unit = prefix match { + case Type.SymRef(ClassDef(name, _, _, _, _), prefix2) => + printPrefix(prefix2) + this += name += "." + case _ => + } + printPrefix(prefix) + this += name + this += ".this" + case _ => printType(tp) } case Type.TypeLambda(paramNames, tparams, body) => From b9159b7830dbb6a0218ef31b0b3eafac0dd4ac50 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 20 Jun 2018 17:46:58 +0200 Subject: [PATCH 2/3] Print self types --- .../src/scala/tasty/util/ShowSourceCode.scala | 18 +++++++++++++++++- tests/pos/selftypes.decompiled | 12 ++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/pos/selftypes.decompiled diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index 8ec4ab40b8da..47722fa9b65a 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -145,14 +145,30 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty case stat@Import(_, _) => stat case stat@Term() => stat } - if (stats1.nonEmpty) { + + def printBody(printSelf: Boolean) = { this += " {" indented { + if (printSelf) { + val Some(ValDef(name, tpt, _)) = self + indented { + val name1 = if (name == "_") "this" else name + this += " " += name1 += ": " + printTypeTree(tpt) + this += " =>" + } + } this += lineBreak() printTrees(stats1, lineBreak()) } this += lineBreak() += "}" } + self match { + case Some(ValDef(_, TypeTree.Singleton(_), _)) => + if (stats1.nonEmpty) printBody(printSelf = false) + case Some(ValDef(_, _, _)) => printBody(printSelf = true) + case _ => if (stats1.nonEmpty) printBody(printSelf = false) + } this case tdef @ TypeDef(name, rhs) => diff --git a/tests/pos/selftypes.decompiled b/tests/pos/selftypes.decompiled new file mode 100644 index 000000000000..4280791ff2fb --- /dev/null +++ b/tests/pos/selftypes.decompiled @@ -0,0 +1,12 @@ +/** Decompiled from out/posTestFromTasty/pos/selftypes/selftypes.class */ +object selftypes { + trait A() extends java.lang.Object { self: selftypes.AB => + type AA = scala.List[this.BX] + class AX() + } + trait B() extends java.lang.Object { self: selftypes.AB => + type BB = B.this.AA + class BX() + } + class AB() extends selftypes.A with selftypes.B +} From b12285e46a4dda3e366758e3a286dbc84f11d0d7 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 21 Jun 2018 13:36:24 +0200 Subject: [PATCH 3/3] Add new lines for redability --- library/src/scala/tasty/util/ShowSourceCode.scala | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index 47722fa9b65a..d7cd6c3c95dd 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -165,9 +165,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty } self match { case Some(ValDef(_, TypeTree.Singleton(_), _)) => - if (stats1.nonEmpty) printBody(printSelf = false) - case Some(ValDef(_, _, _)) => printBody(printSelf = true) - case _ => if (stats1.nonEmpty) printBody(printSelf = false) + if (stats1.nonEmpty) + printBody(printSelf = false) + case Some(ValDef(_, _, _)) => + printBody(printSelf = true) + case _ => + if (stats1.nonEmpty) + printBody(printSelf = false) } this