@@ -43,8 +43,23 @@ pub enum PassWhere {
4343 AfterTerminator ( BasicBlock ) ,
4444}
4545
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:
4863///
4964/// ```text
5065/// rustc.node<node_id>.<pass_num>.<pass_name>.<disambiguator>
@@ -77,12 +92,40 @@ pub fn dump_mir<'tcx, F>(
7792 extra_data : F ,
7893) where
7994 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 < ( ) > ,
80123{
81124 if !dump_enabled ( tcx, pass_name, body. source . def_id ( ) ) {
82125 return ;
83126 }
84127
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 ) ;
86129}
87130
88131pub fn dump_enabled ( tcx : TyCtxt < ' _ > , pass_name : & str , def_id : DefId ) -> bool {
@@ -112,6 +155,7 @@ fn dump_matched_mir_node<'tcx, F>(
112155 disambiguator : & dyn Display ,
113156 body : & Body < ' tcx > ,
114157 mut extra_data : F ,
158+ options : PrettyPrintMirOptions ,
115159) where
116160 F : FnMut ( PassWhere , & mut dyn io:: Write ) -> io:: Result < ( ) > ,
117161{
@@ -133,7 +177,7 @@ fn dump_matched_mir_node<'tcx, F>(
133177 writeln ! ( file) ?;
134178 extra_data ( PassWhere :: BeforeCFG , & mut file) ?;
135179 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 ) ?;
137181 extra_data ( PassWhere :: AfterCFG , & mut file) ?;
138182 } ;
139183
@@ -243,12 +287,15 @@ pub fn create_dump_file<'tcx>(
243287///////////////////////////////////////////////////////////////////////////
244288// Whole MIR bodies
245289
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].
247292pub fn write_mir_pretty < ' tcx > (
248293 tcx : TyCtxt < ' tcx > ,
249294 single : Option < DefId > ,
250295 w : & mut dyn io:: Write ,
251296) -> io:: Result < ( ) > {
297+ let options = PrettyPrintMirOptions :: from_cli ( tcx) ;
298+
252299 writeln ! ( w, "// WARNING: This output format is intended for human consumers only" ) ?;
253300 writeln ! ( w, "// and is subject to change without notice. Knock yourself out." ) ?;
254301
@@ -262,11 +309,11 @@ pub fn write_mir_pretty<'tcx>(
262309 }
263310
264311 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 ) ?;
266313
267314 for body in tcx. promoted_mir ( def_id) {
268315 writeln ! ( w) ?;
269- write_mir_fn ( tcx, body, & mut |_, _| Ok ( ( ) ) , w) ?;
316+ write_mir_fn ( tcx, body, & mut |_, _| Ok ( ( ) ) , w, options ) ?;
270317 }
271318 Ok ( ( ) )
272319 } ;
@@ -278,7 +325,7 @@ pub fn write_mir_pretty<'tcx>(
278325 writeln ! ( w, "// MIR FOR CTFE" ) ?;
279326 // Do not use `render_body`, as that would render the promoteds again, but these
280327 // 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 ) ?;
282329 } else {
283330 let instance_mir = tcx. instance_mir ( ty:: InstanceKind :: Item ( def_id) ) ;
284331 render_body ( w, instance_mir) ?;
@@ -293,14 +340,15 @@ pub fn write_mir_fn<'tcx, F>(
293340 body : & Body < ' tcx > ,
294341 extra_data : & mut F ,
295342 w : & mut dyn io:: Write ,
343+ options : PrettyPrintMirOptions ,
296344) -> io:: Result < ( ) >
297345where
298346 F : FnMut ( PassWhere , & mut dyn io:: Write ) -> io:: Result < ( ) > ,
299347{
300- write_mir_intro ( tcx, body, w) ?;
348+ write_mir_intro ( tcx, body, w, options ) ?;
301349 for block in body. basic_blocks . indices ( ) {
302350 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 ) ?;
304352 if block. index ( ) + 1 != body. basic_blocks . len ( ) {
305353 writeln ! ( w) ?;
306354 }
@@ -321,6 +369,7 @@ fn write_scope_tree(
321369 w : & mut dyn io:: Write ,
322370 parent : SourceScope ,
323371 depth : usize ,
372+ options : PrettyPrintMirOptions ,
324373) -> io:: Result < ( ) > {
325374 let indent = depth * INDENT . len ( ) ;
326375
@@ -333,7 +382,7 @@ fn write_scope_tree(
333382
334383 let indented_debug_info = format ! ( "{0:1$}debug {2:?};" , INDENT , indent, var_debug_info) ;
335384
336- if tcx . sess . opts . unstable_opts . mir_include_spans {
385+ if options . include_extra_comments {
337386 writeln ! (
338387 w,
339388 "{0:1$} // in {2}" ,
@@ -373,7 +422,7 @@ fn write_scope_tree(
373422
374423 let local_name = if local == RETURN_PLACE { " return place" } else { "" } ;
375424
376- if tcx . sess . opts . unstable_opts . mir_include_spans {
425+ if options . include_extra_comments {
377426 writeln ! (
378427 w,
379428 "{0:1$} //{2} in {3}" ,
@@ -410,7 +459,7 @@ fn write_scope_tree(
410459
411460 let indented_header = format ! ( "{0:1$}scope {2}{3} {{" , "" , indent, child. index( ) , special) ;
412461
413- if tcx . sess . opts . unstable_opts . mir_include_spans {
462+ if options . include_extra_comments {
414463 if let Some ( span) = span {
415464 writeln ! (
416465 w,
@@ -426,7 +475,7 @@ fn write_scope_tree(
426475 writeln ! ( w, "{indented_header}" ) ?;
427476 }
428477
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 ) ?;
430479 writeln ! ( w, "{0:1$}}}" , "" , depth * INDENT . len( ) ) ?;
431480 }
432481
@@ -449,10 +498,11 @@ impl Debug for VarDebugInfo<'_> {
449498
450499/// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
451500/// local variables (both user-defined bindings and compiler temporaries).
452- pub fn write_mir_intro < ' tcx > (
501+ fn write_mir_intro < ' tcx > (
453502 tcx : TyCtxt < ' tcx > ,
454503 body : & Body < ' _ > ,
455504 w : & mut dyn io:: Write ,
505+ options : PrettyPrintMirOptions ,
456506) -> io:: Result < ( ) > {
457507 write_mir_sig ( tcx, body, w) ?;
458508 writeln ! ( w, "{{" ) ?;
@@ -468,7 +518,7 @@ pub fn write_mir_intro<'tcx>(
468518 }
469519 }
470520
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 ) ?;
472522
473523 // Add an empty line before the first block is printed.
474524 writeln ! ( w) ?;
@@ -651,12 +701,13 @@ pub fn dump_mir_def_ids(tcx: TyCtxt<'_>, single: Option<DefId>) -> Vec<DefId> {
651701// Basic blocks and their parts (statements, terminators, ...)
652702
653703/// 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 > (
655705 tcx : TyCtxt < ' tcx > ,
656706 block : BasicBlock ,
657707 body : & Body < ' tcx > ,
658708 extra_data : & mut F ,
659709 w : & mut dyn io:: Write ,
710+ options : PrettyPrintMirOptions ,
660711) -> io:: Result < ( ) >
661712where
662713 F : FnMut ( PassWhere , & mut dyn io:: Write ) -> io:: Result < ( ) > ,
@@ -672,7 +723,7 @@ where
672723 for statement in & data. statements {
673724 extra_data ( PassWhere :: BeforeLocation ( current_location) , w) ?;
674725 let indented_body = format ! ( "{INDENT}{INDENT}{statement:?};" ) ;
675- if tcx . sess . opts . unstable_opts . mir_include_spans {
726+ if options . include_extra_comments {
676727 writeln ! (
677728 w,
678729 "{:A$} // {}{}" ,
@@ -689,9 +740,14 @@ where
689740 writeln ! ( w, "{indented_body}" ) ?;
690741 }
691742
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+ ) ?;
695751
696752 extra_data ( PassWhere :: AfterLocation ( current_location) , w) ?;
697753
@@ -701,7 +757,7 @@ where
701757 // Terminator at the bottom.
702758 extra_data ( PassWhere :: BeforeLocation ( current_location) , w) ?;
703759 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 {
705761 writeln ! (
706762 w,
707763 "{:A$} // {}{}" ,
@@ -718,9 +774,14 @@ where
718774 writeln ! ( w, "{indented_terminator}" ) ?;
719775 }
720776
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+ ) ?;
724785
725786 extra_data ( PassWhere :: AfterLocation ( current_location) , w) ?;
726787 extra_data ( PassWhere :: AfterTerminator ( block) , w) ?;
@@ -1271,11 +1332,12 @@ fn write_extra<'tcx, F>(
12711332 tcx : TyCtxt < ' tcx > ,
12721333 write : & mut dyn io:: Write ,
12731334 mut visit_op : F ,
1335+ options : PrettyPrintMirOptions ,
12741336) -> io:: Result < ( ) >
12751337where
12761338 F : FnMut ( & mut ExtraComments < ' tcx > ) ,
12771339{
1278- if tcx . sess . opts . unstable_opts . mir_include_spans {
1340+ if options . include_extra_comments {
12791341 let mut extra_comments = ExtraComments { tcx, comments : vec ! [ ] } ;
12801342 visit_op ( & mut extra_comments) ;
12811343 for comment in extra_comments. comments {
@@ -1890,7 +1952,7 @@ pub(crate) fn pretty_print_const_value<'tcx>(
18901952///////////////////////////////////////////////////////////////////////////
18911953// Miscellaneous
18921954
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.
18941956///
18951957/// ```ignore (cannot-test-private-function)
18961958/// assert_eq!(1, hex_number_length(0));
0 commit comments