Skip to content

Commit f32aa36

Browse files
Merge pull request #4605 from dotty-staging/fix-printing-extractors
Fix printing extractors
2 parents 320583f + 6507d25 commit f32aa36

File tree

9 files changed

+104
-13
lines changed

9 files changed

+104
-13
lines changed

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class DecompilationPrinter extends Phase {
4141
new TastyPrinter(unit.pickled.head._2).printContents()
4242
} else {
4343
out.println(s"/** Decompiled from $unit */")
44-
out.print(TastyImpl.showSourceCode.showTree(unit.tpdTree)(ctx))
44+
out.println(TastyImpl.showSourceCode.showTree(unit.tpdTree)(ctx))
4545
}
4646
}
4747
}

compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,10 @@ object TastyImpl extends scala.tasty.Tasty {
454454

455455
type Pattern = tpd.Tree
456456

457-
def PatternDeco(x: Pattern): AbstractPattern = new AbstractPattern {
458-
def pos(implicit ctx: Context): Position = x.pos
459-
def tpe(implicit ctx: Context): Types.Type = x.tpe.stripTypeVar
457+
def PatternDeco(pattern: Pattern): AbstractPattern = new AbstractPattern {
458+
def show(implicit ctx: Context, s: Show[TastyImpl.this.type]): String = s.showPattern(pattern)
459+
def pos(implicit ctx: Context): Position = pattern.pos
460+
def tpe(implicit ctx: Context): Types.Type = pattern.tpe.stripTypeVar
460461
}
461462

462463
def patternClassTag: ClassTag[Pattern] = implicitly[ClassTag[Pattern]]

library/src/scala/tasty/Tasty.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ abstract class Tasty { tasty =>
324324

325325
type Pattern
326326

327-
trait AbstractPattern extends Typed with Positioned
327+
trait AbstractPattern extends Typed with Positioned {
328+
def show(implicit ctx: Context, s: Show[tasty.type]): String
329+
}
328330
implicit def PatternDeco(x: Pattern): AbstractPattern
329331

330332
implicit def patternClassTag: ClassTag[Pattern]

library/src/scala/tasty/util/Show.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ abstract class Show[T <: Tasty with Singleton](val tasty: T) {
88

99
def showCaseDef(caseDef: tasty.CaseDef)(implicit ctx: tasty.Context): String
1010

11+
def showPattern(pattern: tasty.Pattern)(implicit ctx: tasty.Context): String
12+
1113
def showTypeOrBoundsTree(tpt: tasty.TypeOrBoundsTree)(implicit ctx: tasty.Context): String
1214

1315
def showTypeOrBounds(tpe: tasty.TypeOrBounds)(implicit ctx: tasty.Context): String

library/src/scala/tasty/util/ShowExtractors.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
1111
def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String =
1212
new Buffer().visitCaseDef(caseDef).result()
1313

14+
def showPattern(pattern: tasty.Pattern)(implicit ctx: tasty.Context): String =
15+
new Buffer().visitPattern(pattern).result()
16+
1417
def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String =
1518
new Buffer().visitTypeTree(tpt).result()
1619

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
1010
def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String =
1111
(new Buffer).printCaseDef(caseDef).result()
1212

13+
def showPattern(pattern: Pattern)(implicit ctx: Context): String =
14+
(new Buffer).printPattern(pattern).result()
15+
1316
def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String =
1417
(new Buffer).printTypeOrBoundsTree(tpt).result()
1518

@@ -50,7 +53,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
5053
this += lineBreak()
5154
printTrees(stats1, lineBreak())
5255
}
53-
this += lineBreak() += "}" += lineBreak()
56+
this += lineBreak() += "}"
5457
}
5558

5659
case Import(expr, selectors) =>
@@ -485,7 +488,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
485488
this += " =>"
486489
indented {
487490
this += lineBreak()
488-
printTree(body)
491+
body match {
492+
case Term.Block(stats, expr) =>
493+
printTrees(stats, lineBreak())
494+
printTree(expr)
495+
case body =>
496+
printTree(body)
497+
}
489498
}
490499
this
491500
}
@@ -509,7 +518,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
509518
printPattern(pattern)
510519

511520
case Pattern.Unapply(fun, implicits, patterns) =>
512-
printTree(fun)
521+
fun match {
522+
case Term.Select(extractor, "unapply" | "unapplySeq", _) => printTree(extractor)
523+
case Term.TypeApply(Term.Select(extractor, "unapply" | "unapplySeq", _), _) => printTree(extractor)
524+
case _ => throw new MatchError(fun.show)
525+
}
513526
this += "("
514527
printPatterns(patterns, ", ")
515528
this += ")"
@@ -518,7 +531,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
518531
printPatterns(trees, " | ")
519532

520533
case Pattern.TypeTest(tpt) =>
521-
this
534+
this += "_: "
535+
printTypeOrBoundsTree(tpt)
536+
537+
case _ =>
538+
throw new MatchError(pattern.show)
522539

523540
}
524541

@@ -542,8 +559,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
542559
printTypeTree(lo)
543560
this += " <: "
544561
printTypeTree(hi)
545-
case tpt@Type() =>
546-
printType(tpt)
562+
case tpt @ TypeTree() =>
563+
printTypeTree(tpt)
547564
}
548565

549566
def printTypeTree(tree: TypeTree): Buffer = tree match {
@@ -629,7 +646,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
629646

630647
case Type.TermRef(name, prefix) =>
631648
prefix match {
632-
case prefix@Type() =>
649+
case Type.ThisType(Types.EmptyPackage()) =>
650+
this += name
651+
case prefix @ Type() =>
633652
printType(prefix)
634653
if (name != "package")
635654
this += "." += name
@@ -640,7 +659,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
640659

641660
case Type.TypeRef(name, prefix) =>
642661
prefix match {
643-
case NoPrefix() =>
662+
case NoPrefix() | Type.ThisType(Types.EmptyPackage()) =>
644663
case prefix@Type() =>
645664
printType(prefix)
646665
this += "."

tests/pos/simpleExractors.decompiled

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Bar.class */
2+
object Bar {
3+
def unapply(arg: scala.Any): scala.Option[scala.Any] = scala.Some.apply[scala.Any](arg)
4+
}
5+
/** Decompiled from out/posTestFromTasty/pos/simpleExractors/BarSeq.class */
6+
object BarSeq {
7+
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))
8+
}
9+
/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Baz.class */
10+
object Baz {
11+
def unapply[T](arg: T): scala.Option[T] = scala.Some.apply[T](arg)
12+
}
13+
/** Decompiled from out/posTestFromTasty/pos/simpleExractors/BazSeq.class */
14+
object BazSeq {
15+
def unapplySeq[T](arg: T): scala.Option[scala.Seq[T]] = scala.Some.apply[collection.immutable.List[T]](scala.List.apply[T](arg))
16+
}
17+
/** Decompiled from out/posTestFromTasty/pos/simpleExractors/Foo.class */
18+
class Foo() {
19+
def bar(x: scala.Any): scala.Unit = x match {
20+
case Bar(a) =>
21+
scala.Predef.println(a)
22+
case BarSeq(a) =>
23+
scala.Predef.println(a)
24+
case BarSeq(a, b) =>
25+
scala.Predef.println(a)
26+
}
27+
def baz(x: scala.Any): scala.Unit = x match {
28+
case Baz(a) =>
29+
scala.Predef.println(a)
30+
case BazSeq(a) =>
31+
scala.Predef.println(a)
32+
case BazSeq(a, b) =>
33+
scala.Predef.println(a)
34+
}
35+
}

tests/pos/simpleExractors.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Foo {
2+
def bar(x: Any): Unit = x match {
3+
case Bar(a) => println(a)
4+
case BarSeq(a) => println(a)
5+
case BarSeq(a, b) => println(a)
6+
}
7+
def baz(x: Any): Unit = x match {
8+
case Baz(a) => println(a)
9+
case BazSeq(a) => println(a)
10+
case BazSeq(a, b) => println(a)
11+
}
12+
}
13+
14+
object Bar {
15+
def unapply(arg: Any): Option[Any] = Some(arg)
16+
}
17+
18+
object BarSeq {
19+
def unapplySeq(arg: Any): Option[Seq[Any]] = Some(List(arg))
20+
}
21+
22+
object Baz {
23+
def unapply[T](arg: T): Option[T] = Some(arg)
24+
}
25+
26+
object BazSeq {
27+
def unapplySeq[T](arg: T): Option[Seq[T]] = Some(List(arg))
28+
}

tests/run/tasty-custom-show/quoted_1.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class DummyShow[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) {
4949
import tasty._
5050
def showTree(tree: Tree)(implicit ctx: Context): String = "Tree"
5151
def showCaseDef(caseDef: CaseDef)(implicit ctx: Context): String = "CaseDef"
52+
def showPattern(pattern: Pattern)(implicit ctx: Context): String = "Pattern"
5253
def showTypeOrBoundsTree(tpt: TypeOrBoundsTree)(implicit ctx: Context): String = "TypeOrBoundsTree"
5354
def showTypeOrBounds(tpe: TypeOrBounds)(implicit ctx: Context): String = "TypeOrBounds"
5455
def showConstant(const: Constant)(implicit ctx: Context): String = "Constant"

0 commit comments

Comments
 (0)