@@ -144,7 +144,8 @@ pub fn write_mir_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
144
144
}
145
145
146
146
writeln ! ( w, "{}scope tree:" , INDENT ) ?;
147
- write_scope_tree ( tcx, mir, auxiliary, & scope_tree, w, None , 1 ) ?;
147
+ write_scope_tree ( tcx, mir, auxiliary, & scope_tree, w, None , 1 , false ) ?;
148
+ writeln ! ( w, "" ) ?;
148
149
149
150
writeln ! ( w, "}}" ) ?;
150
151
Ok ( ( ) )
@@ -207,10 +208,27 @@ fn write_scope_tree(tcx: TyCtxt,
207
208
scope_tree : & FnvHashMap < Option < ScopeId > , Vec < ScopeId > > ,
208
209
w : & mut Write ,
209
210
parent : Option < ScopeId > ,
210
- depth : usize )
211
+ depth : usize ,
212
+ same_line : bool )
211
213
-> io:: Result < ( ) > {
212
- for & child in scope_tree. get ( & parent) . unwrap_or ( & vec ! [ ] ) {
213
- let indent = depth * INDENT . len ( ) ;
214
+ let indent = if same_line {
215
+ 0
216
+ } else {
217
+ depth * INDENT . len ( )
218
+ } ;
219
+
220
+ let children = match scope_tree. get ( & parent) {
221
+ Some ( childs) => childs,
222
+ None => return Ok ( ( ) ) ,
223
+ } ;
224
+
225
+ for ( index, & child) in children. iter ( ) . enumerate ( ) {
226
+ if index == 0 && same_line {
227
+ // We know we're going to output a scope, so prefix it with a space to separate it from
228
+ // the previous scopes on this line
229
+ write ! ( w, " " ) ?;
230
+ }
231
+
214
232
let data = & mir. scopes [ child] ;
215
233
assert_eq ! ( data. parent_scope, parent) ;
216
234
write ! ( w, "{0:1$}{2}" , "" , indent, child. index( ) ) ?;
@@ -223,15 +241,22 @@ fn write_scope_tree(tcx: TyCtxt,
223
241
writeln ! ( w, "{0:1$}Extent: {2:?}" , "" , indent, data) ?;
224
242
}
225
243
226
- if scope_tree. get ( & Some ( child) ) . map ( Vec :: is_empty) . unwrap_or ( true ) {
227
- // No child scopes, skip the braces
228
- writeln ! ( w, "" ) ?;
244
+ let child_count = scope_tree. get ( & Some ( child) ) . map ( Vec :: len) . unwrap_or ( 0 ) ;
245
+ if child_count < 2 {
246
+ // Skip the braces when there's no or only a single subscope
247
+ write_scope_tree ( tcx, mir, auxiliary, scope_tree, w,
248
+ Some ( child) , depth, true ) ?;
229
249
} else {
250
+ // 2 or more child scopes? Put them in braces and on new lines.
230
251
writeln ! ( w, " {{" ) ?;
231
252
write_scope_tree ( tcx, mir, auxiliary, scope_tree, w,
232
- Some ( child) , depth + 1 ) ?;
253
+ Some ( child) , depth + 1 , false ) ?;
233
254
234
- writeln ! ( w, "{0:1$}}}" , "" , indent - INDENT . len( ) ) ?;
255
+ write ! ( w, "\n {0:1$}}}" , "" , depth * INDENT . len( ) ) ?;
256
+ }
257
+
258
+ if !same_line && index + 1 < children. len ( ) {
259
+ writeln ! ( w, "" ) ?;
235
260
}
236
261
}
237
262
0 commit comments