@@ -194,10 +194,15 @@ object Inliner {
194194
195195 /** Replace `Inlined` node by a block that contains its bindings and expansion */
196196 def dropInlined (inlined : Inlined )(implicit ctx : Context ): Tree =
197- if (enclosingInlineds.nonEmpty) inlined // Remove in the outer most inlined call
198- else reposition(inlined, inlined.call.span)
197+ val tree1 =
198+ if inlined.bindings.isEmpty then inlined.expansion
199+ else cpy.Block (inlined)(inlined.bindings, inlined.expansion)
200+ // Reposition in the outer most inlined call
201+ if (enclosingInlineds.nonEmpty) tree1 else reposition(tree1, inlined.span)
202+
203+ def reposition (tree : Tree , callSpan : Span )(implicit ctx : Context ): Tree = {
204+ // Reference test tests/run/i4947b
199205
200- def reposition (tree : Tree , span : Span )(implicit ctx : Context ): Tree = {
201206 val curSource = ctx.compilationUnit.source
202207
203208 // Tree copier that changes the source of all trees to `curSource`
@@ -211,39 +216,30 @@ object Inliner {
211216 /** Removes all Inlined trees, replacing them with blocks.
212217 * Repositions all trees directly inside an inlined expansion of a non empty call to the position of the call.
213218 * Any tree directly inside an empty call (inlined in the inlined code) retains their position.
219+ *
220+ * Until we implement JSR-45, we cannot represent in output positions in other source files.
221+ * So, reposition inlined code from other files with the call position.
214222 */
215223 class Reposition extends TreeMap (cpyWithNewSource) {
216- def finalize (tree : Tree , copied : untpd.Tree ) =
217- copied.withSpan(tree.span).withAttachmentsFrom(tree).withTypeUnchecked(tree.tpe)
218-
219- def reposition (tree : Tree )(implicit ctx : Context ): Tree = enclosingInlineds match {
220- case call :: _ if call.symbol.source != curSource =>
221- tree match {
222- case _ : EmptyTree [? ] | _ : EmptyValDef [? ] => tree
223- case _ =>
224- // Until we implement JSR-45, we cannot represent in output positions in other source files.
225- // So, reposition inlined code from other files with the call position:
226- tree.withSpan(span)
227- }
228- case _ => tree
229- }
230224
231225 override def transform (tree : Tree )(implicit ctx : Context ): Tree = {
232- val transformed = reposition(tree match {
233- case tree : Inlined =>
234- tpd.seq(transformSub(tree.bindings), transform(tree.expansion)(inlineContext(tree.call)))(ctx.withSource(curSource)) : Tree
235- case tree : Ident => finalize(tree, untpd.Ident (tree.name)(curSource))
236- case tree : Literal => finalize(tree, untpd.Literal (tree.const)(curSource))
237- case tree : This => finalize(tree, untpd.This (tree.qual)(curSource))
238- case tree : JavaSeqLiteral => finalize(tree, untpd.JavaSeqLiteral (transform(tree.elems), transform(tree.elemtpt))(curSource))
239- case tree : SeqLiteral => finalize(tree, untpd.SeqLiteral (transform(tree.elems), transform(tree.elemtpt))(curSource))
240- case tree : TypeTree => tpd.TypeTree (tree.tpe)(ctx.withSource(curSource)).withSpan(tree.span)
241- case tree : Bind => finalize(tree, untpd.Bind (tree.name, transform(tree.body))(curSource))
226+ def finalize (copied : untpd.Tree ) =
227+ val span = if tree.source == curSource then tree.span else callSpan
228+ copied.withSpan(span).withAttachmentsFrom(tree).withTypeUnchecked(tree.tpe)
229+
230+ given as Context = ctx.withSource(curSource)
231+
232+ tree match {
233+ case tree : Ident => finalize(untpd.Ident (tree.name)(curSource))
234+ case tree : Literal => finalize(untpd.Literal (tree.const)(curSource))
235+ case tree : This => finalize(untpd.This (tree.qual)(curSource))
236+ case tree : JavaSeqLiteral => finalize(untpd.JavaSeqLiteral (transform(tree.elems), transform(tree.elemtpt))(curSource))
237+ case tree : SeqLiteral => finalize(untpd.SeqLiteral (transform(tree.elems), transform(tree.elemtpt))(curSource))
238+ case tree : Bind => finalize(untpd.Bind (tree.name, transform(tree.body))(curSource))
239+ case tree : TypeTree => finalize(tpd.TypeTree (tree.tpe))
242240 case tree : DefTree => super .transform(tree).setDefTree
243241 case _ => super .transform(tree)
244- })
245- assert(transformed.isInstanceOf [EmptyTree [? ]] || transformed.isInstanceOf [EmptyValDef [? ]] || transformed.source == curSource)
246- transformed
242+ }
247243 }
248244 }
249245
0 commit comments