Skip to content

Commit

Permalink
Convert lifted non-SeqLiteral vararg splices into Seqs
Browse files Browse the repository at this point in the history
closes lampepfl#18233
  • Loading branch information
KacperFKorban committed Aug 18, 2023
1 parent 046ba60 commit 73036c5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/ElimRepeated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
private def arrayToSeq(tree: Tree)(using Context): Tree =
wrapArray(tree, tree.tpe.elemType)

/**
* Wrap vararg splices that have been lifted to valdefs in a call to `arrayToSeq`.
* `SeqLiteral`s are not wrapped, since they are eliminated in their own phase see `SeqLiterals`.
*/
override def transformValDef(tree: ValDef)(using Context): Tree =
if tree.tpt.typeOpt.isRepeatedParam then
tree.rhs match {
case Typed(expr, tpt)
if tpt.typeOpt.isRepeatedParam && expr.typeOpt.derivesFrom(defn.ArrayClass) && !expr.isInstanceOf[SeqLiteral] =>
cpy.ValDef(tree)(rhs = Typed(arrayToSeq(expr), tpt))
case _ =>
tree
}
else
tree

/** Generate the method definitions for the varargs forwarders created in transform */
override def transformDefDef(tree: DefDef)(using Context): Tree =
// If transform reported an error, don't go further
Expand Down
15 changes: 15 additions & 0 deletions tests/run/i18233-min.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// scalac: -coverage-out:coverage

def aList =
List(Array[String]()*)

def arr =
Array("abc", "def")

def anotherList =
List(arr*)

object Test extends App {
println(aList)
println(anotherList)
}
11 changes: 11 additions & 0 deletions tests/run/i18233.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// scalac: -coverage-out:coverage

enum Foo:
case Bar, Baz

object Foo:
def render = List(values.tail*).mkString

object Test extends App {
println(Foo.render)
}

0 comments on commit 73036c5

Please sign in to comment.