@@ -1157,9 +1157,12 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11571157 * - be tail-recursive where possible
11581158 * - don't re-allocate trees where nothing has changed
11591159 */
1160- inline def mapStatements (exprOwner : Symbol , inline op : Tree => Context ?=> Tree )(using Context ): List [Tree ] =
1160+ inline def mapStatements [T ](
1161+ exprOwner : Symbol ,
1162+ inline op : Tree => Context ?=> Tree ,
1163+ inline wrapResult : List [Tree ] => Context ?=> T )(using Context ): T =
11611164 @ tailrec
1162- def loop (mapped : mutable.ListBuffer [Tree ] | Null , unchanged : List [Tree ], pending : List [Tree ])(using Context ): List [ Tree ] =
1165+ def loop (mapped : mutable.ListBuffer [Tree ] | Null , unchanged : List [Tree ], pending : List [Tree ])(using Context ): T =
11631166 pending match
11641167 case stat :: rest =>
11651168 val statCtx = stat match
@@ -1182,8 +1185,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11821185 case _ => buf += stat1
11831186 loop(buf, rest, rest)(using restCtx)
11841187 case nil =>
1185- if mapped == null then unchanged
1186- else mapped.prependToList(unchanged)
1188+ wrapResult(
1189+ if mapped == null then unchanged
1190+ else mapped.prependToList(unchanged))
11871191
11881192 loop(null , trees, trees)
11891193 end mapStatements
@@ -1195,8 +1199,15 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11951199 * - imports are reflected in the contexts of subsequent statements
11961200 */
11971201 class TreeMapWithPreciseStatContexts (cpy : TreeCopier = tpd.cpy) extends TreeMap (cpy):
1198- override def transformStats (trees : List [Tree ], exprOwner : Symbol )(using Context ): List [Tree ] =
1199- trees.mapStatements(exprOwner, transform(_))
1202+ def transformStats [T ](trees : List [Tree ], exprOwner : Symbol , wrapResult : List [Tree ] => Context ?=> T )(using Context ): T =
1203+ trees.mapStatements(exprOwner, transform(_), wrapResult)
1204+ final override def transformStats (trees : List [Tree ], exprOwner : Symbol )(using Context ): List [Tree ] =
1205+ transformStats(trees, exprOwner, sameStats)
1206+ override def transformBlock (blk : Block )(using Context ) =
1207+ transformStats(blk.stats, ctx.owner,
1208+ stats1 => ctx ?=> cpy.Block (blk)(stats1, transform(blk.expr)))
1209+
1210+ val sameStats : List [Tree ] => Context ?=> List [Tree ] = stats => stats
12001211
12011212 /** Map Inlined nodes, NamedArgs, Blocks with no statements and local references to underlying arguments.
12021213 * Also drops Inline and Block with no statements.
0 commit comments