@@ -15,7 +15,7 @@ import dotty.tools.dotc.ast.tpd
15
15
*
16
16
* Transforms `scala.Array.apply([....])` and `scala.Array.apply(..., [....])` into `[...]`
17
17
*/
18
- class ArrayApply extends MiniPhase {
18
+ class ArrayApply extends MiniPhase :
19
19
import tpd ._
20
20
21
21
override def phaseName : String = ArrayApply .name
@@ -25,14 +25,18 @@ class ArrayApply extends MiniPhase {
25
25
override def transformApply (tree : tpd.Apply )(using Context ): tpd.Tree =
26
26
if isArrayModuleApply(tree.symbol) then
27
27
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) =>
30
29
seqLit
31
30
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 =>
34
35
tpd.JavaSeqLiteral (elem0 :: seqLit.elems, seqLit.elemtpt)
35
36
37
+ case elem0 :: InlinedSplice (inlined, seqLit) :: Nil =>
38
+ tpd.cpy.Inlined (inlined)(inlined.call, inlined.bindings, tpd.JavaSeqLiteral (elem0 :: seqLit.elems, seqLit.elemtpt))
39
+
36
40
case _ =>
37
41
tree
38
42
}
@@ -49,6 +53,7 @@ class ArrayApply extends MiniPhase {
49
53
* - `ClassTag.XYZ` for primitive types
50
54
*/
51
55
private def elideClassTag (ct : Tree )(using Context ): Boolean = ct match {
56
+ case Inlined (_, _, expansion) => elideClassTag(expansion)
52
57
case Apply (_, rc :: Nil ) if ct.symbol == defn.ClassTagModule_apply =>
53
58
rc match {
54
59
case _ : Literal => true // ClassTag.apply(classOf[XYZ])
@@ -63,13 +68,27 @@ class ArrayApply extends MiniPhase {
63
68
case _ => false
64
69
}
65
70
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
73
92
74
93
object ArrayApply :
75
94
val name : String = " arrayApply"
0 commit comments