Skip to content

Resugar Varargs #5273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/test/dotc/pos-recompilation.whitelist
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ t116
t3869
t6225b
t704
varargs
varargs-position
35 changes: 31 additions & 4 deletions library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case Term.Typed(term, tpt) =>
tpt.tpe match {
case Types.Repeated(_) =>
printTree(term)
term match {
case Term.Repeated(_) =>
printTree(term)
case _ =>
printTree(term)
this += ": " += highlightTypeDef("_*", color)
}
case _ =>
inParens {
printTree(term)
Expand Down Expand Up @@ -790,9 +796,16 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty

case TypeTree.Annotated(tpt, annot) =>
val Annotation(ref, args) = annot
printTypeTree(tpt)
this += " "
printAnnotation(annot)
ref.tpe match {
case Types.RepeatedAnnotation() =>
val Types.Sequence(tp) = tpt.tpe
printType(tp)
this += highlightTypeDef("*", color)
case _ =>
printTypeTree(tpt)
this += " "
printAnnotation(annot)
}

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

object Sequence {
def unapply(tpe: Type)(implicit ctx: Context): Option[Type] = tpe match {
case Type.AppliedType(Type.TypeRef("Seq", Type.SymRef(sym, _)), IsType(tp) :: Nil) if sym.fullName == "scala.collection" => Some(tp)
case _ => None
}
}

object RepeatedAnnotation {
def unapply(tpe: Type)(implicit ctx: Context): Boolean = tpe match {
case Type.TypeRef("Repeated", Type.SymRef(sym, _)) if sym.fullName == "scala.annotation.internal" => true
case _ => false
}
}

object Repeated {
def unapply(tpe: Type)(implicit ctx: Context): Option[Type] = tpe match {
case Type.AppliedType(Type.TypeRef("<repeated>", ScalaPackage()), IsType(tp) :: Nil) => Some(tp)
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/varargs-position.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
object varargspos {
def g(a: scala.Int, x: scala.Int*): scala.Int = a.+(x.length)
varargspos.g(1, 2, 3, 4)
val xs: collection.immutable.List[scala.Int] = scala.Nil.::[scala.Int](2).::[scala.Int](1)
val a: scala.Int = 8
val b: scala.Int = 7
varargspos.g(5, varargspos.xs: _*)
varargspos.g(3, scala.Nil: _*)
varargspos.g(varargspos.a, varargspos.xs: _*)
varargspos.g(varargspos.a, varargspos.b, 2, 3)
varargspos.g(1)
}
12 changes: 12 additions & 0 deletions tests/pos/varargs-position.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
object varargspos {
def g(a: Int, x: Int*) = a + x.length
g(1, 2, 3, 4)
val xs = 1 :: 2 :: Nil
val a = 8
val b = 7
g(5, xs: _*)
g(3, Nil: _*)
g(a, xs: _*)
g(a, b, 2, 3)
g(1)
}