Skip to content

Commit bd4254b

Browse files
committed
Fix type of modue objects
1 parent 2d57399 commit bd4254b

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
@@ -105,8 +105,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
105105
}
106106
}
107107

108+
def keepDefinition(d: Definition): Boolean = {
109+
val flags = d.flags
110+
import flags._
111+
!isParam && !isParamAccessor // && !isSynthetic // TODO: should print synthetic methods?
112+
}
113+
108114
val stats1 = stats.collect {
109-
case stat@Definition() if !stat.flags.isParam && !stat.flags.isParamAccessor => stat
115+
case stat @ Definition() if keepDefinition(stat) => stat
110116
case stat@Import(_, _) => stat
111117
case stat@Term() => stat
112118
}
@@ -377,9 +383,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
377383
def printTypeTrees(typesTrees: List[TypeTree], sep: String): Buffer = {
378384
def printSeparated(list: List[TypeTree]): Unit = list match {
379385
case Nil =>
380-
case x :: Nil => printTypeTree(x)
386+
case x :: Nil => printTypeTreeTop(x)
381387
case x :: xs =>
382-
printTypeTree(x)
388+
printTypeTreeTop(x)
383389
this += sep
384390
printSeparated(xs)
385391
}
@@ -551,6 +557,12 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
551557
printTypeTree(tpt)
552558
}
553559

560+
def printTypeTreeTop(tree: TypeTree): Buffer = tree match {
561+
case TypeTree.Synthetic() => printTypeTop(tree.tpe)
562+
case TypeTree.TypeIdent(name) => printTypeTop(tree.tpe)
563+
case _ => printTypeTree(tree)
564+
}
565+
554566
def printTypeTree(tree: TypeTree): Buffer = tree match {
555567
case TypeTree.Synthetic() =>
556568
printType(tree.tpe)
@@ -615,13 +627,25 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
615627
case tpe@Type() => printType(tpe)
616628
}
617629

630+
def printTypeTop(tree: Type): Buffer = tree match {
631+
case Type.TypeRef(name, prefix) =>
632+
prefix match {
633+
case NoPrefix() | Type.ThisType(Types.EmptyPackage()) =>
634+
case prefix@Type() => printType(prefix) += "."
635+
}
636+
if (name.endsWith("$")) this += name.stripSuffix("$") += ".type"
637+
else this += name
638+
case _ => printType(tree)
639+
}
640+
618641
def printType(tpe: Type): Buffer = tpe match {
619642
case Type.ConstantType(const) =>
620643
printConstant(const)
621644

622645
case Type.SymRef(sym, prefix) =>
623646
prefix match {
624647
case Type.ThisType(Types.EmptyPackage() | Types.RootPackage()) =>
648+
case Type.ThisType(Type.SymRef(ClassDef(_, _, _, _, _), _)) =>
625649
case prefix@Type.SymRef(ClassDef(_, _, _, _, _), _) =>
626650
printType(prefix)
627651
this += "#"
@@ -648,9 +672,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
648672
case Type.TypeRef(name, prefix) =>
649673
prefix match {
650674
case NoPrefix() | Type.ThisType(Types.EmptyPackage()) =>
651-
case prefix@Type() =>
652-
printType(prefix)
653-
this += "."
675+
case prefix@Type() => printType(prefix) += "."
654676
}
655677
this += name.stripSuffix("$")
656678

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)