Skip to content

Commit 19a069c

Browse files
committed
Fix type of modue objects
1 parent be7859e commit 19a069c

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
108108
}
109109
}
110110

111+
def keepDefinition(d: Definition): Boolean = {
112+
val flags = d.flags
113+
import flags._
114+
!isParam && !isParamAccessor // && !isSynthetic // TODO: should print synthetic methods?
115+
}
116+
111117
val stats1 = stats.collect {
112-
case stat@Definition() if !stat.flags.isParam && !stat.flags.isParamAccessor => stat
118+
case stat @ Definition() if keepDefinition(stat) => stat
113119
case stat@Import(_, _) => stat
114120
case stat@Term() => stat
115121
}
@@ -380,9 +386,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
380386
def printTypeTrees(typesTrees: List[TypeTree], sep: String): Buffer = {
381387
def printSeparated(list: List[TypeTree]): Unit = list match {
382388
case Nil =>
383-
case x :: Nil => printTypeTree(x)
389+
case x :: Nil => printTypeTreeTop(x)
384390
case x :: xs =>
385-
printTypeTree(x)
391+
printTypeTreeTop(x)
386392
this += sep
387393
printSeparated(xs)
388394
}
@@ -563,6 +569,12 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
563569
printTypeTree(tpt)
564570
}
565571

572+
def printTypeTreeTop(tree: TypeTree): Buffer = tree match {
573+
case TypeTree.Synthetic() => printTypeTop(tree.tpe)
574+
case TypeTree.TypeIdent(name) => printTypeTop(tree.tpe)
575+
case _ => printTypeTree(tree)
576+
}
577+
566578
def printTypeTree(tree: TypeTree): Buffer = tree match {
567579
case TypeTree.Synthetic() =>
568580
printType(tree.tpe)
@@ -627,13 +639,25 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
627639
case tpe@Type() => printType(tpe)
628640
}
629641

642+
def printTypeTop(tree: Type): Buffer = tree match {
643+
case Type.TypeRef(name, prefix) =>
644+
prefix match {
645+
case NoPrefix() | Type.ThisType(Types.EmptyPackage()) =>
646+
case prefix@Type() => printType(prefix) += "."
647+
}
648+
if (name.endsWith("$")) this += name.stripSuffix("$") += ".type"
649+
else this += name
650+
case _ => printType(tree)
651+
}
652+
630653
def printType(tpe: Type): Buffer = tpe match {
631654
case Type.ConstantType(const) =>
632655
printConstant(const)
633656

634657
case Type.SymRef(sym, prefix) =>
635658
prefix match {
636659
case Type.ThisType(Types.EmptyPackage() | Types.RootPackage()) =>
660+
case Type.ThisType(Type.SymRef(ClassDef(_, _, _, _, _), _)) =>
637661
case prefix@Type.SymRef(ClassDef(_, _, _, _, _), _) =>
638662
printType(prefix)
639663
this += "#"
@@ -660,9 +684,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
660684
case Type.TypeRef(name, prefix) =>
661685
prefix match {
662686
case NoPrefix() | Type.ThisType(Types.EmptyPackage()) =>
663-
case prefix@Type() =>
664-
printType(prefix)
665-
this += "."
687+
case prefix@Type() => printType(prefix) += "."
666688
}
667689
this += name.stripSuffix("$")
668690

tests/pos/i2104.decompiled

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
class Cons[H, T]() extends java.lang.Object
33
object Cons {
44
def apply[H, T](h: H, t: T): Cons[H, T] = scala.Predef.???
5-
def unapply[H, T](t: Cons[H, T]): scala.Option[<empty>.Pair[H, T]] = scala.Predef.???
5+
def unapply[H, T](t: Cons[H, T]): scala.Option[Pair[H, T]] = scala.Predef.???
66
}/** Decompiled from out/posTestFromTasty/pos/i2104/Pair.class */
77
case class Pair[A, B](_1: A, _2: B) {
88
def copy[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any](_1: A, _2: B): Pair[A, B] = new Pair[A, B](_1, _2)
9-
def copy$default$1[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any]: Pair.A = (Pair._1: Pair._1)
10-
def copy$default$2[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any]: Pair.B = (Pair._2: Pair._2)
9+
def copy$default$1[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any]: A = (_1: _1)
10+
def copy$default$2[A >: scala.Nothing <: scala.Any, B >: scala.Nothing <: scala.Any]: B = (_2: _2)
1111
override def hashCode(): scala.Int = {
1212
var acc: scala.Int = 2479866
13-
acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(Pair._1))
14-
acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(Pair._2))
13+
acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(_1))
14+
acc = scala.runtime.Statics.mix(acc, scala.runtime.Statics.anyHash(_2))
1515
scala.runtime.Statics.finalizeHash(acc, 2)
1616
}
1717
override def equals(x$0: scala.Any): scala.Boolean = this.eq(x$0.asInstanceOf[java.lang.Object]).||(x$0 match {
18-
case x$0: Pair[Pair.A, Pair.B] =>
18+
case x$0: Pair[A, B] =>
1919
this._1.==(x$0._1).&&(this._2.==(x$0._2))
2020
case _ =>
2121
false
2222
})
2323
override def toString(): java.lang.String = scala.runtime.ScalaRunTime._toString(this)
24-
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[Pair[Pair.A, Pair.B]]
24+
override def canEqual(that: scala.Any): scala.Boolean = that.isInstanceOf[Pair[A, B]]
2525
override def productArity: scala.Int = 2
2626
override def productPrefix: java.lang.String = "Pair"
2727
override def productElement(n: scala.Int): scala.Any = n match {
@@ -39,12 +39,12 @@ object Pair extends scala.AnyRef {
3939
}/** Decompiled from out/posTestFromTasty/pos/i2104/Test.class */
4040
object Test {
4141
def main(args: scala.Array[scala.Predef.String]): scala.Unit = {
42-
<empty>.Cons.apply[scala.Option[scala.Int], scala.None](scala.Option.apply[scala.Int](1), scala.None) match {
43-
case <empty>.Cons.unapply[scala.Option[scala.Int], scala.None](, scala.None) =>
42+
Cons.apply[scala.Option[scala.Int], scala.None.type](scala.Option.apply[scala.Int](1), scala.None) match {
43+
case Cons(_: scala.Some[scala.Int], scala.None) =>
4444
{
4545
(i: scala.Int)
4646
dotty.DottyPredef.assert(i.==(1))
4747
}
4848
}
4949
}
50-
}
50+
}

0 commit comments

Comments
 (0)