From 9bf9e9dbcc1967f997c83aa6d1ef264c074c94be Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 31 May 2018 11:42:08 +0200 Subject: [PATCH 1/7] Fix wrong extractor --- library/src/scala/tasty/util/ShowSourceCode.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index 584880e5b8b2..34cd46232ac5 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -541,8 +541,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty printTypeTree(lo) this += " <: " printTypeTree(hi) - case tpt@Type() => - printType(tpt) + case tpt @ TypeTree() => + printTypeTree(tpt) } def printTypeTree(tree: TypeTree): Buffer = tree match { From 82d4d0f17a7e4e748265a0c562772a5c087dc92d Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 31 May 2018 11:41:30 +0200 Subject: [PATCH 2/7] Add show on Tasty.Pattern --- compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala | 7 ++++--- library/src/scala/tasty/Tasty.scala | 4 +++- library/src/scala/tasty/util/Show.scala | 2 ++ library/src/scala/tasty/util/ShowExtractors.scala | 3 +++ library/src/scala/tasty/util/ShowSourceCode.scala | 3 +++ tests/run/tasty-custom-show/quoted_1.scala | 1 + 6 files changed, 16 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala b/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala index e5c824f06fc6..84420409b822 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala @@ -454,9 +454,10 @@ object TastyImpl extends scala.tasty.Tasty { type Pattern = tpd.Tree - def PatternDeco(x: Pattern): AbstractPattern = new AbstractPattern { - def pos(implicit ctx: Context): Position = x.pos - def tpe(implicit ctx: Context): Types.Type = x.tpe.stripTypeVar + def PatternDeco(pattern: Pattern): AbstractPattern = new AbstractPattern { + def show(implicit ctx: Context, s: Show[TastyImpl.this.type]): String = s.showPattern(pattern) + def pos(implicit ctx: Context): Position = pattern.pos + def tpe(implicit ctx: Context): Types.Type = pattern.tpe.stripTypeVar } def patternClassTag: ClassTag[Pattern] = implicitly[ClassTag[Pattern]] diff --git a/library/src/scala/tasty/Tasty.scala b/library/src/scala/tasty/Tasty.scala index 03b260bf3aaa..7ae49e9467d8 100644 --- a/library/src/scala/tasty/Tasty.scala +++ b/library/src/scala/tasty/Tasty.scala @@ -324,7 +324,9 @@ abstract class Tasty { tasty => type Pattern - trait AbstractPattern extends Typed with Positioned + trait AbstractPattern extends Typed with Positioned { + def show(implicit ctx: Context, s: Show[tasty.type]): String + } implicit def PatternDeco(x: Pattern): AbstractPattern implicit def patternClassTag: ClassTag[Pattern] diff --git a/library/src/scala/tasty/util/Show.scala b/library/src/scala/tasty/util/Show.scala index 7d6f1cccf28b..e1beb41e59da 100644 --- a/library/src/scala/tasty/util/Show.scala +++ b/library/src/scala/tasty/util/Show.scala @@ -8,6 +8,8 @@ abstract class Show[T <: Tasty with Singleton](val tasty: T) { def showCaseDef(caseDef: tasty.CaseDef)(implicit ctx: tasty.Context): String + def showPattern(pattern: tasty.Pattern)(implicit ctx: tasty.Context): String + def showTypeOrBoundsTree(tpt: tasty.TypeOrBoundsTree)(implicit ctx: tasty.Context): String def showTypeOrBounds(tpe: tasty.TypeOrBounds)(implicit ctx: tasty.Context): String diff --git a/library/src/scala/tasty/util/ShowExtractors.scala b/library/src/scala/tasty/util/ShowExtractors.scala index 46dc8966478e..2edbdafb5f11 100644 --- a/library/src/scala/tasty/util/ShowExtractors.scala +++ b/library/src/scala/tasty/util/ShowExtractors.scala @@ -11,6 +11,9 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String = new Buffer().visitCaseDef(caseDef).result() + def showPattern(pattern: tasty.Pattern)(implicit ctx: tasty.Context): String = + new Buffer().visitPattern(pattern).result() + def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String = new Buffer().visitTypeTree(tpt).result() diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index 34cd46232ac5..cb041b5cb223 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -10,6 +10,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String = (new Buffer).printCaseDef(caseDef).result() + def showPattern(pattern: Pattern)(implicit ctx: Context): String = + (new Buffer).printPattern(pattern).result() + def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String = (new Buffer).printTypeOrBoundsTree(tpt).result() diff --git a/tests/run/tasty-custom-show/quoted_1.scala b/tests/run/tasty-custom-show/quoted_1.scala index ba50cfbb3f27..e7897f81ab7a 100644 --- a/tests/run/tasty-custom-show/quoted_1.scala +++ b/tests/run/tasty-custom-show/quoted_1.scala @@ -49,6 +49,7 @@ class DummyShow[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) { import tasty._ def showTree(tree: Tree)(implicit ctx: Context): String = "Tree" def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String = "CaseDef" + def showPattern(pattern: Pattern)(implicit ctx: Context): String = "Pattern" def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String = "TypeOrBoundsTree" def showTypeOrBounds(tpe: TypeOrBounds)(implicit ctx: Context): String = "TypeOrBounds" def showConstant(const: Constant)(implicit ctx: Context): String = "Constant" From a9f3e952100fb3e4071584206c9ae76e27236861 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 30 May 2018 16:39:36 +0200 Subject: [PATCH 3/7] Do not print packages --- library/src/scala/tasty/util/ShowSourceCode.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index cb041b5cb223..8188e9279772 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -631,7 +631,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty case Type.TermRef(name, prefix) => prefix match { - case prefix@Type() => + case Type.ThisType(Types.EmptyPackage()) => + this += name + case prefix @ Type() => printType(prefix) if (name != "package") this += "." += name @@ -642,7 +644,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty case Type.TypeRef(name, prefix) => prefix match { - case NoPrefix() => + case NoPrefix() | Type.ThisType(Types.EmptyPackage()) => case prefix@Type() => printType(prefix) this += "." From cfed294f110beff736c09f8c8b1bf3aa3edae147 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 31 May 2018 12:45:59 +0200 Subject: [PATCH 4/7] Fix extractors printing --- .../src/scala/tasty/util/ShowSourceCode.scala | 9 +++- tests/pos/simpleExractors.decompiled | 43 +++++++++++++++++++ tests/pos/simpleExractors.scala | 28 ++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tests/pos/simpleExractors.decompiled create mode 100644 tests/pos/simpleExractors.scala diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index 8188e9279772..d4e29dd9e4fe 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -511,7 +511,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty printPattern(pattern) case Pattern.Unapply(fun, implicits, patterns) => - printTree(fun) + fun match { + case Term.Select(extractor, "unapply" | "unapplySeq", _) => printTree(extractor) + case Term.TypeApply(Term.Select(extractor, "unapply" | "unapplySeq", _), _) => printTree(extractor) + case _ => throw new MatchError(fun.show) + } this += "(" printPatterns(patterns, ", ") this += ")" @@ -520,7 +524,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty printPatterns(trees, " | ") case Pattern.TypeTest(tpt) => - this + this += "_: " + printTypeOrBoundsTree(tpt) } diff --git a/tests/pos/simpleExractors.decompiled b/tests/pos/simpleExractors.decompiled new file mode 100644 index 000000000000..914fc8171ca0 --- /dev/null +++ b/tests/pos/simpleExractors.decompiled @@ -0,0 +1,43 @@ +/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Bar.class */ +object Bar { + def unapply(arg: scala.Any): scala.Option[scala.Any] = scala.Some.apply[scala.Any](arg) +}/** Decompiled from out/posTestFromTasty/pos/simpleExractors/BarSeq.class */ +object BarSeq { + def unapplySeq(arg: scala.Any): scala.Option[scala.Seq[scala.Any]] = scala.Some.apply[collection.immutable.List[scala.Any]](scala.List.apply[scala.Any](arg)) +}/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Baz.class */ +object Baz { + def unapply[T](arg: T): scala.Option[T] = scala.Some.apply[T](arg) +}/** Decompiled from out/posTestFromTasty/pos/simpleExractors/BazSeq.class */ +object BazSeq { + def unapplySeq[T](arg: T): scala.Option[scala.Seq[T]] = scala.Some.apply[collection.immutable.List[T]](scala.List.apply[T](arg)) +}/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Foo.class */ +class Foo() { + def bar(x: scala.Any): scala.Unit = x match { + case Bar(a) => + { + scala.Predef.println(a) + } + case BarSeq(a) => + { + scala.Predef.println(a) + } + case BarSeq(a, b) => + { + scala.Predef.println(a) + } + } + def baz(x: scala.Any): scala.Unit = x match { + case Baz(a) => + { + scala.Predef.println(a) + } + case BazSeq(a) => + { + scala.Predef.println(a) + } + case BazSeq(a, b) => + { + scala.Predef.println(a) + } + } +} diff --git a/tests/pos/simpleExractors.scala b/tests/pos/simpleExractors.scala new file mode 100644 index 000000000000..05bde494079d --- /dev/null +++ b/tests/pos/simpleExractors.scala @@ -0,0 +1,28 @@ +class Foo { + def bar(x: Any): Unit = x match { + case Bar(a) => println(a) + case BarSeq(a) => println(a) + case BarSeq(a, b) => println(a) + } + def baz(x: Any): Unit = x match { + case Baz(a) => println(a) + case BazSeq(a) => println(a) + case BazSeq(a, b) => println(a) + } +} + +object Bar { + def unapply(arg: Any): Option[Any] = Some(arg) +} + +object BarSeq { + def unapplySeq(arg: Any): Option[Seq[Any]] = Some(List(arg)) +} + +object Baz { + def unapply[T](arg: T): Option[T] = Some(arg) +} + +object BazSeq { + def unapplySeq[T](arg: T): Option[Seq[T]] = Some(List(arg)) +} From 4bc6e078a2b35a2951d942522a95eaec246829fe Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 31 May 2018 13:02:33 +0200 Subject: [PATCH 5/7] Flatten blocks in rhs of pattern matches --- .../src/scala/tasty/util/ShowSourceCode.scala | 8 ++++++- tests/pos/simpleExractors.decompiled | 24 +++++-------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index d4e29dd9e4fe..096e75acc16b 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -487,7 +487,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty this += " =>" indented { this += lineBreak() - printTree(body) + body match { + case Term.Block(stats, expr) => + printTrees(stats, lineBreak()) + printTree(expr) + case body => + printTree(body) + } } this } diff --git a/tests/pos/simpleExractors.decompiled b/tests/pos/simpleExractors.decompiled index 914fc8171ca0..cb17a6a2c5ef 100644 --- a/tests/pos/simpleExractors.decompiled +++ b/tests/pos/simpleExractors.decompiled @@ -14,30 +14,18 @@ object BazSeq { class Foo() { def bar(x: scala.Any): scala.Unit = x match { case Bar(a) => - { - scala.Predef.println(a) - } + scala.Predef.println(a) case BarSeq(a) => - { - scala.Predef.println(a) - } + scala.Predef.println(a) case BarSeq(a, b) => - { - scala.Predef.println(a) - } + scala.Predef.println(a) } def baz(x: scala.Any): scala.Unit = x match { case Baz(a) => - { - scala.Predef.println(a) - } + scala.Predef.println(a) case BazSeq(a) => - { - scala.Predef.println(a) - } + scala.Predef.println(a) case BazSeq(a, b) => - { - scala.Predef.println(a) - } + scala.Predef.println(a) } } From 38e098937eb35bf7edef12e7ef54d25a943a6527 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 31 May 2018 13:07:57 +0200 Subject: [PATCH 6/7] Fix missing line break after top level class definitions --- .../tools/dotc/decompiler/DecompilationPrinter.scala | 2 +- library/src/scala/tasty/util/ShowSourceCode.scala | 2 +- tests/pos/simpleExractors.decompiled | 12 ++++++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala b/compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala index ad95bcd57429..707f319dce51 100644 --- a/compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala +++ b/compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala @@ -41,7 +41,7 @@ class DecompilationPrinter extends Phase { new TastyPrinter(unit.pickled.head._2).printContents() } else { out.println(s"/** Decompiled from $unit */") - out.print(TastyImpl.showSourceCode.showTree(unit.tpdTree)(ctx)) + out.println(TastyImpl.showSourceCode.showTree(unit.tpdTree)(ctx)) } } } diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index 096e75acc16b..1a2b9ee5f446 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -52,7 +52,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty this += lineBreak() printTrees(stats1, lineBreak()) } - this += lineBreak() += "}" += lineBreak() + this += lineBreak() += "}" } case Import(expr, selectors) => diff --git a/tests/pos/simpleExractors.decompiled b/tests/pos/simpleExractors.decompiled index cb17a6a2c5ef..e432e5a99d65 100644 --- a/tests/pos/simpleExractors.decompiled +++ b/tests/pos/simpleExractors.decompiled @@ -1,16 +1,20 @@ /** Decompiled from out/posTestFromTasty/pos/simpleExractors/Bar.class */ object Bar { def unapply(arg: scala.Any): scala.Option[scala.Any] = scala.Some.apply[scala.Any](arg) -}/** Decompiled from out/posTestFromTasty/pos/simpleExractors/BarSeq.class */ +} +/** Decompiled from out/posTestFromTasty/pos/simpleExractors/BarSeq.class */ object BarSeq { def unapplySeq(arg: scala.Any): scala.Option[scala.Seq[scala.Any]] = scala.Some.apply[collection.immutable.List[scala.Any]](scala.List.apply[scala.Any](arg)) -}/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Baz.class */ +} +/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Baz.class */ object Baz { def unapply[T](arg: T): scala.Option[T] = scala.Some.apply[T](arg) -}/** Decompiled from out/posTestFromTasty/pos/simpleExractors/BazSeq.class */ +} +/** Decompiled from out/posTestFromTasty/pos/simpleExractors/BazSeq.class */ object BazSeq { def unapplySeq[T](arg: T): scala.Option[scala.Seq[T]] = scala.Some.apply[collection.immutable.List[T]](scala.List.apply[T](arg)) -}/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Foo.class */ +} +/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Foo.class */ class Foo() { def bar(x: scala.Any): scala.Unit = x match { case Bar(a) => From 6507d252346dfb3fbbb03514e0a49737e6a018d3 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 31 May 2018 13:12:16 +0200 Subject: [PATCH 7/7] If a pattern is missing print the extractors needed --- library/src/scala/tasty/util/ShowSourceCode.scala | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index 1a2b9ee5f446..7e3987a5a6a1 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -533,6 +533,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty this += "_: " printTypeOrBoundsTree(tpt) + case _ => + throw new MatchError(pattern.show) + } def printConstant(const: Constant): Buffer = const match {