@@ -21,6 +21,8 @@ use syntax::ast::NodeId;
21
21
use syntax:: codemap:: Span ;
22
22
23
23
const INDENT : & ' static str = " " ;
24
+ /// Alignment for lining up comments following MIR statements
25
+ const ALIGN : usize = 50 ;
24
26
25
27
/// If the session is properly configured, dumps a human-readable
26
28
/// representation of the mir into:
@@ -79,11 +81,20 @@ pub fn write_mir_pretty<'a, 'b, 'tcx, I>(tcx: TyCtxt<'b, 'tcx, 'tcx>,
79
81
-> io:: Result < ( ) >
80
82
where I : Iterator < Item =( & ' a NodeId , & ' a Mir < ' tcx > ) > , ' tcx : ' a
81
83
{
84
+ let mut first = true ;
82
85
for ( & id, mir) in iter {
86
+ if first {
87
+ first = false ;
88
+ } else {
89
+ // Put empty lines between all items
90
+ writeln ! ( w, "" ) ?;
91
+ }
92
+
83
93
let src = MirSource :: from_node ( tcx, id) ;
84
94
write_mir_fn ( tcx, src, mir, w, None ) ?;
85
95
86
96
for ( i, mir) in mir. promoted . iter ( ) . enumerate ( ) {
97
+ writeln ! ( w, "" ) ?;
87
98
write_mir_fn ( tcx, MirSource :: Promoted ( id, i) , mir, w, None ) ?;
88
99
}
89
100
}
@@ -131,6 +142,8 @@ pub fn write_mir_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
131
142
. or_insert ( vec ! [ ] )
132
143
. push ( ScopeId :: new ( index) ) ;
133
144
}
145
+
146
+ writeln ! ( w, "{}scope tree:" , INDENT ) ?;
134
147
write_scope_tree ( tcx, mir, auxiliary, & scope_tree, w, None , 1 ) ?;
135
148
136
149
writeln ! ( w, "}}" ) ?;
@@ -147,7 +160,7 @@ fn write_basic_block(tcx: TyCtxt,
147
160
let data = mir. basic_block_data ( block) ;
148
161
149
162
// Basic block label at the top.
150
- writeln ! ( w, "\n {}{:?}: {{" , INDENT , block) ?;
163
+ writeln ! ( w, "{}{:?}: {{" , INDENT , block) ?;
151
164
152
165
// List of statements in the middle.
153
166
let mut current_location = Location { block : block, statement_index : 0 } ;
@@ -165,25 +178,27 @@ fn write_basic_block(tcx: TyCtxt,
165
178
}
166
179
}
167
180
168
- writeln ! ( w, "{0}{0}{1:?}; // {2}" ,
169
- INDENT ,
170
- statement,
181
+ let indented_mir = format ! ( "{0}{0}{1:?};" , INDENT , statement) ;
182
+ writeln ! ( w, "{0:1$} // {2}" ,
183
+ indented_mir,
184
+ ALIGN ,
171
185
comment( tcx, statement. scope, statement. span) ) ?;
172
186
173
187
current_location. statement_index += 1 ;
174
188
}
175
189
176
190
// Terminator at the bottom.
177
- writeln ! ( w, "{0}{0}{1:?}; // {2}" ,
178
- INDENT ,
179
- data. terminator( ) . kind,
191
+ let indented_terminator = format ! ( "{0}{0}{1:?};" , INDENT , data. terminator( ) . kind) ;
192
+ writeln ! ( w, "{0:1$} // {2}" ,
193
+ indented_terminator,
194
+ ALIGN ,
180
195
comment( tcx, data. terminator( ) . scope, data. terminator( ) . span) ) ?;
181
196
182
- writeln ! ( w, "{}}}" , INDENT )
197
+ writeln ! ( w, "{}}}\n " , INDENT )
183
198
}
184
199
185
200
fn comment ( tcx : TyCtxt , scope : ScopeId , span : Span ) -> String {
186
- format ! ( "Scope({}) at {}" , scope. index( ) , tcx. sess. codemap( ) . span_to_string( span) )
201
+ format ! ( "scope {} at {}" , scope. index( ) , tcx. sess. codemap( ) . span_to_string( span) )
187
202
}
188
203
189
204
fn write_scope_tree ( tcx : TyCtxt ,
@@ -198,22 +213,28 @@ fn write_scope_tree(tcx: TyCtxt,
198
213
let indent = depth * INDENT . len ( ) ;
199
214
let data = & mir. scopes [ child] ;
200
215
assert_eq ! ( data. parent_scope, parent) ;
201
- writeln ! ( w, "{0:1$}Scope( {2}) {{ " , "" , indent, child. index( ) ) ?;
216
+ write ! ( w, "{0:1$}{2}" , "" , indent, child. index( ) ) ?;
202
217
203
218
let indent = indent + INDENT . len ( ) ;
204
- if let Some ( parent) = parent {
205
- writeln ! ( w, "{0:1$}Parent: Scope({2})" , "" , indent, parent. index( ) ) ?;
206
- }
207
219
208
220
if let Some ( auxiliary) = auxiliary {
209
221
let extent = auxiliary[ child] . extent ;
210
222
let data = tcx. region_maps . code_extent_data ( extent) ;
211
223
writeln ! ( w, "{0:1$}Extent: {2:?}" , "" , indent, data) ?;
212
224
}
213
225
214
- write_scope_tree ( tcx, mir, auxiliary, scope_tree, w,
215
- Some ( child) , depth + 1 ) ?;
226
+ if scope_tree. get ( & Some ( child) ) . map ( Vec :: is_empty) . unwrap_or ( true ) {
227
+ // No child scopes, skip the braces
228
+ writeln ! ( w, "" ) ?;
229
+ } else {
230
+ writeln ! ( w, " {{" ) ?;
231
+ write_scope_tree ( tcx, mir, auxiliary, scope_tree, w,
232
+ Some ( child) , depth + 1 ) ?;
233
+
234
+ writeln ! ( w, "{0:1$}}}" , "" , indent - INDENT . len( ) ) ?;
235
+ }
216
236
}
237
+
217
238
Ok ( ( ) )
218
239
}
219
240
@@ -261,13 +282,20 @@ fn write_mir_intro<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
261
282
262
283
// User variable types (including the user's name in a comment).
263
284
for ( i, var) in mir. var_decls . iter ( ) . enumerate ( ) {
264
- write ! ( w, "{}let " , INDENT ) ?;
265
- if var. mutability == Mutability :: Mut {
266
- write ! ( w, "mut " ) ?;
267
- }
268
- writeln ! ( w, "{:?}: {}; // {} in {}" ,
269
- Lvalue :: Var ( i as u32 ) ,
270
- var. ty,
285
+ let mut_str = if var. mutability == Mutability :: Mut {
286
+ "mut "
287
+ } else {
288
+ ""
289
+ } ;
290
+
291
+ let indented_var = format ! ( "{}let {}{:?}: {};" ,
292
+ INDENT ,
293
+ mut_str,
294
+ Lvalue :: Var ( i as u32 ) ,
295
+ var. ty) ;
296
+ writeln ! ( w, "{0:1$} // \" {2}\" in {3}" ,
297
+ indented_var,
298
+ ALIGN ,
271
299
var. name,
272
300
comment( tcx, var. scope, var. span) ) ?;
273
301
}
@@ -277,5 +305,10 @@ fn write_mir_intro<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
277
305
writeln ! ( w, "{}let mut {:?}: {};" , INDENT , Lvalue :: Temp ( i as u32 ) , temp. ty) ?;
278
306
}
279
307
308
+ // Wrote any declaration? Add an empty line before the first block is printed.
309
+ if !mir. var_decls . is_empty ( ) || !mir. temp_decls . is_empty ( ) {
310
+ writeln ! ( w, "" ) ?;
311
+ }
312
+
280
313
Ok ( ( ) )
281
314
}
0 commit comments