Skip to content

Commit ae7af82

Browse files
Merge pull request #5273 from tuvior/decompiler-varargs
Resugar Varargs
2 parents e30f1ac + e94f0ef commit ae7af82

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

compiler/test/dotc/pos-recompilation.whitelist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ t116
3838
t3869
3939
t6225b
4040
t704
41+
varargs
42+
varargs-position

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
344344
case Term.Typed(term, tpt) =>
345345
tpt.tpe match {
346346
case Types.Repeated(_) =>
347-
printTree(term)
347+
term match {
348+
case Term.Repeated(_) =>
349+
printTree(term)
350+
case _ =>
351+
printTree(term)
352+
this += ": " += highlightTypeDef("_*", color)
353+
}
348354
case _ =>
349355
inParens {
350356
printTree(term)
@@ -790,9 +796,16 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
790796

791797
case TypeTree.Annotated(tpt, annot) =>
792798
val Annotation(ref, args) = annot
793-
printTypeTree(tpt)
794-
this += " "
795-
printAnnotation(annot)
799+
ref.tpe match {
800+
case Types.RepeatedAnnotation() =>
801+
val Types.Sequence(tp) = tpt.tpe
802+
printType(tp)
803+
this += highlightTypeDef("*", color)
804+
case _ =>
805+
printTypeTree(tpt)
806+
this += " "
807+
printAnnotation(annot)
808+
}
796809

797810
case TypeTree.And(left, right) =>
798811
printTypeTree(left)
@@ -1160,6 +1173,20 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
11601173
}
11611174
}
11621175

1176+
object Sequence {
1177+
def unapply(tpe: Type)(implicit ctx: Context): Option[Type] = tpe match {
1178+
case Type.AppliedType(Type.TypeRef("Seq", Type.SymRef(sym, _)), IsType(tp) :: Nil) if sym.fullName == "scala.collection" => Some(tp)
1179+
case _ => None
1180+
}
1181+
}
1182+
1183+
object RepeatedAnnotation {
1184+
def unapply(tpe: Type)(implicit ctx: Context): Boolean = tpe match {
1185+
case Type.TypeRef("Repeated", Type.SymRef(sym, _)) if sym.fullName == "scala.annotation.internal" => true
1186+
case _ => false
1187+
}
1188+
}
1189+
11631190
object Repeated {
11641191
def unapply(tpe: Type)(implicit ctx: Context): Option[Type] = tpe match {
11651192
case Type.AppliedType(Type.TypeRef("<repeated>", ScalaPackage()), IsType(tp) :: Nil) => Some(tp)

tests/pos/varargs-position.decompiled

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object varargspos {
2+
def g(a: scala.Int, x: scala.Int*): scala.Int = a.+(x.length)
3+
varargspos.g(1, 2, 3, 4)
4+
val xs: collection.immutable.List[scala.Int] = scala.Nil.::[scala.Int](2).::[scala.Int](1)
5+
val a: scala.Int = 8
6+
val b: scala.Int = 7
7+
varargspos.g(5, varargspos.xs: _*)
8+
varargspos.g(3, scala.Nil: _*)
9+
varargspos.g(varargspos.a, varargspos.xs: _*)
10+
varargspos.g(varargspos.a, varargspos.b, 2, 3)
11+
varargspos.g(1)
12+
}

tests/pos/varargs-position.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object varargspos {
2+
def g(a: Int, x: Int*) = a + x.length
3+
g(1, 2, 3, 4)
4+
val xs = 1 :: 2 :: Nil
5+
val a = 8
6+
val b = 7
7+
g(5, xs: _*)
8+
g(3, Nil: _*)
9+
g(a, xs: _*)
10+
g(a, b, 2, 3)
11+
g(1)
12+
}

0 commit comments

Comments
 (0)