@@ -1151,35 +1151,26 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
1151
1151
case _ => tree1 :: rest1
1152
1152
case nil => nil
1153
1153
recur(trees, 0 )
1154
- end extension
1155
-
1156
- /** A treemap that generates the same contexts as the original typer for statements.
1157
- * This means:
1158
- * - statements that are not definitions get the exprOwner as owner
1159
- * - imports are reflected in the contexts of subsequent statements
1160
- */
1161
- class TreeMapWithPreciseStatContexts (cpy : TreeCopier = tpd.cpy) extends TreeMap (cpy):
1162
1154
1163
- /** Transform statements, while maintaining import contexts and expression contexts
1155
+ /** Transform statements while maintaining import contexts and expression contexts
1164
1156
* in the same way as Typer does. The code addresses additional concerns:
1165
1157
* - be tail-recursive where possible
1166
1158
* - don't re-allocate trees where nothing has changed
1167
- * - avoid stack overflows for long statement lists
1168
1159
*/
1169
- override def transformStats ( stats : List [ Tree ], exprOwner : Symbol )(using Context ): List [Tree ] =
1160
+ inline def mapStatements ( exprOwner : Symbol , inline op : Tree => Context ?=> Tree )(using Context ): List [Tree ] =
1170
1161
@ tailrec
1171
1162
def loop (mapped : mutable.ListBuffer [Tree ] | Null , unchanged : List [Tree ], pending : List [Tree ])(using Context ): List [Tree ] =
1172
1163
pending match
1173
1164
case stat :: rest =>
1174
- def statCtx = stat match
1165
+ val statCtx = stat match
1175
1166
case _ : DefTree | _ : ImportOrExport => ctx
1176
1167
case _ => ctx.exprContext(stat, exprOwner)
1177
- def restCtx = stat match
1168
+ val stat1 = op(stat)(using statCtx)
1169
+ val restCtx = stat match
1178
1170
case stat : Import => ctx.importContext(stat, stat.symbol)
1179
1171
case _ => ctx
1180
- val stat1 = transform(stat)(using statCtx)
1181
1172
if stat1 eq stat then
1182
- loop(mapped, unchanged, rest)
1173
+ loop(mapped, unchanged, rest)( using restCtx)
1183
1174
else
1184
1175
val buf = if mapped == null then new mutable.ListBuffer [Tree ] else mapped
1185
1176
var xc = unchanged
@@ -1194,9 +1185,18 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
1194
1185
if mapped == null then unchanged
1195
1186
else mapped.prependToList(unchanged)
1196
1187
1197
- loop(null , stats, stats)
1198
- end transformStats
1199
- end TreeMapWithPreciseStatContexts
1188
+ loop(null , trees, trees)
1189
+ end mapStatements
1190
+ end extension
1191
+
1192
+ /** A treemap that generates the same contexts as the original typer for statements.
1193
+ * This means:
1194
+ * - statements that are not definitions get the exprOwner as owner
1195
+ * - imports are reflected in the contexts of subsequent statements
1196
+ */
1197
+ 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(_))
1200
1200
1201
1201
/** Map Inlined nodes, NamedArgs, Blocks with no statements and local references to underlying arguments.
1202
1202
* Also drops Inline and Block with no statements.
0 commit comments