@@ -43,8 +43,23 @@ pub enum PassWhere {
43
43
AfterTerminator ( BasicBlock ) ,
44
44
}
45
45
46
- /// If the session is properly configured, dumps a human-readable
47
- /// representation of the mir into:
46
+ /// Cosmetic options for pretty-printing the MIR contents, gathered from the CLI. Each pass can
47
+ /// override these when dumping its own specific MIR information with [`dump_mir_with_options`].
48
+ #[ derive( Copy , Clone ) ]
49
+ pub struct PrettyPrintMirOptions {
50
+ /// Whether to include extra comments, like span info. From `-Z mir-include-spans`.
51
+ pub include_extra_comments : bool ,
52
+ }
53
+
54
+ impl PrettyPrintMirOptions {
55
+ /// Create the default set of MIR pretty-printing options from the CLI flags.
56
+ pub fn from_cli ( tcx : TyCtxt < ' _ > ) -> Self {
57
+ Self { include_extra_comments : tcx. sess . opts . unstable_opts . mir_include_spans }
58
+ }
59
+ }
60
+
61
+ /// If the session is properly configured, dumps a human-readable representation of the MIR (with
62
+ /// default pretty-printing options) into:
48
63
///
49
64
/// ```text
50
65
/// rustc.node<node_id>.<pass_num>.<pass_name>.<disambiguator>
@@ -77,12 +92,40 @@ pub fn dump_mir<'tcx, F>(
77
92
extra_data : F ,
78
93
) where
79
94
F : FnMut ( PassWhere , & mut dyn io:: Write ) -> io:: Result < ( ) > ,
95
+ {
96
+ dump_mir_with_options (
97
+ tcx,
98
+ pass_num,
99
+ pass_name,
100
+ disambiguator,
101
+ body,
102
+ extra_data,
103
+ PrettyPrintMirOptions :: from_cli ( tcx) ,
104
+ ) ;
105
+ }
106
+
107
+ /// If the session is properly configured, dumps a human-readable representation of the MIR, with
108
+ /// the given [pretty-printing options][PrettyPrintMirOptions].
109
+ ///
110
+ /// See [`dump_mir`] for more details.
111
+ ///
112
+ #[ inline]
113
+ pub fn dump_mir_with_options < ' tcx , F > (
114
+ tcx : TyCtxt < ' tcx > ,
115
+ pass_num : bool ,
116
+ pass_name : & str ,
117
+ disambiguator : & dyn Display ,
118
+ body : & Body < ' tcx > ,
119
+ extra_data : F ,
120
+ options : PrettyPrintMirOptions ,
121
+ ) where
122
+ F : FnMut ( PassWhere , & mut dyn io:: Write ) -> io:: Result < ( ) > ,
80
123
{
81
124
if !dump_enabled ( tcx, pass_name, body. source . def_id ( ) ) {
82
125
return ;
83
126
}
84
127
85
- dump_matched_mir_node ( tcx, pass_num, pass_name, disambiguator, body, extra_data) ;
128
+ dump_matched_mir_node ( tcx, pass_num, pass_name, disambiguator, body, extra_data, options ) ;
86
129
}
87
130
88
131
pub fn dump_enabled ( tcx : TyCtxt < ' _ > , pass_name : & str , def_id : DefId ) -> bool {
@@ -112,6 +155,7 @@ fn dump_matched_mir_node<'tcx, F>(
112
155
disambiguator : & dyn Display ,
113
156
body : & Body < ' tcx > ,
114
157
mut extra_data : F ,
158
+ options : PrettyPrintMirOptions ,
115
159
) where
116
160
F : FnMut ( PassWhere , & mut dyn io:: Write ) -> io:: Result < ( ) > ,
117
161
{
@@ -133,7 +177,7 @@ fn dump_matched_mir_node<'tcx, F>(
133
177
writeln ! ( file) ?;
134
178
extra_data ( PassWhere :: BeforeCFG , & mut file) ?;
135
179
write_user_type_annotations ( tcx, body, & mut file) ?;
136
- write_mir_fn ( tcx, body, & mut extra_data, & mut file) ?;
180
+ write_mir_fn ( tcx, body, & mut extra_data, & mut file, options ) ?;
137
181
extra_data ( PassWhere :: AfterCFG , & mut file) ?;
138
182
} ;
139
183
@@ -243,12 +287,15 @@ pub fn create_dump_file<'tcx>(
243
287
///////////////////////////////////////////////////////////////////////////
244
288
// Whole MIR bodies
245
289
246
- /// Write out a human-readable textual representation for the given MIR.
290
+ /// Write out a human-readable textual representation for the given MIR, with the default
291
+ /// [PrettyPrintMirOptions].
247
292
pub fn write_mir_pretty < ' tcx > (
248
293
tcx : TyCtxt < ' tcx > ,
249
294
single : Option < DefId > ,
250
295
w : & mut dyn io:: Write ,
251
296
) -> io:: Result < ( ) > {
297
+ let options = PrettyPrintMirOptions :: from_cli ( tcx) ;
298
+
252
299
writeln ! ( w, "// WARNING: This output format is intended for human consumers only" ) ?;
253
300
writeln ! ( w, "// and is subject to change without notice. Knock yourself out." ) ?;
254
301
@@ -262,11 +309,11 @@ pub fn write_mir_pretty<'tcx>(
262
309
}
263
310
264
311
let render_body = |w : & mut dyn io:: Write , body| -> io:: Result < ( ) > {
265
- write_mir_fn ( tcx, body, & mut |_, _| Ok ( ( ) ) , w) ?;
312
+ write_mir_fn ( tcx, body, & mut |_, _| Ok ( ( ) ) , w, options ) ?;
266
313
267
314
for body in tcx. promoted_mir ( def_id) {
268
315
writeln ! ( w) ?;
269
- write_mir_fn ( tcx, body, & mut |_, _| Ok ( ( ) ) , w) ?;
316
+ write_mir_fn ( tcx, body, & mut |_, _| Ok ( ( ) ) , w, options ) ?;
270
317
}
271
318
Ok ( ( ) )
272
319
} ;
@@ -278,7 +325,7 @@ pub fn write_mir_pretty<'tcx>(
278
325
writeln ! ( w, "// MIR FOR CTFE" ) ?;
279
326
// Do not use `render_body`, as that would render the promoteds again, but these
280
327
// are shared between mir_for_ctfe and optimized_mir
281
- write_mir_fn ( tcx, tcx. mir_for_ctfe ( def_id) , & mut |_, _| Ok ( ( ) ) , w) ?;
328
+ write_mir_fn ( tcx, tcx. mir_for_ctfe ( def_id) , & mut |_, _| Ok ( ( ) ) , w, options ) ?;
282
329
} else {
283
330
let instance_mir = tcx. instance_mir ( ty:: InstanceKind :: Item ( def_id) ) ;
284
331
render_body ( w, instance_mir) ?;
@@ -293,14 +340,15 @@ pub fn write_mir_fn<'tcx, F>(
293
340
body : & Body < ' tcx > ,
294
341
extra_data : & mut F ,
295
342
w : & mut dyn io:: Write ,
343
+ options : PrettyPrintMirOptions ,
296
344
) -> io:: Result < ( ) >
297
345
where
298
346
F : FnMut ( PassWhere , & mut dyn io:: Write ) -> io:: Result < ( ) > ,
299
347
{
300
- write_mir_intro ( tcx, body, w) ?;
348
+ write_mir_intro ( tcx, body, w, options ) ?;
301
349
for block in body. basic_blocks . indices ( ) {
302
350
extra_data ( PassWhere :: BeforeBlock ( block) , w) ?;
303
- write_basic_block ( tcx, block, body, extra_data, w) ?;
351
+ write_basic_block ( tcx, block, body, extra_data, w, options ) ?;
304
352
if block. index ( ) + 1 != body. basic_blocks . len ( ) {
305
353
writeln ! ( w) ?;
306
354
}
@@ -321,6 +369,7 @@ fn write_scope_tree(
321
369
w : & mut dyn io:: Write ,
322
370
parent : SourceScope ,
323
371
depth : usize ,
372
+ options : PrettyPrintMirOptions ,
324
373
) -> io:: Result < ( ) > {
325
374
let indent = depth * INDENT . len ( ) ;
326
375
@@ -333,7 +382,7 @@ fn write_scope_tree(
333
382
334
383
let indented_debug_info = format ! ( "{0:1$}debug {2:?};" , INDENT , indent, var_debug_info) ;
335
384
336
- if tcx . sess . opts . unstable_opts . mir_include_spans {
385
+ if options . include_extra_comments {
337
386
writeln ! (
338
387
w,
339
388
"{0:1$} // in {2}" ,
@@ -373,7 +422,7 @@ fn write_scope_tree(
373
422
374
423
let local_name = if local == RETURN_PLACE { " return place" } else { "" } ;
375
424
376
- if tcx . sess . opts . unstable_opts . mir_include_spans {
425
+ if options . include_extra_comments {
377
426
writeln ! (
378
427
w,
379
428
"{0:1$} //{2} in {3}" ,
@@ -410,7 +459,7 @@ fn write_scope_tree(
410
459
411
460
let indented_header = format ! ( "{0:1$}scope {2}{3} {{" , "" , indent, child. index( ) , special) ;
412
461
413
- if tcx . sess . opts . unstable_opts . mir_include_spans {
462
+ if options . include_extra_comments {
414
463
if let Some ( span) = span {
415
464
writeln ! (
416
465
w,
@@ -426,7 +475,7 @@ fn write_scope_tree(
426
475
writeln ! ( w, "{indented_header}" ) ?;
427
476
}
428
477
429
- write_scope_tree ( tcx, body, scope_tree, w, child, depth + 1 ) ?;
478
+ write_scope_tree ( tcx, body, scope_tree, w, child, depth + 1 , options ) ?;
430
479
writeln ! ( w, "{0:1$}}}" , "" , depth * INDENT . len( ) ) ?;
431
480
}
432
481
@@ -449,10 +498,11 @@ impl Debug for VarDebugInfo<'_> {
449
498
450
499
/// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
451
500
/// local variables (both user-defined bindings and compiler temporaries).
452
- pub fn write_mir_intro < ' tcx > (
501
+ fn write_mir_intro < ' tcx > (
453
502
tcx : TyCtxt < ' tcx > ,
454
503
body : & Body < ' _ > ,
455
504
w : & mut dyn io:: Write ,
505
+ options : PrettyPrintMirOptions ,
456
506
) -> io:: Result < ( ) > {
457
507
write_mir_sig ( tcx, body, w) ?;
458
508
writeln ! ( w, "{{" ) ?;
@@ -468,7 +518,7 @@ pub fn write_mir_intro<'tcx>(
468
518
}
469
519
}
470
520
471
- write_scope_tree ( tcx, body, & scope_tree, w, OUTERMOST_SOURCE_SCOPE , 1 ) ?;
521
+ write_scope_tree ( tcx, body, & scope_tree, w, OUTERMOST_SOURCE_SCOPE , 1 , options ) ?;
472
522
473
523
// Add an empty line before the first block is printed.
474
524
writeln ! ( w) ?;
@@ -651,12 +701,13 @@ pub fn dump_mir_def_ids(tcx: TyCtxt<'_>, single: Option<DefId>) -> Vec<DefId> {
651
701
// Basic blocks and their parts (statements, terminators, ...)
652
702
653
703
/// Write out a human-readable textual representation for the given basic block.
654
- pub fn write_basic_block < ' tcx , F > (
704
+ fn write_basic_block < ' tcx , F > (
655
705
tcx : TyCtxt < ' tcx > ,
656
706
block : BasicBlock ,
657
707
body : & Body < ' tcx > ,
658
708
extra_data : & mut F ,
659
709
w : & mut dyn io:: Write ,
710
+ options : PrettyPrintMirOptions ,
660
711
) -> io:: Result < ( ) >
661
712
where
662
713
F : FnMut ( PassWhere , & mut dyn io:: Write ) -> io:: Result < ( ) > ,
@@ -672,7 +723,7 @@ where
672
723
for statement in & data. statements {
673
724
extra_data ( PassWhere :: BeforeLocation ( current_location) , w) ?;
674
725
let indented_body = format ! ( "{INDENT}{INDENT}{statement:?};" ) ;
675
- if tcx . sess . opts . unstable_opts . mir_include_spans {
726
+ if options . include_extra_comments {
676
727
writeln ! (
677
728
w,
678
729
"{:A$} // {}{}" ,
@@ -689,9 +740,14 @@ where
689
740
writeln ! ( w, "{indented_body}" ) ?;
690
741
}
691
742
692
- write_extra ( tcx, w, |visitor| {
693
- visitor. visit_statement ( statement, current_location) ;
694
- } ) ?;
743
+ write_extra (
744
+ tcx,
745
+ w,
746
+ |visitor| {
747
+ visitor. visit_statement ( statement, current_location) ;
748
+ } ,
749
+ options,
750
+ ) ?;
695
751
696
752
extra_data ( PassWhere :: AfterLocation ( current_location) , w) ?;
697
753
@@ -701,7 +757,7 @@ where
701
757
// Terminator at the bottom.
702
758
extra_data ( PassWhere :: BeforeLocation ( current_location) , w) ?;
703
759
let indented_terminator = format ! ( "{0}{0}{1:?};" , INDENT , data. terminator( ) . kind) ;
704
- if tcx . sess . opts . unstable_opts . mir_include_spans {
760
+ if options . include_extra_comments {
705
761
writeln ! (
706
762
w,
707
763
"{:A$} // {}{}" ,
@@ -718,9 +774,14 @@ where
718
774
writeln ! ( w, "{indented_terminator}" ) ?;
719
775
}
720
776
721
- write_extra ( tcx, w, |visitor| {
722
- visitor. visit_terminator ( data. terminator ( ) , current_location) ;
723
- } ) ?;
777
+ write_extra (
778
+ tcx,
779
+ w,
780
+ |visitor| {
781
+ visitor. visit_terminator ( data. terminator ( ) , current_location) ;
782
+ } ,
783
+ options,
784
+ ) ?;
724
785
725
786
extra_data ( PassWhere :: AfterLocation ( current_location) , w) ?;
726
787
extra_data ( PassWhere :: AfterTerminator ( block) , w) ?;
@@ -1271,11 +1332,12 @@ fn write_extra<'tcx, F>(
1271
1332
tcx : TyCtxt < ' tcx > ,
1272
1333
write : & mut dyn io:: Write ,
1273
1334
mut visit_op : F ,
1335
+ options : PrettyPrintMirOptions ,
1274
1336
) -> io:: Result < ( ) >
1275
1337
where
1276
1338
F : FnMut ( & mut ExtraComments < ' tcx > ) ,
1277
1339
{
1278
- if tcx . sess . opts . unstable_opts . mir_include_spans {
1340
+ if options . include_extra_comments {
1279
1341
let mut extra_comments = ExtraComments { tcx, comments : vec ! [ ] } ;
1280
1342
visit_op ( & mut extra_comments) ;
1281
1343
for comment in extra_comments. comments {
@@ -1890,7 +1952,7 @@ pub(crate) fn pretty_print_const_value<'tcx>(
1890
1952
///////////////////////////////////////////////////////////////////////////
1891
1953
// Miscellaneous
1892
1954
1893
- /// Calc converted u64 decimal into hex and return it's length in chars
1955
+ /// Calc converted u64 decimal into hex and return its length in chars.
1894
1956
///
1895
1957
/// ```ignore (cannot-test-private-function)
1896
1958
/// assert_eq!(1, hex_number_length(0));
0 commit comments