Skip to content

Keep inlined nodes until the backend phase #18230

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases._
import dotty.tools.dotc.core.Decorators.em
import dotty.tools.dotc.report
import dotty.tools.dotc.inlines.Inlines

/*
*
Expand Down Expand Up @@ -479,6 +480,10 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
case t: TypeApply => // dotty specific
generatedType = genTypeApply(t)

case inlined @ Inlined(_, _, _) =>
genLoadTo(Inlines.dropInlined(inlined) , expectedType, dest)
generatedDest = dest

case _ => abort(s"Unexpected tree in genLoad: $tree/${tree.getClass} at: ${tree.span}")
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import dotty.tools.dotc.transform.sjs.JSSymUtils._

import JSEncoding._
import ScopedVar.withScopedVars
import dotty.tools.dotc.inlines.Inlines

/** Main codegen for Scala.js IR.
*
Expand Down Expand Up @@ -1930,6 +1931,9 @@ class JSCodeGen()(using genCtx: Context) {
case EmptyTree =>
js.Skip()

case inlined @ Inlined(_, _, _) =>
genStatOrExpr(Inlines.dropInlined(inlined), isStat)

case _ =>
throw new FatalError("Unexpected tree in genExpr: " +
tree + "/" + tree.getClass + " at: " + (tree.span: Position))
Expand Down
43 changes: 31 additions & 12 deletions compiler/src/dotty/tools/dotc/transform/ArrayApply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dotty.tools.dotc.ast.tpd
*
* Transforms `scala.Array.apply([....])` and `scala.Array.apply(..., [....])` into `[...]`
*/
class ArrayApply extends MiniPhase {
class ArrayApply extends MiniPhase:
import tpd._

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

case elem0 :: StripAscription(Apply(wrapRefArrayMeth, (seqLit: tpd.JavaSeqLiteral) :: Nil)) :: Nil
if defn.WrapArrayMethods().contains(wrapRefArrayMeth.symbol) =>
case InlinedSplice(inlined, seqLit) :: ct :: Nil if elideClassTag(ct) =>
tpd.cpy.Inlined(inlined)(inlined.call, inlined.bindings, seqLit)

case elem0 :: AppliedLiterals(seqLit) :: Nil =>
tpd.JavaSeqLiteral(elem0 :: seqLit.elems, seqLit.elemtpt)

case elem0 :: InlinedSplice(inlined, seqLit) :: Nil =>
tpd.cpy.Inlined(inlined)(inlined.call, inlined.bindings, tpd.JavaSeqLiteral(elem0 :: seqLit.elems, seqLit.elemtpt))

case _ =>
tree
}
Expand All @@ -49,6 +53,7 @@ class ArrayApply extends MiniPhase {
* - `ClassTag.XYZ` for primitive types
*/
private def elideClassTag(ct: Tree)(using Context): Boolean = ct match {
case Inlined(_, _, expansion) => elideClassTag(expansion)
case Apply(_, rc :: Nil) if ct.symbol == defn.ClassTagModule_apply =>
rc match {
case _: Literal => true // ClassTag.apply(classOf[XYZ])
Expand All @@ -63,13 +68,27 @@ class ArrayApply extends MiniPhase {
case _ => false
}

object StripAscription {
def unapply(tree: Tree)(using Context): Some[Tree] = tree match {
case Typed(expr, _) => unapply(expr)
case _ => Some(tree)
}
}
}
// Match a sequence of literal arguments passed to an Array constructor
private object AppliedLiterals:

def unapply(tree: Tree)(using Context): Option[tpd.JavaSeqLiteral] = tree match
case Apply(wrapRefArrayMeth, (seqLit: tpd.JavaSeqLiteral) :: Nil)
if defn.WrapArrayMethods().contains(wrapRefArrayMeth.symbol) =>
Some(seqLit)
case _ => None

end AppliedLiterals

// Match an inlined sequence splice
private object InlinedSplice:
def unapply(tree: Tree)(using Context): Option[(Inlined, tpd.JavaSeqLiteral)] = tree match
case inlined @ Inlined(_, _, Typed(AppliedLiterals(seqLit), _)) =>
Some((inlined, seqLit))
case _ => None

end InlinedSplice

end ArrayApply

object ArrayApply:
val name: String = "arrayApply"
Expand Down
5 changes: 0 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -895,11 +895,6 @@ object Erasure {
tree.typeOpt
else valueErasure(tree.typeOpt)

override def typedInlined(tree: untpd.Inlined, pt: Type)(using Context): Tree =
super.typedInlined(tree, pt) match {
case tree: Inlined => Inlines.dropInlined(tree)
}

override def typedValDef(vdef: untpd.ValDef, sym: Symbol)(using Context): Tree =
if (sym.isEffectivelyErased) erasedDef(sym)
else
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/Mixin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Names._
import NameKinds._
import NameOps._
import ast.Trees._
import dotty.tools.dotc.inlines.Inlines

object Mixin {
val name: String = "mixin"
Expand Down Expand Up @@ -221,6 +222,7 @@ class Mixin extends MiniPhase with SymTransformer { thisPhase =>
case _ =>
}
(scall, stats ::: inits, args)
case inlined @ Inlined(_, _, _) => transformConstructor(Inlines.dropInlined(inlined) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this one needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newClassExtendsClassParams was finding an unexpected inlined node while transforming the constructor

case _ =>
val Apply(sel @ Select(New(_), nme.CONSTRUCTOR), args) = tree: @unchecked
val (callArgs, initArgs) = if (tree.symbol.owner.is(Trait)) (Nil, args) else (args, Nil)
Expand Down