@@ -183,6 +183,25 @@ object Inliner:
183183
184184 end OpaqueProxy
185185
186+ /** A more powerful version of `constToLiteral` that also recurses through elidable (see [[isElideableExpr ]]) [[Block ]], [[Inlined ]], and [[Typed ]] nodes.
187+ */
188+ def inlinedConstToLiteral (rootTree : Tree )(using Context ): Tree =
189+ def rec (tree : Tree ): Tree =
190+ inline def recChild (subTree : Tree ): Tree =
191+ val res = rec(subTree)
192+ if res eq subTree then tree else res
193+
194+ tree match
195+ case Typed (expr, _) => recChild(expr)
196+ case Inlined (_, _, expr) => recChild(expr)
197+ case Block (_, expr) => recChild(expr)
198+ case _ => constToLiteral(tree)
199+
200+ if isElideableExpr(rootTree) then
201+ rec(rootTree)
202+ else
203+ constToLiteral(rootTree)
204+
186205 private [inlines] def newSym (name : Name , flags : FlagSet , info : Type , span : Span )(using Context ): Symbol =
187206 newSymbol(ctx.owner, name, flags, info, coord = span)
188207end Inliner
@@ -897,7 +916,7 @@ class Inliner(val call: tpd.Tree)(using Context):
897916 // if the projection leads to a typed tree then we stop reduction
898917 resNoReduce
899918 else
900- val res = constToLiteral (reducedProjection)
919+ val res = inlinedConstToLiteral (reducedProjection)
901920 if resNoReduce ne res then
902921 typed(res, pt) // redo typecheck if reduction changed something
903922 else if res.symbol.isInlineMethod then
@@ -928,19 +947,19 @@ class Inliner(val call: tpd.Tree)(using Context):
928947 override def typedValDef (vdef : untpd.ValDef , sym : Symbol )(using Context ): Tree =
929948 val vdef1 =
930949 if sym.is(Inline ) then
931- val rhs = typed(vdef.rhs)
950+ val rhs = inlinedConstToLiteral( typed(vdef.rhs) )
932951 sym.info = rhs.tpe
933952 untpd.cpy.ValDef (vdef)(vdef.name, untpd.TypeTree (rhs.tpe), untpd.TypedSplice (rhs))
934953 else vdef
935954 super .typedValDef(vdef1, sym)
936955
937956 override def typedApply (tree : untpd.Apply , pt : Type )(using Context ): Tree =
938957 val locked = ctx.typerState.ownedVars
939- specializeEq(inlineIfNeeded(constToLiteral (BetaReduce (super .typedApply(tree, pt))), pt, locked))
958+ specializeEq(inlineIfNeeded(inlinedConstToLiteral (BetaReduce (super .typedApply(tree, pt))), pt, locked))
940959
941960 override def typedTypeApply (tree : untpd.TypeApply , pt : Type )(using Context ): Tree =
942961 val locked = ctx.typerState.ownedVars
943- val tree1 = inlineIfNeeded(constToLiteral (BetaReduce (super .typedTypeApply(tree, pt))), pt, locked)
962+ val tree1 = inlineIfNeeded(inlinedConstToLiteral (BetaReduce (super .typedTypeApply(tree, pt))), pt, locked)
944963 if tree1.symbol == defn.QuotedTypeModule_of then
945964 ctx.compilationUnit.needsStaging = true
946965 tree1
@@ -1021,8 +1040,8 @@ class Inliner(val call: tpd.Tree)(using Context):
10211040 case _ => rhs0
10221041 }
10231042 val rhs2 = rhs1 match {
1024- case Typed (expr, tpt) if rhs1.span.isSynthetic => constToLiteral (expr)
1025- case _ => constToLiteral (rhs1)
1043+ case Typed (expr, tpt) if rhs1.span.isSynthetic => inlinedConstToLiteral (expr)
1044+ case _ => inlinedConstToLiteral (rhs1)
10261045 }
10271046 val (usedBindings, rhs3) = dropUnusedDefs(caseBindings, rhs2)
10281047 val rhs = seq(usedBindings, rhs3)
@@ -1056,7 +1075,7 @@ class Inliner(val call: tpd.Tree)(using Context):
10561075 val meth = tree.symbol
10571076 if meth.isAllOf(DeferredInline ) then
10581077 errorTree(tree, em " Deferred inline ${meth.showLocated} cannot be invoked " )
1059- else if Inlines .needsInlining(tree) then Inlines .inlineCall(simplify(tree, pt, locked))
1078+ else if Inlines .needsInlining(tree) then inlinedConstToLiteral( Inlines .inlineCall(simplify(tree, pt, locked) ))
10601079 else tree
10611080
10621081 override def typedUnadapted (tree : untpd.Tree , pt : Type , locked : TypeVars )(using Context ): Tree =
0 commit comments