Skip to content

Commit 66ddf48

Browse files
committed
Keep inlined nodes until backend phase
Refactoring of inlined nodes to simplify the work on #17055. This work is based on #18229.
1 parent 40d44d3 commit 66ddf48

File tree

5 files changed

+42
-17
lines changed

5 files changed

+42
-17
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import dotty.tools.dotc.core.Contexts._
2525
import dotty.tools.dotc.core.Phases._
2626
import dotty.tools.dotc.core.Decorators.em
2727
import dotty.tools.dotc.report
28+
import dotty.tools.dotc.inlines.Inlines
2829

2930
/*
3031
*
@@ -479,6 +480,10 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
479480
case t: TypeApply => // dotty specific
480481
generatedType = genTypeApply(t)
481482

483+
case inlined @ Inlined(_, _, _) =>
484+
genLoadTo(Inlines.dropInlined(inlined) , expectedType, dest)
485+
generatedDest = dest
486+
482487
case _ => abort(s"Unexpected tree in genLoad: $tree/${tree.getClass} at: ${tree.span}")
483488
}
484489

compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import dotty.tools.dotc.transform.sjs.JSSymUtils._
3636

3737
import JSEncoding._
3838
import ScopedVar.withScopedVars
39+
import dotty.tools.dotc.inlines.Inlines
3940

4041
/** Main codegen for Scala.js IR.
4142
*
@@ -1930,6 +1931,9 @@ class JSCodeGen()(using genCtx: Context) {
19301931
case EmptyTree =>
19311932
js.Skip()
19321933

1934+
case inlined @ Inlined(_, _, _) =>
1935+
genStatOrExpr(Inlines.dropInlined(inlined), isStat)
1936+
19331937
case _ =>
19341938
throw new FatalError("Unexpected tree in genExpr: " +
19351939
tree + "/" + tree.getClass + " at: " + (tree.span: Position))

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(_, _, 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"

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,6 @@ object Erasure {
895895
tree.typeOpt
896896
else valueErasure(tree.typeOpt)
897897

898-
override def typedInlined(tree: untpd.Inlined, pt: Type)(using Context): Tree =
899-
super.typedInlined(tree, pt) match {
900-
case tree: Inlined => Inlines.dropInlined(tree)
901-
}
902-
903898
override def typedValDef(vdef: untpd.ValDef, sym: Symbol)(using Context): Tree =
904899
if (sym.isEffectivelyErased) erasedDef(sym)
905900
else

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Names._
1717
import NameKinds._
1818
import NameOps._
1919
import ast.Trees._
20+
import dotty.tools.dotc.inlines.Inlines
2021

2122
object Mixin {
2223
val name: String = "mixin"
@@ -221,6 +222,7 @@ class Mixin extends MiniPhase with SymTransformer { thisPhase =>
221222
case _ =>
222223
}
223224
(scall, stats ::: inits, args)
225+
case inlined @ Inlined(_, _, _) => transformConstructor(Inlines.dropInlined(inlined) )
224226
case _ =>
225227
val Apply(sel @ Select(New(_), nme.CONSTRUCTOR), args) = tree: @unchecked
226228
val (callArgs, initArgs) = if (tree.symbol.owner.is(Trait)) (Nil, args) else (args, Nil)

0 commit comments

Comments
 (0)