Skip to content

Commit b867bc5

Browse files
committed
Fix ArrayApply tests
1 parent b39ebe2 commit b867bc5

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

compiler/src/dotty/tools/dotc/transform/ArrayApply.scala

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import dotty.tools.dotc.ast.tpd
1515
*
1616
* Transforms `scala.Array.apply([....])` and `scala.Array.apply(..., [....])` into `[...]`
1717
*/
18-
class ArrayApply extends MiniPhase {
18+
class ArrayApply extends MiniPhase:
1919
import tpd._
2020

2121
override def phaseName: String = ArrayApply.name
@@ -25,14 +25,18 @@ class ArrayApply extends MiniPhase {
2525
override def transformApply(tree: tpd.Apply)(using Context): tpd.Tree =
2626
if isArrayModuleApply(tree.symbol) then
2727
tree.args match {
28-
case StripAscription(Apply(wrapRefArrayMeth, (seqLit: tpd.JavaSeqLiteral) :: Nil)) :: ct :: Nil
29-
if defn.WrapArrayMethods().contains(wrapRefArrayMeth.symbol) && elideClassTag(ct) =>
28+
case AppliedLiterals(seqLit) :: ct :: Nil if elideClassTag(ct) =>
3029
seqLit
3130

32-
case elem0 :: StripAscription(Apply(wrapRefArrayMeth, (seqLit: tpd.JavaSeqLiteral) :: Nil)) :: Nil
33-
if defn.WrapArrayMethods().contains(wrapRefArrayMeth.symbol) =>
31+
case InlinedSplice(inlined, seqLit) :: ct :: Nil if elideClassTag(ct) =>
32+
tpd.cpy.Inlined(inlined)(inlined.call, inlined.bindings, seqLit)
33+
34+
case elem0 :: AppliedLiterals(seqLit) :: Nil =>
3435
tpd.JavaSeqLiteral(elem0 :: seqLit.elems, seqLit.elemtpt)
3536

37+
case elem0 :: InlinedSplice(inlined, seqLit) :: Nil =>
38+
tpd.cpy.Inlined(inlined)(inlined.call, inlined.bindings, tpd.JavaSeqLiteral(elem0 :: seqLit.elems, seqLit.elemtpt))
39+
3640
case _ =>
3741
tree
3842
}
@@ -49,6 +53,7 @@ class ArrayApply extends MiniPhase {
4953
* - `ClassTag.XYZ` for primitive types
5054
*/
5155
private def elideClassTag(ct: Tree)(using Context): Boolean = ct match {
56+
case Inlined(_, _, expansion) => elideClassTag(expansion)
5257
case Apply(_, rc :: Nil) if ct.symbol == defn.ClassTagModule_apply =>
5358
rc match {
5459
case _: Literal => true // ClassTag.apply(classOf[XYZ])
@@ -63,13 +68,27 @@ class ArrayApply extends MiniPhase {
6368
case _ => false
6469
}
6570

66-
object StripAscription {
67-
def unapply(tree: Tree)(using Context): Some[Tree] = tree match {
68-
case Typed(expr, _) => unapply(expr)
69-
case _ => Some(tree)
70-
}
71-
}
72-
}
71+
// Match a sequence of literal arguments passed to an Array constructor
72+
private object AppliedLiterals:
73+
74+
def unapply(tree: Tree)(using Context): Option[tpd.JavaSeqLiteral] = tree match
75+
case Apply(wrapRefArrayMeth, (seqLit: tpd.JavaSeqLiteral) :: Nil)
76+
if defn.WrapArrayMethods().contains(wrapRefArrayMeth.symbol) =>
77+
Some(seqLit)
78+
case _ => None
79+
80+
end AppliedLiterals
81+
82+
// Match an inlined sequence splice
83+
private object InlinedSplice:
84+
def unapply(tree: Tree)(using Context): Option[(Inlined, tpd.JavaSeqLiteral)] = tree match
85+
case inlined @ Inlined(call, bindings, Typed(AppliedLiterals(seqLit), _)) =>
86+
Some((inlined, seqLit))
87+
case _ => None
88+
89+
end InlinedSplice
90+
91+
end ArrayApply
7392

7493
object ArrayApply:
7594
val name: String = "arrayApply"

0 commit comments

Comments
 (0)