rustc_middle/mir/
pretty.rs

1use std::collections::BTreeSet;
2use std::fmt::{Display, Write as _};
3use std::path::{Path, PathBuf};
4use std::{fs, io};
5
6use rustc_abi::Size;
7use rustc_ast::InlineAsmTemplatePiece;
8use tracing::trace;
9use ty::print::PrettyPrinter;
10
11use super::graphviz::write_mir_fn_graphviz;
12use crate::mir::interpret::{
13    AllocBytes, AllocId, Allocation, ConstAllocation, GlobalAlloc, Pointer, Provenance,
14    alloc_range, read_target_uint,
15};
16use crate::mir::visit::Visitor;
17use crate::mir::*;
18
19const INDENT: &str = "    ";
20/// Alignment for lining up comments following MIR statements
21pub(crate) const ALIGN: usize = 40;
22
23/// An indication of where we are in the control flow graph. Used for printing
24/// extra information in `dump_mir`
25#[derive(Clone, Copy)]
26pub enum PassWhere {
27    /// We have not started dumping the control flow graph, but we are about to.
28    BeforeCFG,
29
30    /// We just finished dumping the control flow graph. This is right before EOF
31    AfterCFG,
32
33    /// We are about to start dumping the given basic block.
34    BeforeBlock(BasicBlock),
35
36    /// We are just about to dump the given statement or terminator.
37    BeforeLocation(Location),
38
39    /// We just dumped the given statement or terminator.
40    AfterLocation(Location),
41
42    /// We just dumped the terminator for a block but not the closing `}`.
43    AfterTerminator(BasicBlock),
44}
45
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)]
49pub struct PrettyPrintMirOptions {
50    /// Whether to include extra comments, like span info. From `-Z mir-include-spans`.
51    pub include_extra_comments: bool,
52}
53
54impl 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.is_enabled() }
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:
63///
64/// ```text
65/// rustc.node<node_id>.<pass_num>.<pass_name>.<disambiguator>
66/// ```
67///
68/// Output from this function is controlled by passing `-Z dump-mir=<filter>`,
69/// where `<filter>` takes the following forms:
70///
71/// - `all` -- dump MIR for all fns, all passes, all everything
72/// - a filter defined by a set of substrings combined with `&` and `|`
73///   (`&` has higher precedence). At least one of the `|`-separated groups
74///   must match; an `|`-separated group matches if all of its `&`-separated
75///   substrings are matched.
76///
77/// Example:
78///
79/// - `nll` == match if `nll` appears in the name
80/// - `foo & nll` == match if `foo` and `nll` both appear in the name
81/// - `foo & nll | typeck` == match if `foo` and `nll` both appear in the name
82///   or `typeck` appears in the name.
83/// - `foo & nll | bar & typeck` == match if `foo` and `nll` both appear in the name
84///   or `typeck` and `bar` both appear in the name.
85#[inline]
86pub fn dump_mir<'tcx, F>(
87    tcx: TyCtxt<'tcx>,
88    pass_num: bool,
89    pass_name: &str,
90    disambiguator: &dyn Display,
91    body: &Body<'tcx>,
92    extra_data: F,
93) where
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]
113pub 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<()>,
123{
124    if !dump_enabled(tcx, pass_name, body.source.def_id()) {
125        return;
126    }
127
128    dump_matched_mir_node(tcx, pass_num, pass_name, disambiguator, body, extra_data, options);
129}
130
131pub fn dump_enabled(tcx: TyCtxt<'_>, pass_name: &str, def_id: DefId) -> bool {
132    let Some(ref filters) = tcx.sess.opts.unstable_opts.dump_mir else {
133        return false;
134    };
135    // see notes on #41697 below
136    let node_path = ty::print::with_forced_impl_filename_line!(tcx.def_path_str(def_id));
137    filters.split('|').any(|or_filter| {
138        or_filter.split('&').all(|and_filter| {
139            let and_filter_trimmed = and_filter.trim();
140            and_filter_trimmed == "all"
141                || pass_name.contains(and_filter_trimmed)
142                || node_path.contains(and_filter_trimmed)
143        })
144    })
145}
146
147// #41697 -- we use `with_forced_impl_filename_line()` because
148// `def_path_str()` would otherwise trigger `type_of`, and this can
149// run while we are already attempting to evaluate `type_of`.
150
151/// Most use-cases of dumping MIR should use the [dump_mir] entrypoint instead, which will also
152/// check if dumping MIR is enabled, and if this body matches the filters passed on the CLI.
153///
154/// That being said, if the above requirements have been validated already, this function is where
155/// most of the MIR dumping occurs, if one needs to export it to a file they have created with
156/// [create_dump_file], rather than to a new file created as part of [dump_mir], or to stdout/stderr
157/// for debugging purposes.
158pub fn dump_mir_to_writer<'tcx, F>(
159    tcx: TyCtxt<'tcx>,
160    pass_name: &str,
161    disambiguator: &dyn Display,
162    body: &Body<'tcx>,
163    w: &mut dyn io::Write,
164    mut extra_data: F,
165    options: PrettyPrintMirOptions,
166) -> io::Result<()>
167where
168    F: FnMut(PassWhere, &mut dyn io::Write) -> io::Result<()>,
169{
170    // see notes on #41697 above
171    let def_path =
172        ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
173    // ignore-tidy-odd-backticks the literal below is fine
174    write!(w, "// MIR for `{def_path}")?;
175    match body.source.promoted {
176        None => write!(w, "`")?,
177        Some(promoted) => write!(w, "::{promoted:?}`")?,
178    }
179    writeln!(w, " {disambiguator} {pass_name}")?;
180    if let Some(ref layout) = body.coroutine_layout_raw() {
181        writeln!(w, "/* coroutine_layout = {layout:#?} */")?;
182    }
183    writeln!(w)?;
184    extra_data(PassWhere::BeforeCFG, w)?;
185    write_user_type_annotations(tcx, body, w)?;
186    write_mir_fn(tcx, body, &mut extra_data, w, options)?;
187    extra_data(PassWhere::AfterCFG, w)
188}
189
190fn dump_matched_mir_node<'tcx, F>(
191    tcx: TyCtxt<'tcx>,
192    pass_num: bool,
193    pass_name: &str,
194    disambiguator: &dyn Display,
195    body: &Body<'tcx>,
196    extra_data: F,
197    options: PrettyPrintMirOptions,
198) where
199    F: FnMut(PassWhere, &mut dyn io::Write) -> io::Result<()>,
200{
201    let _: io::Result<()> = try {
202        let mut file = create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, body)?;
203        dump_mir_to_writer(tcx, pass_name, disambiguator, body, &mut file, extra_data, options)?;
204    };
205
206    if tcx.sess.opts.unstable_opts.dump_mir_graphviz {
207        let _: io::Result<()> = try {
208            let mut file = create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, body)?;
209            write_mir_fn_graphviz(tcx, body, false, &mut file)?;
210        };
211    }
212}
213
214/// Returns the path to the filename where we should dump a given MIR.
215/// Also used by other bits of code (e.g., NLL inference) that dump
216/// graphviz data or other things.
217fn dump_path<'tcx>(
218    tcx: TyCtxt<'tcx>,
219    extension: &str,
220    pass_num: bool,
221    pass_name: &str,
222    disambiguator: &dyn Display,
223    body: &Body<'tcx>,
224) -> PathBuf {
225    let source = body.source;
226    let promotion_id = match source.promoted {
227        Some(id) => format!("-{id:?}"),
228        None => String::new(),
229    };
230
231    let pass_num = if tcx.sess.opts.unstable_opts.dump_mir_exclude_pass_number {
232        String::new()
233    } else if pass_num {
234        let (dialect_index, phase_index) = body.phase.index();
235        format!(".{}-{}-{:03}", dialect_index, phase_index, body.pass_count)
236    } else {
237        ".-------".to_string()
238    };
239
240    let crate_name = tcx.crate_name(source.def_id().krate);
241    let item_name = tcx.def_path(source.def_id()).to_filename_friendly_no_crate();
242    // All drop shims have the same DefId, so we have to add the type
243    // to get unique file names.
244    let shim_disambiguator = match source.instance {
245        ty::InstanceKind::DropGlue(_, Some(ty)) => {
246            // Unfortunately, pretty-printed typed are not very filename-friendly.
247            // We dome some filtering.
248            let mut s = ".".to_owned();
249            s.extend(ty.to_string().chars().filter_map(|c| match c {
250                ' ' => None,
251                ':' | '<' | '>' => Some('_'),
252                c => Some(c),
253            }));
254            s
255        }
256        ty::InstanceKind::AsyncDropGlueCtorShim(_, ty) => {
257            let mut s = ".".to_owned();
258            s.extend(ty.to_string().chars().filter_map(|c| match c {
259                ' ' => None,
260                ':' | '<' | '>' => Some('_'),
261                c => Some(c),
262            }));
263            s
264        }
265        ty::InstanceKind::AsyncDropGlue(_, ty) => {
266            let ty::Coroutine(_, args) = ty.kind() else {
267                bug!();
268            };
269            let ty = args.first().unwrap().expect_ty();
270            let mut s = ".".to_owned();
271            s.extend(ty.to_string().chars().filter_map(|c| match c {
272                ' ' => None,
273                ':' | '<' | '>' => Some('_'),
274                c => Some(c),
275            }));
276            s
277        }
278        ty::InstanceKind::FutureDropPollShim(_, proxy_cor, impl_cor) => {
279            let mut s = ".".to_owned();
280            s.extend(proxy_cor.to_string().chars().filter_map(|c| match c {
281                ' ' => None,
282                ':' | '<' | '>' => Some('_'),
283                c => Some(c),
284            }));
285            s.push('.');
286            s.extend(impl_cor.to_string().chars().filter_map(|c| match c {
287                ' ' => None,
288                ':' | '<' | '>' => Some('_'),
289                c => Some(c),
290            }));
291            s
292        }
293        _ => String::new(),
294    };
295
296    let mut file_path = PathBuf::new();
297    file_path.push(Path::new(&tcx.sess.opts.unstable_opts.dump_mir_dir));
298
299    let file_name = format!(
300        "{crate_name}.{item_name}{shim_disambiguator}{promotion_id}{pass_num}.{pass_name}.{disambiguator}.{extension}",
301    );
302
303    file_path.push(&file_name);
304
305    file_path
306}
307
308/// Attempts to open a file where we should dump a given MIR or other
309/// bit of MIR-related data. Used by `mir-dump`, but also by other
310/// bits of code (e.g., NLL inference) that dump graphviz data or
311/// other things, and hence takes the extension as an argument.
312pub fn create_dump_file<'tcx>(
313    tcx: TyCtxt<'tcx>,
314    extension: &str,
315    pass_num: bool,
316    pass_name: &str,
317    disambiguator: &dyn Display,
318    body: &Body<'tcx>,
319) -> io::Result<io::BufWriter<fs::File>> {
320    let file_path = dump_path(tcx, extension, pass_num, pass_name, disambiguator, body);
321    if let Some(parent) = file_path.parent() {
322        fs::create_dir_all(parent).map_err(|e| {
323            io::Error::new(
324                e.kind(),
325                format!("IO error creating MIR dump directory: {parent:?}; {e}"),
326            )
327        })?;
328    }
329    fs::File::create_buffered(&file_path).map_err(|e| {
330        io::Error::new(e.kind(), format!("IO error creating MIR dump file: {file_path:?}; {e}"))
331    })
332}
333
334///////////////////////////////////////////////////////////////////////////
335// Whole MIR bodies
336
337/// Write out a human-readable textual representation for the given MIR, with the default
338/// [PrettyPrintMirOptions].
339pub fn write_mir_pretty<'tcx>(
340    tcx: TyCtxt<'tcx>,
341    single: Option<DefId>,
342    w: &mut dyn io::Write,
343) -> io::Result<()> {
344    let options = PrettyPrintMirOptions::from_cli(tcx);
345
346    writeln!(w, "// WARNING: This output format is intended for human consumers only")?;
347    writeln!(w, "// and is subject to change without notice. Knock yourself out.")?;
348    writeln!(w, "// HINT: See also -Z dump-mir for MIR at specific points during compilation.")?;
349
350    let mut first = true;
351    for def_id in dump_mir_def_ids(tcx, single) {
352        if first {
353            first = false;
354        } else {
355            // Put empty lines between all items
356            writeln!(w)?;
357        }
358
359        let render_body = |w: &mut dyn io::Write, body| -> io::Result<()> {
360            write_mir_fn(tcx, body, &mut |_, _| Ok(()), w, options)?;
361
362            for body in tcx.promoted_mir(def_id) {
363                writeln!(w)?;
364                write_mir_fn(tcx, body, &mut |_, _| Ok(()), w, options)?;
365            }
366            Ok(())
367        };
368
369        // For `const fn` we want to render both the optimized MIR and the MIR for ctfe.
370        if tcx.is_const_fn(def_id) {
371            render_body(w, tcx.optimized_mir(def_id))?;
372            writeln!(w)?;
373            writeln!(w, "// MIR FOR CTFE")?;
374            // Do not use `render_body`, as that would render the promoteds again, but these
375            // are shared between mir_for_ctfe and optimized_mir
376            write_mir_fn(tcx, tcx.mir_for_ctfe(def_id), &mut |_, _| Ok(()), w, options)?;
377        } else {
378            let instance_mir = tcx.instance_mir(ty::InstanceKind::Item(def_id));
379            render_body(w, instance_mir)?;
380        }
381    }
382    Ok(())
383}
384
385/// Write out a human-readable textual representation for the given function.
386pub fn write_mir_fn<'tcx, F>(
387    tcx: TyCtxt<'tcx>,
388    body: &Body<'tcx>,
389    extra_data: &mut F,
390    w: &mut dyn io::Write,
391    options: PrettyPrintMirOptions,
392) -> io::Result<()>
393where
394    F: FnMut(PassWhere, &mut dyn io::Write) -> io::Result<()>,
395{
396    write_mir_intro(tcx, body, w, options)?;
397    for block in body.basic_blocks.indices() {
398        extra_data(PassWhere::BeforeBlock(block), w)?;
399        write_basic_block(tcx, block, body, extra_data, w, options)?;
400        if block.index() + 1 != body.basic_blocks.len() {
401            writeln!(w)?;
402        }
403    }
404
405    writeln!(w, "}}")?;
406
407    write_allocations(tcx, body, w)?;
408
409    Ok(())
410}
411
412/// Prints local variables in a scope tree.
413fn write_scope_tree(
414    tcx: TyCtxt<'_>,
415    body: &Body<'_>,
416    scope_tree: &FxHashMap<SourceScope, Vec<SourceScope>>,
417    w: &mut dyn io::Write,
418    parent: SourceScope,
419    depth: usize,
420    options: PrettyPrintMirOptions,
421) -> io::Result<()> {
422    let indent = depth * INDENT.len();
423
424    // Local variable debuginfo.
425    for var_debug_info in &body.var_debug_info {
426        if var_debug_info.source_info.scope != parent {
427            // Not declared in this scope.
428            continue;
429        }
430
431        let indented_debug_info = format!("{0:1$}debug {2:?};", INDENT, indent, var_debug_info);
432
433        if options.include_extra_comments {
434            writeln!(
435                w,
436                "{0:1$} // in {2}",
437                indented_debug_info,
438                ALIGN,
439                comment(tcx, var_debug_info.source_info),
440            )?;
441        } else {
442            writeln!(w, "{indented_debug_info}")?;
443        }
444    }
445
446    // Local variable types.
447    for (local, local_decl) in body.local_decls.iter_enumerated() {
448        if (1..body.arg_count + 1).contains(&local.index()) {
449            // Skip over argument locals, they're printed in the signature.
450            continue;
451        }
452
453        if local_decl.source_info.scope != parent {
454            // Not declared in this scope.
455            continue;
456        }
457
458        let mut_str = local_decl.mutability.prefix_str();
459
460        let mut indented_decl = ty::print::with_no_trimmed_paths!(format!(
461            "{0:1$}let {2}{3:?}: {4}",
462            INDENT, indent, mut_str, local, local_decl.ty
463        ));
464        if let Some(user_ty) = &local_decl.user_ty {
465            for user_ty in user_ty.projections() {
466                write!(indented_decl, " as {user_ty:?}").unwrap();
467            }
468        }
469        indented_decl.push(';');
470
471        let local_name = if local == RETURN_PLACE { " return place" } else { "" };
472
473        if options.include_extra_comments {
474            writeln!(
475                w,
476                "{0:1$} //{2} in {3}",
477                indented_decl,
478                ALIGN,
479                local_name,
480                comment(tcx, local_decl.source_info),
481            )?;
482        } else {
483            writeln!(w, "{indented_decl}",)?;
484        }
485    }
486
487    let Some(children) = scope_tree.get(&parent) else {
488        return Ok(());
489    };
490
491    for &child in children {
492        let child_data = &body.source_scopes[child];
493        assert_eq!(child_data.parent_scope, Some(parent));
494
495        let (special, span) = if let Some((callee, callsite_span)) = child_data.inlined {
496            (
497                format!(
498                    " (inlined {}{})",
499                    if callee.def.requires_caller_location(tcx) { "#[track_caller] " } else { "" },
500                    callee
501                ),
502                Some(callsite_span),
503            )
504        } else {
505            (String::new(), None)
506        };
507
508        let indented_header = format!("{0:1$}scope {2}{3} {{", "", indent, child.index(), special);
509
510        if options.include_extra_comments {
511            if let Some(span) = span {
512                writeln!(
513                    w,
514                    "{0:1$} // at {2}",
515                    indented_header,
516                    ALIGN,
517                    tcx.sess.source_map().span_to_embeddable_string(span),
518                )?;
519            } else {
520                writeln!(w, "{indented_header}")?;
521            }
522        } else {
523            writeln!(w, "{indented_header}")?;
524        }
525
526        write_scope_tree(tcx, body, scope_tree, w, child, depth + 1, options)?;
527        writeln!(w, "{0:1$}}}", "", depth * INDENT.len())?;
528    }
529
530    Ok(())
531}
532
533impl Debug for VarDebugInfo<'_> {
534    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
535        if let Some(box VarDebugInfoFragment { ty, ref projection }) = self.composite {
536            pre_fmt_projection(&projection[..], fmt)?;
537            write!(fmt, "({}: {})", self.name, ty)?;
538            post_fmt_projection(&projection[..], fmt)?;
539        } else {
540            write!(fmt, "{}", self.name)?;
541        }
542
543        write!(fmt, " => {:?}", self.value)
544    }
545}
546
547/// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
548/// local variables (both user-defined bindings and compiler temporaries).
549fn write_mir_intro<'tcx>(
550    tcx: TyCtxt<'tcx>,
551    body: &Body<'_>,
552    w: &mut dyn io::Write,
553    options: PrettyPrintMirOptions,
554) -> io::Result<()> {
555    write_mir_sig(tcx, body, w)?;
556    writeln!(w, "{{")?;
557
558    // construct a scope tree and write it out
559    let mut scope_tree: FxHashMap<SourceScope, Vec<SourceScope>> = Default::default();
560    for (index, scope_data) in body.source_scopes.iter_enumerated() {
561        if let Some(parent) = scope_data.parent_scope {
562            scope_tree.entry(parent).or_default().push(index);
563        } else {
564            // Only the argument scope has no parent, because it's the root.
565            assert_eq!(index, OUTERMOST_SOURCE_SCOPE);
566        }
567    }
568
569    write_scope_tree(tcx, body, &scope_tree, w, OUTERMOST_SOURCE_SCOPE, 1, options)?;
570
571    // Add an empty line before the first block is printed.
572    writeln!(w)?;
573
574    if let Some(coverage_info_hi) = &body.coverage_info_hi {
575        write_coverage_info_hi(coverage_info_hi, w)?;
576    }
577    if let Some(function_coverage_info) = &body.function_coverage_info {
578        write_function_coverage_info(function_coverage_info, w)?;
579    }
580
581    Ok(())
582}
583
584fn write_coverage_info_hi(
585    coverage_info_hi: &coverage::CoverageInfoHi,
586    w: &mut dyn io::Write,
587) -> io::Result<()> {
588    let coverage::CoverageInfoHi { num_block_markers: _, branch_spans } = coverage_info_hi;
589
590    // Only add an extra trailing newline if we printed at least one thing.
591    let mut did_print = false;
592
593    for coverage::BranchSpan { span, true_marker, false_marker } in branch_spans {
594        writeln!(
595            w,
596            "{INDENT}coverage branch {{ true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
597        )?;
598        did_print = true;
599    }
600
601    if did_print {
602        writeln!(w)?;
603    }
604
605    Ok(())
606}
607
608fn write_function_coverage_info(
609    function_coverage_info: &coverage::FunctionCoverageInfo,
610    w: &mut dyn io::Write,
611) -> io::Result<()> {
612    let coverage::FunctionCoverageInfo { mappings, .. } = function_coverage_info;
613
614    for coverage::Mapping { kind, span } in mappings {
615        writeln!(w, "{INDENT}coverage {kind:?} => {span:?};")?;
616    }
617    writeln!(w)?;
618
619    Ok(())
620}
621
622fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn io::Write) -> io::Result<()> {
623    use rustc_hir::def::DefKind;
624
625    trace!("write_mir_sig: {:?}", body.source.instance);
626    let def_id = body.source.def_id();
627    let kind = tcx.def_kind(def_id);
628    let is_function = match kind {
629        DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..) | DefKind::SyntheticCoroutineBody => {
630            true
631        }
632        _ => tcx.is_closure_like(def_id),
633    };
634    match (kind, body.source.promoted) {
635        (_, Some(_)) => write!(w, "const ")?, // promoteds are the closest to consts
636        (DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?,
637        (DefKind::Static { safety: _, mutability: hir::Mutability::Not, nested: false }, _) => {
638            write!(w, "static ")?
639        }
640        (DefKind::Static { safety: _, mutability: hir::Mutability::Mut, nested: false }, _) => {
641            write!(w, "static mut ")?
642        }
643        (_, _) if is_function => write!(w, "fn ")?,
644        // things like anon const, not an item
645        (DefKind::AnonConst | DefKind::InlineConst, _) => {}
646        // `global_asm!` have fake bodies, which we may dump after mir-build
647        (DefKind::GlobalAsm, _) => {}
648        _ => bug!("Unexpected def kind {:?}", kind),
649    }
650
651    ty::print::with_forced_impl_filename_line! {
652        // see notes on #41697 elsewhere
653        write!(w, "{}", tcx.def_path_str(def_id))?
654    }
655    if let Some(p) = body.source.promoted {
656        write!(w, "::{p:?}")?;
657    }
658
659    if body.source.promoted.is_none() && is_function {
660        write!(w, "(")?;
661
662        // fn argument types.
663        for (i, arg) in body.args_iter().enumerate() {
664            if i != 0 {
665                write!(w, ", ")?;
666            }
667            write!(w, "{:?}: {}", Place::from(arg), body.local_decls[arg].ty)?;
668        }
669
670        write!(w, ") -> {}", body.return_ty())?;
671    } else {
672        assert_eq!(body.arg_count, 0);
673        write!(w, ": {} =", body.return_ty())?;
674    }
675
676    if let Some(yield_ty) = body.yield_ty() {
677        writeln!(w)?;
678        writeln!(w, "yields {yield_ty}")?;
679    }
680
681    write!(w, " ")?;
682    // Next thing that gets printed is the opening {
683
684    Ok(())
685}
686
687fn write_user_type_annotations(
688    tcx: TyCtxt<'_>,
689    body: &Body<'_>,
690    w: &mut dyn io::Write,
691) -> io::Result<()> {
692    if !body.user_type_annotations.is_empty() {
693        writeln!(w, "| User Type Annotations")?;
694    }
695    for (index, annotation) in body.user_type_annotations.iter_enumerated() {
696        writeln!(
697            w,
698            "| {:?}: user_ty: {}, span: {}, inferred_ty: {}",
699            index.index(),
700            annotation.user_ty,
701            tcx.sess.source_map().span_to_embeddable_string(annotation.span),
702            with_no_trimmed_paths!(format!("{}", annotation.inferred_ty)),
703        )?;
704    }
705    if !body.user_type_annotations.is_empty() {
706        writeln!(w, "|")?;
707    }
708    Ok(())
709}
710
711pub fn dump_mir_def_ids(tcx: TyCtxt<'_>, single: Option<DefId>) -> Vec<DefId> {
712    if let Some(i) = single {
713        vec![i]
714    } else {
715        tcx.mir_keys(()).iter().map(|def_id| def_id.to_def_id()).collect()
716    }
717}
718
719///////////////////////////////////////////////////////////////////////////
720// Basic blocks and their parts (statements, terminators, ...)
721
722/// Write out a human-readable textual representation for the given basic block.
723fn write_basic_block<'tcx, F>(
724    tcx: TyCtxt<'tcx>,
725    block: BasicBlock,
726    body: &Body<'tcx>,
727    extra_data: &mut F,
728    w: &mut dyn io::Write,
729    options: PrettyPrintMirOptions,
730) -> io::Result<()>
731where
732    F: FnMut(PassWhere, &mut dyn io::Write) -> io::Result<()>,
733{
734    let data = &body[block];
735
736    // Basic block label at the top.
737    let cleanup_text = if data.is_cleanup { " (cleanup)" } else { "" };
738    writeln!(w, "{INDENT}{block:?}{cleanup_text}: {{")?;
739
740    // List of statements in the middle.
741    let mut current_location = Location { block, statement_index: 0 };
742    for statement in &data.statements {
743        extra_data(PassWhere::BeforeLocation(current_location), w)?;
744        let indented_body = format!("{INDENT}{INDENT}{statement:?};");
745        if options.include_extra_comments {
746            writeln!(
747                w,
748                "{:A$} // {}{}",
749                indented_body,
750                if tcx.sess.verbose_internals() {
751                    format!("{current_location:?}: ")
752                } else {
753                    String::new()
754                },
755                comment(tcx, statement.source_info),
756                A = ALIGN,
757            )?;
758        } else {
759            writeln!(w, "{indented_body}")?;
760        }
761
762        write_extra(
763            tcx,
764            w,
765            |visitor| {
766                visitor.visit_statement(statement, current_location);
767            },
768            options,
769        )?;
770
771        extra_data(PassWhere::AfterLocation(current_location), w)?;
772
773        current_location.statement_index += 1;
774    }
775
776    // Terminator at the bottom.
777    extra_data(PassWhere::BeforeLocation(current_location), w)?;
778    if data.terminator.is_some() {
779        let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
780        if options.include_extra_comments {
781            writeln!(
782                w,
783                "{:A$} // {}{}",
784                indented_terminator,
785                if tcx.sess.verbose_internals() {
786                    format!("{current_location:?}: ")
787                } else {
788                    String::new()
789                },
790                comment(tcx, data.terminator().source_info),
791                A = ALIGN,
792            )?;
793        } else {
794            writeln!(w, "{indented_terminator}")?;
795        }
796
797        write_extra(
798            tcx,
799            w,
800            |visitor| {
801                visitor.visit_terminator(data.terminator(), current_location);
802            },
803            options,
804        )?;
805    }
806
807    extra_data(PassWhere::AfterLocation(current_location), w)?;
808    extra_data(PassWhere::AfterTerminator(block), w)?;
809
810    writeln!(w, "{INDENT}}}")
811}
812
813impl Debug for Statement<'_> {
814    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
815        use self::StatementKind::*;
816        match self.kind {
817            Assign(box (ref place, ref rv)) => write!(fmt, "{place:?} = {rv:?}"),
818            FakeRead(box (ref cause, ref place)) => {
819                write!(fmt, "FakeRead({cause:?}, {place:?})")
820            }
821            Retag(ref kind, ref place) => write!(
822                fmt,
823                "Retag({}{:?})",
824                match kind {
825                    RetagKind::FnEntry => "[fn entry] ",
826                    RetagKind::TwoPhase => "[2phase] ",
827                    RetagKind::Raw => "[raw] ",
828                    RetagKind::Default => "",
829                },
830                place,
831            ),
832            StorageLive(ref place) => write!(fmt, "StorageLive({place:?})"),
833            StorageDead(ref place) => write!(fmt, "StorageDead({place:?})"),
834            SetDiscriminant { ref place, variant_index } => {
835                write!(fmt, "discriminant({place:?}) = {variant_index:?}")
836            }
837            Deinit(ref place) => write!(fmt, "Deinit({place:?})"),
838            PlaceMention(ref place) => {
839                write!(fmt, "PlaceMention({place:?})")
840            }
841            AscribeUserType(box (ref place, ref c_ty), ref variance) => {
842                write!(fmt, "AscribeUserType({place:?}, {variance:?}, {c_ty:?})")
843            }
844            Coverage(ref kind) => write!(fmt, "Coverage::{kind:?}"),
845            Intrinsic(box ref intrinsic) => write!(fmt, "{intrinsic}"),
846            ConstEvalCounter => write!(fmt, "ConstEvalCounter"),
847            Nop => write!(fmt, "nop"),
848            BackwardIncompatibleDropHint { ref place, reason: _ } => {
849                // For now, we don't record the reason because there is only one use case,
850                // which is to report breaking change in drop order by Edition 2024
851                write!(fmt, "BackwardIncompatibleDropHint({place:?})")
852            }
853        }
854    }
855}
856
857impl Display for NonDivergingIntrinsic<'_> {
858    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
859        match self {
860            Self::Assume(op) => write!(f, "assume({op:?})"),
861            Self::CopyNonOverlapping(CopyNonOverlapping { src, dst, count }) => {
862                write!(f, "copy_nonoverlapping(dst = {dst:?}, src = {src:?}, count = {count:?})")
863            }
864        }
865    }
866}
867
868impl<'tcx> Debug for TerminatorKind<'tcx> {
869    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
870        self.fmt_head(fmt)?;
871        let successor_count = self.successors().count();
872        let labels = self.fmt_successor_labels();
873        assert_eq!(successor_count, labels.len());
874
875        // `Cleanup` is already included in successors
876        let show_unwind = !matches!(self.unwind(), None | Some(UnwindAction::Cleanup(_)));
877        let fmt_unwind = |fmt: &mut Formatter<'_>| -> fmt::Result {
878            write!(fmt, "unwind ")?;
879            match self.unwind() {
880                // Not needed or included in successors
881                None | Some(UnwindAction::Cleanup(_)) => unreachable!(),
882                Some(UnwindAction::Continue) => write!(fmt, "continue"),
883                Some(UnwindAction::Unreachable) => write!(fmt, "unreachable"),
884                Some(UnwindAction::Terminate(reason)) => {
885                    write!(fmt, "terminate({})", reason.as_short_str())
886                }
887            }
888        };
889
890        match (successor_count, show_unwind) {
891            (0, false) => Ok(()),
892            (0, true) => {
893                write!(fmt, " -> ")?;
894                fmt_unwind(fmt)
895            }
896            (1, false) => write!(fmt, " -> {:?}", self.successors().next().unwrap()),
897            _ => {
898                write!(fmt, " -> [")?;
899                for (i, target) in self.successors().enumerate() {
900                    if i > 0 {
901                        write!(fmt, ", ")?;
902                    }
903                    write!(fmt, "{}: {:?}", labels[i], target)?;
904                }
905                if show_unwind {
906                    write!(fmt, ", ")?;
907                    fmt_unwind(fmt)?;
908                }
909                write!(fmt, "]")
910            }
911        }
912    }
913}
914
915impl<'tcx> TerminatorKind<'tcx> {
916    /// Writes the "head" part of the terminator; that is, its name and the data it uses to pick the
917    /// successor basic block, if any. The only information not included is the list of possible
918    /// successors, which may be rendered differently between the text and the graphviz format.
919    pub fn fmt_head<W: fmt::Write>(&self, fmt: &mut W) -> fmt::Result {
920        use self::TerminatorKind::*;
921        match self {
922            Goto { .. } => write!(fmt, "goto"),
923            SwitchInt { discr, .. } => write!(fmt, "switchInt({discr:?})"),
924            Return => write!(fmt, "return"),
925            CoroutineDrop => write!(fmt, "coroutine_drop"),
926            UnwindResume => write!(fmt, "resume"),
927            UnwindTerminate(reason) => {
928                write!(fmt, "terminate({})", reason.as_short_str())
929            }
930            Yield { value, resume_arg, .. } => write!(fmt, "{resume_arg:?} = yield({value:?})"),
931            Unreachable => write!(fmt, "unreachable"),
932            Drop { place, async_fut: None, .. } => write!(fmt, "drop({place:?})"),
933            Drop { place, async_fut: Some(async_fut), .. } => {
934                write!(fmt, "async drop({place:?}; poll={async_fut:?})")
935            }
936            Call { func, args, destination, .. } => {
937                write!(fmt, "{destination:?} = ")?;
938                write!(fmt, "{func:?}(")?;
939                for (index, arg) in args.iter().enumerate() {
940                    if index > 0 {
941                        write!(fmt, ", ")?;
942                    }
943                    write!(fmt, "{:?}", arg.node)?;
944                }
945                write!(fmt, ")")
946            }
947            TailCall { func, args, .. } => {
948                write!(fmt, "tailcall {func:?}(")?;
949                for (index, arg) in args.iter().enumerate() {
950                    if index > 0 {
951                        write!(fmt, ", ")?;
952                    }
953                    write!(fmt, "{:?}", arg.node)?;
954                }
955                write!(fmt, ")")
956            }
957            Assert { cond, expected, msg, .. } => {
958                write!(fmt, "assert(")?;
959                if !expected {
960                    write!(fmt, "!")?;
961                }
962                write!(fmt, "{cond:?}, ")?;
963                msg.fmt_assert_args(fmt)?;
964                write!(fmt, ")")
965            }
966            FalseEdge { .. } => write!(fmt, "falseEdge"),
967            FalseUnwind { .. } => write!(fmt, "falseUnwind"),
968            InlineAsm { template, operands, options, .. } => {
969                write!(fmt, "asm!(\"{}\"", InlineAsmTemplatePiece::to_string(template))?;
970                for op in operands {
971                    write!(fmt, ", ")?;
972                    let print_late = |&late| if late { "late" } else { "" };
973                    match op {
974                        InlineAsmOperand::In { reg, value } => {
975                            write!(fmt, "in({reg}) {value:?}")?;
976                        }
977                        InlineAsmOperand::Out { reg, late, place: Some(place) } => {
978                            write!(fmt, "{}out({}) {:?}", print_late(late), reg, place)?;
979                        }
980                        InlineAsmOperand::Out { reg, late, place: None } => {
981                            write!(fmt, "{}out({}) _", print_late(late), reg)?;
982                        }
983                        InlineAsmOperand::InOut {
984                            reg,
985                            late,
986                            in_value,
987                            out_place: Some(out_place),
988                        } => {
989                            write!(
990                                fmt,
991                                "in{}out({}) {:?} => {:?}",
992                                print_late(late),
993                                reg,
994                                in_value,
995                                out_place
996                            )?;
997                        }
998                        InlineAsmOperand::InOut { reg, late, in_value, out_place: None } => {
999                            write!(fmt, "in{}out({}) {:?} => _", print_late(late), reg, in_value)?;
1000                        }
1001                        InlineAsmOperand::Const { value } => {
1002                            write!(fmt, "const {value:?}")?;
1003                        }
1004                        InlineAsmOperand::SymFn { value } => {
1005                            write!(fmt, "sym_fn {value:?}")?;
1006                        }
1007                        InlineAsmOperand::SymStatic { def_id } => {
1008                            write!(fmt, "sym_static {def_id:?}")?;
1009                        }
1010                        InlineAsmOperand::Label { target_index } => {
1011                            write!(fmt, "label {target_index}")?;
1012                        }
1013                    }
1014                }
1015                write!(fmt, ", options({options:?}))")
1016            }
1017        }
1018    }
1019
1020    /// Returns the list of labels for the edges to the successor basic blocks.
1021    pub fn fmt_successor_labels(&self) -> Vec<Cow<'static, str>> {
1022        use self::TerminatorKind::*;
1023        match *self {
1024            Return
1025            | TailCall { .. }
1026            | UnwindResume
1027            | UnwindTerminate(_)
1028            | Unreachable
1029            | CoroutineDrop => vec![],
1030            Goto { .. } => vec!["".into()],
1031            SwitchInt { ref targets, .. } => targets
1032                .values
1033                .iter()
1034                .map(|&u| Cow::Owned(u.to_string()))
1035                .chain(iter::once("otherwise".into()))
1036                .collect(),
1037            Call { target: Some(_), unwind: UnwindAction::Cleanup(_), .. } => {
1038                vec!["return".into(), "unwind".into()]
1039            }
1040            Call { target: Some(_), unwind: _, .. } => vec!["return".into()],
1041            Call { target: None, unwind: UnwindAction::Cleanup(_), .. } => vec!["unwind".into()],
1042            Call { target: None, unwind: _, .. } => vec![],
1043            Yield { drop: Some(_), .. } => vec!["resume".into(), "drop".into()],
1044            Yield { drop: None, .. } => vec!["resume".into()],
1045            Drop { unwind: UnwindAction::Cleanup(_), drop: Some(_), .. } => {
1046                vec!["return".into(), "unwind".into(), "drop".into()]
1047            }
1048            Drop { unwind: UnwindAction::Cleanup(_), drop: None, .. } => {
1049                vec!["return".into(), "unwind".into()]
1050            }
1051            Drop { unwind: _, drop: Some(_), .. } => vec!["return".into(), "drop".into()],
1052            Drop { unwind: _, .. } => vec!["return".into()],
1053            Assert { unwind: UnwindAction::Cleanup(_), .. } => {
1054                vec!["success".into(), "unwind".into()]
1055            }
1056            Assert { unwind: _, .. } => vec!["success".into()],
1057            FalseEdge { .. } => vec!["real".into(), "imaginary".into()],
1058            FalseUnwind { unwind: UnwindAction::Cleanup(_), .. } => {
1059                vec!["real".into(), "unwind".into()]
1060            }
1061            FalseUnwind { unwind: _, .. } => vec!["real".into()],
1062            InlineAsm { asm_macro, options, ref targets, unwind, .. } => {
1063                let mut vec = Vec::with_capacity(targets.len() + 1);
1064                if !asm_macro.diverges(options) {
1065                    vec.push("return".into());
1066                }
1067                vec.resize(targets.len(), "label".into());
1068
1069                if let UnwindAction::Cleanup(_) = unwind {
1070                    vec.push("unwind".into());
1071                }
1072
1073                vec
1074            }
1075        }
1076    }
1077}
1078
1079impl<'tcx> Debug for Rvalue<'tcx> {
1080    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1081        use self::Rvalue::*;
1082
1083        match *self {
1084            Use(ref place) => write!(fmt, "{place:?}"),
1085            Repeat(ref a, b) => {
1086                write!(fmt, "[{a:?}; ")?;
1087                pretty_print_const(b, fmt, false)?;
1088                write!(fmt, "]")
1089            }
1090            Len(ref a) => write!(fmt, "Len({a:?})"),
1091            Cast(ref kind, ref place, ref ty) => {
1092                with_no_trimmed_paths!(write!(fmt, "{place:?} as {ty} ({kind:?})"))
1093            }
1094            BinaryOp(ref op, box (ref a, ref b)) => write!(fmt, "{op:?}({a:?}, {b:?})"),
1095            UnaryOp(ref op, ref a) => write!(fmt, "{op:?}({a:?})"),
1096            Discriminant(ref place) => write!(fmt, "discriminant({place:?})"),
1097            NullaryOp(ref op, ref t) => {
1098                let t = with_no_trimmed_paths!(format!("{}", t));
1099                match op {
1100                    NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
1101                    NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
1102                    NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
1103                    NullOp::UbChecks => write!(fmt, "UbChecks()"),
1104                    NullOp::ContractChecks => write!(fmt, "ContractChecks()"),
1105                }
1106            }
1107            ThreadLocalRef(did) => ty::tls::with(|tcx| {
1108                let muta = tcx.static_mutability(did).unwrap().prefix_str();
1109                write!(fmt, "&/*tls*/ {}{}", muta, tcx.def_path_str(did))
1110            }),
1111            Ref(region, borrow_kind, ref place) => {
1112                let kind_str = match borrow_kind {
1113                    BorrowKind::Shared => "",
1114                    BorrowKind::Fake(FakeBorrowKind::Deep) => "fake ",
1115                    BorrowKind::Fake(FakeBorrowKind::Shallow) => "fake shallow ",
1116                    BorrowKind::Mut { .. } => "mut ",
1117                };
1118
1119                // When printing regions, add trailing space if necessary.
1120                let print_region = ty::tls::with(|tcx| {
1121                    tcx.sess.verbose_internals() || tcx.sess.opts.unstable_opts.identify_regions
1122                });
1123                let region = if print_region {
1124                    let mut region = region.to_string();
1125                    if !region.is_empty() {
1126                        region.push(' ');
1127                    }
1128                    region
1129                } else {
1130                    // Do not even print 'static
1131                    String::new()
1132                };
1133                write!(fmt, "&{region}{kind_str}{place:?}")
1134            }
1135
1136            CopyForDeref(ref place) => write!(fmt, "deref_copy {place:#?}"),
1137
1138            RawPtr(mutability, ref place) => {
1139                write!(fmt, "&raw {mut_str} {place:?}", mut_str = mutability.ptr_str())
1140            }
1141
1142            Aggregate(ref kind, ref places) => {
1143                let fmt_tuple = |fmt: &mut Formatter<'_>, name: &str| {
1144                    let mut tuple_fmt = fmt.debug_tuple(name);
1145                    for place in places {
1146                        tuple_fmt.field(place);
1147                    }
1148                    tuple_fmt.finish()
1149                };
1150
1151                match **kind {
1152                    AggregateKind::Array(_) => write!(fmt, "{places:?}"),
1153
1154                    AggregateKind::Tuple => {
1155                        if places.is_empty() {
1156                            write!(fmt, "()")
1157                        } else {
1158                            fmt_tuple(fmt, "")
1159                        }
1160                    }
1161
1162                    AggregateKind::Adt(adt_did, variant, args, _user_ty, _) => {
1163                        ty::tls::with(|tcx| {
1164                            let variant_def = &tcx.adt_def(adt_did).variant(variant);
1165                            let args = tcx.lift(args).expect("could not lift for printing");
1166                            let name = FmtPrinter::print_string(tcx, Namespace::ValueNS, |p| {
1167                                p.print_def_path(variant_def.def_id, args)
1168                            })?;
1169
1170                            match variant_def.ctor_kind() {
1171                                Some(CtorKind::Const) => fmt.write_str(&name),
1172                                Some(CtorKind::Fn) => fmt_tuple(fmt, &name),
1173                                None => {
1174                                    let mut struct_fmt = fmt.debug_struct(&name);
1175                                    for (field, place) in iter::zip(&variant_def.fields, places) {
1176                                        struct_fmt.field(field.name.as_str(), place);
1177                                    }
1178                                    struct_fmt.finish()
1179                                }
1180                            }
1181                        })
1182                    }
1183
1184                    AggregateKind::Closure(def_id, args)
1185                    | AggregateKind::CoroutineClosure(def_id, args) => ty::tls::with(|tcx| {
1186                        let name = if tcx.sess.opts.unstable_opts.span_free_formats {
1187                            let args = tcx.lift(args).unwrap();
1188                            format!("{{closure@{}}}", tcx.def_path_str_with_args(def_id, args),)
1189                        } else {
1190                            let span = tcx.def_span(def_id);
1191                            format!(
1192                                "{{closure@{}}}",
1193                                tcx.sess.source_map().span_to_diagnostic_string(span)
1194                            )
1195                        };
1196                        let mut struct_fmt = fmt.debug_struct(&name);
1197
1198                        // FIXME(project-rfc-2229#48): This should be a list of capture names/places
1199                        if let Some(def_id) = def_id.as_local()
1200                            && let Some(upvars) = tcx.upvars_mentioned(def_id)
1201                        {
1202                            for (&var_id, place) in iter::zip(upvars.keys(), places) {
1203                                let var_name = tcx.hir_name(var_id);
1204                                struct_fmt.field(var_name.as_str(), place);
1205                            }
1206                        } else {
1207                            for (index, place) in places.iter().enumerate() {
1208                                struct_fmt.field(&format!("{index}"), place);
1209                            }
1210                        }
1211
1212                        struct_fmt.finish()
1213                    }),
1214
1215                    AggregateKind::Coroutine(def_id, _) => ty::tls::with(|tcx| {
1216                        let name = format!("{{coroutine@{:?}}}", tcx.def_span(def_id));
1217                        let mut struct_fmt = fmt.debug_struct(&name);
1218
1219                        // FIXME(project-rfc-2229#48): This should be a list of capture names/places
1220                        if let Some(def_id) = def_id.as_local()
1221                            && let Some(upvars) = tcx.upvars_mentioned(def_id)
1222                        {
1223                            for (&var_id, place) in iter::zip(upvars.keys(), places) {
1224                                let var_name = tcx.hir_name(var_id);
1225                                struct_fmt.field(var_name.as_str(), place);
1226                            }
1227                        } else {
1228                            for (index, place) in places.iter().enumerate() {
1229                                struct_fmt.field(&format!("{index}"), place);
1230                            }
1231                        }
1232
1233                        struct_fmt.finish()
1234                    }),
1235
1236                    AggregateKind::RawPtr(pointee_ty, mutability) => {
1237                        let kind_str = match mutability {
1238                            Mutability::Mut => "mut",
1239                            Mutability::Not => "const",
1240                        };
1241                        with_no_trimmed_paths!(write!(fmt, "*{kind_str} {pointee_ty} from "))?;
1242                        fmt_tuple(fmt, "")
1243                    }
1244                }
1245            }
1246
1247            ShallowInitBox(ref place, ref ty) => {
1248                with_no_trimmed_paths!(write!(fmt, "ShallowInitBox({place:?}, {ty})"))
1249            }
1250
1251            WrapUnsafeBinder(ref op, ty) => {
1252                with_no_trimmed_paths!(write!(fmt, "wrap_binder!({op:?}; {ty})"))
1253            }
1254        }
1255    }
1256}
1257
1258impl<'tcx> Debug for Operand<'tcx> {
1259    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1260        use self::Operand::*;
1261        match *self {
1262            Constant(ref a) => write!(fmt, "{a:?}"),
1263            Copy(ref place) => write!(fmt, "copy {place:?}"),
1264            Move(ref place) => write!(fmt, "move {place:?}"),
1265        }
1266    }
1267}
1268
1269impl<'tcx> Debug for ConstOperand<'tcx> {
1270    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1271        write!(fmt, "{self}")
1272    }
1273}
1274
1275impl<'tcx> Display for ConstOperand<'tcx> {
1276    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1277        match self.ty().kind() {
1278            ty::FnDef(..) => {}
1279            _ => write!(fmt, "const ")?,
1280        }
1281        Display::fmt(&self.const_, fmt)
1282    }
1283}
1284
1285impl Debug for Place<'_> {
1286    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1287        self.as_ref().fmt(fmt)
1288    }
1289}
1290
1291impl Debug for PlaceRef<'_> {
1292    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
1293        pre_fmt_projection(self.projection, fmt)?;
1294        write!(fmt, "{:?}", self.local)?;
1295        post_fmt_projection(self.projection, fmt)
1296    }
1297}
1298
1299fn pre_fmt_projection(projection: &[PlaceElem<'_>], fmt: &mut Formatter<'_>) -> fmt::Result {
1300    for &elem in projection.iter().rev() {
1301        match elem {
1302            ProjectionElem::OpaqueCast(_)
1303            | ProjectionElem::Subtype(_)
1304            | ProjectionElem::Downcast(_, _)
1305            | ProjectionElem::Field(_, _) => {
1306                write!(fmt, "(")?;
1307            }
1308            ProjectionElem::Deref => {
1309                write!(fmt, "(*")?;
1310            }
1311            ProjectionElem::Index(_)
1312            | ProjectionElem::ConstantIndex { .. }
1313            | ProjectionElem::Subslice { .. } => {}
1314            ProjectionElem::UnwrapUnsafeBinder(_) => {
1315                write!(fmt, "unwrap_binder!(")?;
1316            }
1317        }
1318    }
1319
1320    Ok(())
1321}
1322
1323fn post_fmt_projection(projection: &[PlaceElem<'_>], fmt: &mut Formatter<'_>) -> fmt::Result {
1324    for &elem in projection.iter() {
1325        match elem {
1326            ProjectionElem::OpaqueCast(ty) => {
1327                write!(fmt, " as {ty})")?;
1328            }
1329            ProjectionElem::Subtype(ty) => {
1330                write!(fmt, " as subtype {ty})")?;
1331            }
1332            ProjectionElem::Downcast(Some(name), _index) => {
1333                write!(fmt, " as {name})")?;
1334            }
1335            ProjectionElem::Downcast(None, index) => {
1336                write!(fmt, " as variant#{index:?})")?;
1337            }
1338            ProjectionElem::Deref => {
1339                write!(fmt, ")")?;
1340            }
1341            ProjectionElem::Field(field, ty) => {
1342                with_no_trimmed_paths!(write!(fmt, ".{:?}: {})", field.index(), ty)?);
1343            }
1344            ProjectionElem::Index(ref index) => {
1345                write!(fmt, "[{index:?}]")?;
1346            }
1347            ProjectionElem::ConstantIndex { offset, min_length, from_end: false } => {
1348                write!(fmt, "[{offset:?} of {min_length:?}]")?;
1349            }
1350            ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => {
1351                write!(fmt, "[-{offset:?} of {min_length:?}]")?;
1352            }
1353            ProjectionElem::Subslice { from, to: 0, from_end: true } => {
1354                write!(fmt, "[{from:?}:]")?;
1355            }
1356            ProjectionElem::Subslice { from: 0, to, from_end: true } => {
1357                write!(fmt, "[:-{to:?}]")?;
1358            }
1359            ProjectionElem::Subslice { from, to, from_end: true } => {
1360                write!(fmt, "[{from:?}:-{to:?}]")?;
1361            }
1362            ProjectionElem::Subslice { from, to, from_end: false } => {
1363                write!(fmt, "[{from:?}..{to:?}]")?;
1364            }
1365            ProjectionElem::UnwrapUnsafeBinder(ty) => {
1366                write!(fmt, "; {ty})")?;
1367            }
1368        }
1369    }
1370
1371    Ok(())
1372}
1373
1374/// After we print the main statement, we sometimes dump extra
1375/// information. There's often a lot of little things "nuzzled up" in
1376/// a statement.
1377fn write_extra<'tcx, F>(
1378    tcx: TyCtxt<'tcx>,
1379    write: &mut dyn io::Write,
1380    mut visit_op: F,
1381    options: PrettyPrintMirOptions,
1382) -> io::Result<()>
1383where
1384    F: FnMut(&mut ExtraComments<'tcx>),
1385{
1386    if options.include_extra_comments {
1387        let mut extra_comments = ExtraComments { tcx, comments: vec![] };
1388        visit_op(&mut extra_comments);
1389        for comment in extra_comments.comments {
1390            writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
1391        }
1392    }
1393    Ok(())
1394}
1395
1396struct ExtraComments<'tcx> {
1397    tcx: TyCtxt<'tcx>,
1398    comments: Vec<String>,
1399}
1400
1401impl<'tcx> ExtraComments<'tcx> {
1402    fn push(&mut self, lines: &str) {
1403        for line in lines.split('\n') {
1404            self.comments.push(line.to_string());
1405        }
1406    }
1407}
1408
1409fn use_verbose(ty: Ty<'_>, fn_def: bool) -> bool {
1410    match *ty.kind() {
1411        ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char | ty::Float(_) => false,
1412        // Unit type
1413        ty::Tuple(g_args) if g_args.is_empty() => false,
1414        ty::Tuple(g_args) => g_args.iter().any(|g_arg| use_verbose(g_arg, fn_def)),
1415        ty::Array(ty, _) => use_verbose(ty, fn_def),
1416        ty::FnDef(..) => fn_def,
1417        _ => true,
1418    }
1419}
1420
1421impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
1422    fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, _location: Location) {
1423        let ConstOperand { span, user_ty, const_ } = constant;
1424        if use_verbose(const_.ty(), true) {
1425            self.push("mir::ConstOperand");
1426            self.push(&format!(
1427                "+ span: {}",
1428                self.tcx.sess.source_map().span_to_embeddable_string(*span)
1429            ));
1430            if let Some(user_ty) = user_ty {
1431                self.push(&format!("+ user_ty: {user_ty:?}"));
1432            }
1433
1434            let fmt_val = |val: ConstValue, ty: Ty<'tcx>| {
1435                let tcx = self.tcx;
1436                rustc_data_structures::make_display(move |fmt| {
1437                    pretty_print_const_value_tcx(tcx, val, ty, fmt)
1438                })
1439            };
1440
1441            let fmt_valtree = |cv: &ty::Value<'tcx>| {
1442                let mut p = FmtPrinter::new(self.tcx, Namespace::ValueNS);
1443                p.pretty_print_const_valtree(*cv, /*print_ty*/ true).unwrap();
1444                p.into_buffer()
1445            };
1446
1447            let val = match const_ {
1448                Const::Ty(_, ct) => match ct.kind() {
1449                    ty::ConstKind::Param(p) => format!("ty::Param({p})"),
1450                    ty::ConstKind::Unevaluated(uv) => {
1451                        format!("ty::Unevaluated({}, {:?})", self.tcx.def_path_str(uv.def), uv.args,)
1452                    }
1453                    ty::ConstKind::Value(cv) => {
1454                        format!("ty::Valtree({})", fmt_valtree(&cv))
1455                    }
1456                    // No `ty::` prefix since we also use this to represent errors from `mir::Unevaluated`.
1457                    ty::ConstKind::Error(_) => "Error".to_string(),
1458                    // These variants shouldn't exist in the MIR.
1459                    ty::ConstKind::Placeholder(_)
1460                    | ty::ConstKind::Infer(_)
1461                    | ty::ConstKind::Expr(_)
1462                    | ty::ConstKind::Bound(..) => bug!("unexpected MIR constant: {:?}", const_),
1463                },
1464                Const::Unevaluated(uv, _) => {
1465                    format!(
1466                        "Unevaluated({}, {:?}, {:?})",
1467                        self.tcx.def_path_str(uv.def),
1468                        uv.args,
1469                        uv.promoted,
1470                    )
1471                }
1472                Const::Val(val, ty) => format!("Value({})", fmt_val(*val, *ty)),
1473            };
1474
1475            // This reflects what `Const` looked liked before `val` was renamed
1476            // as `kind`. We print it like this to avoid having to update
1477            // expected output in a lot of tests.
1478            self.push(&format!("+ const_: Const {{ ty: {}, val: {} }}", const_.ty(), val));
1479        }
1480    }
1481
1482    fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
1483        self.super_rvalue(rvalue, location);
1484        if let Rvalue::Aggregate(kind, _) = rvalue {
1485            match **kind {
1486                AggregateKind::Closure(def_id, args) => {
1487                    self.push("closure");
1488                    self.push(&format!("+ def_id: {def_id:?}"));
1489                    self.push(&format!("+ args: {args:#?}"));
1490                }
1491
1492                AggregateKind::Coroutine(def_id, args) => {
1493                    self.push("coroutine");
1494                    self.push(&format!("+ def_id: {def_id:?}"));
1495                    self.push(&format!("+ args: {args:#?}"));
1496                    self.push(&format!("+ kind: {:?}", self.tcx.coroutine_kind(def_id)));
1497                }
1498
1499                AggregateKind::Adt(_, _, _, Some(user_ty), _) => {
1500                    self.push("adt");
1501                    self.push(&format!("+ user_ty: {user_ty:?}"));
1502                }
1503
1504                _ => {}
1505            }
1506        }
1507    }
1508}
1509
1510fn comment(tcx: TyCtxt<'_>, SourceInfo { span, scope }: SourceInfo) -> String {
1511    let location = tcx.sess.source_map().span_to_embeddable_string(span);
1512    format!("scope {} at {}", scope.index(), location,)
1513}
1514
1515///////////////////////////////////////////////////////////////////////////
1516// Allocations
1517
1518/// Find all `AllocId`s mentioned (recursively) in the MIR body and print their corresponding
1519/// allocations.
1520pub fn write_allocations<'tcx>(
1521    tcx: TyCtxt<'tcx>,
1522    body: &Body<'_>,
1523    w: &mut dyn io::Write,
1524) -> io::Result<()> {
1525    fn alloc_ids_from_alloc(
1526        alloc: ConstAllocation<'_>,
1527    ) -> impl DoubleEndedIterator<Item = AllocId> {
1528        alloc.inner().provenance().ptrs().values().map(|p| p.alloc_id())
1529    }
1530
1531    fn alloc_id_from_const_val(val: ConstValue) -> Option<AllocId> {
1532        match val {
1533            ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _)) => Some(ptr.provenance.alloc_id()),
1534            ConstValue::Scalar(interpret::Scalar::Int { .. }) => None,
1535            ConstValue::ZeroSized => None,
1536            ConstValue::Slice { alloc_id, .. } | ConstValue::Indirect { alloc_id, .. } => {
1537                // FIXME: we don't actually want to print all of these, since some are printed nicely directly as values inline in MIR.
1538                // Really we'd want `pretty_print_const_value` to decide which allocations to print, instead of having a separate visitor.
1539                Some(alloc_id)
1540            }
1541        }
1542    }
1543    struct CollectAllocIds(BTreeSet<AllocId>);
1544
1545    impl<'tcx> Visitor<'tcx> for CollectAllocIds {
1546        fn visit_const_operand(&mut self, c: &ConstOperand<'tcx>, _: Location) {
1547            match c.const_ {
1548                Const::Ty(_, _) | Const::Unevaluated(..) => {}
1549                Const::Val(val, _) => {
1550                    if let Some(id) = alloc_id_from_const_val(val) {
1551                        self.0.insert(id);
1552                    }
1553                }
1554            }
1555        }
1556    }
1557
1558    let mut visitor = CollectAllocIds(Default::default());
1559    visitor.visit_body(body);
1560
1561    // `seen` contains all seen allocations, including the ones we have *not* printed yet.
1562    // The protocol is to first `insert` into `seen`, and only if that returns `true`
1563    // then push to `todo`.
1564    let mut seen = visitor.0;
1565    let mut todo: Vec<_> = seen.iter().copied().collect();
1566    while let Some(id) = todo.pop() {
1567        let mut write_allocation_track_relocs =
1568            |w: &mut dyn io::Write, alloc: ConstAllocation<'tcx>| -> io::Result<()> {
1569                // `.rev()` because we are popping them from the back of the `todo` vector.
1570                for id in alloc_ids_from_alloc(alloc).rev() {
1571                    if seen.insert(id) {
1572                        todo.push(id);
1573                    }
1574                }
1575                write!(w, "{}", display_allocation(tcx, alloc.inner()))
1576            };
1577        write!(w, "\n{id:?}")?;
1578        match tcx.try_get_global_alloc(id) {
1579            // This can't really happen unless there are bugs, but it doesn't cost us anything to
1580            // gracefully handle it and allow buggy rustc to be debugged via allocation printing.
1581            None => write!(w, " (deallocated)")?,
1582            Some(GlobalAlloc::Function { instance, .. }) => write!(w, " (fn: {instance})")?,
1583            Some(GlobalAlloc::VTable(ty, dyn_ty)) => {
1584                write!(w, " (vtable: impl {dyn_ty} for {ty})")?
1585            }
1586            Some(GlobalAlloc::TypeId { ty }) => write!(w, " (typeid for {ty})")?,
1587            Some(GlobalAlloc::Static(did)) if !tcx.is_foreign_item(did) => {
1588                write!(w, " (static: {}", tcx.def_path_str(did))?;
1589                if body.phase <= MirPhase::Runtime(RuntimePhase::PostCleanup)
1590                    && body
1591                        .source
1592                        .def_id()
1593                        .as_local()
1594                        .is_some_and(|def_id| tcx.hir_body_const_context(def_id).is_some())
1595                {
1596                    // Statics may be cyclic and evaluating them too early
1597                    // in the MIR pipeline may cause cycle errors even though
1598                    // normal compilation is fine.
1599                    write!(w, ")")?;
1600                } else {
1601                    match tcx.eval_static_initializer(did) {
1602                        Ok(alloc) => {
1603                            write!(w, ", ")?;
1604                            write_allocation_track_relocs(w, alloc)?;
1605                        }
1606                        Err(_) => write!(w, ", error during initializer evaluation)")?,
1607                    }
1608                }
1609            }
1610            Some(GlobalAlloc::Static(did)) => {
1611                write!(w, " (extern static: {})", tcx.def_path_str(did))?
1612            }
1613            Some(GlobalAlloc::Memory(alloc)) => {
1614                write!(w, " (")?;
1615                write_allocation_track_relocs(w, alloc)?
1616            }
1617        }
1618        writeln!(w)?;
1619    }
1620    Ok(())
1621}
1622
1623/// Dumps the size and metadata and content of an allocation to the given writer.
1624/// The expectation is that the caller first prints other relevant metadata, so the exact
1625/// format of this function is (*without* leading or trailing newline):
1626///
1627/// ```text
1628/// size: {}, align: {}) {
1629///     <bytes>
1630/// }
1631/// ```
1632///
1633/// The byte format is similar to how hex editors print bytes. Each line starts with the address of
1634/// the start of the line, followed by all bytes in hex format (space separated).
1635/// If the allocation is small enough to fit into a single line, no start address is given.
1636/// After the hex dump, an ascii dump follows, replacing all unprintable characters (control
1637/// characters or characters whose value is larger than 127) with a `.`
1638/// This also prints provenance adequately.
1639pub fn display_allocation<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>(
1640    tcx: TyCtxt<'tcx>,
1641    alloc: &'a Allocation<Prov, Extra, Bytes>,
1642) -> RenderAllocation<'a, 'tcx, Prov, Extra, Bytes> {
1643    RenderAllocation { tcx, alloc }
1644}
1645
1646#[doc(hidden)]
1647pub struct RenderAllocation<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes> {
1648    tcx: TyCtxt<'tcx>,
1649    alloc: &'a Allocation<Prov, Extra, Bytes>,
1650}
1651
1652impl<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes> std::fmt::Display
1653    for RenderAllocation<'a, 'tcx, Prov, Extra, Bytes>
1654{
1655    fn fmt(&self, w: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1656        let RenderAllocation { tcx, alloc } = *self;
1657        write!(w, "size: {}, align: {})", alloc.size().bytes(), alloc.align.bytes())?;
1658        if alloc.size() == Size::ZERO {
1659            // We are done.
1660            return write!(w, " {{}}");
1661        }
1662        if tcx.sess.opts.unstable_opts.dump_mir_exclude_alloc_bytes {
1663            return write!(w, " {{ .. }}");
1664        }
1665        // Write allocation bytes.
1666        writeln!(w, " {{")?;
1667        write_allocation_bytes(tcx, alloc, w, "    ")?;
1668        write!(w, "}}")?;
1669        Ok(())
1670    }
1671}
1672
1673fn write_allocation_endline(w: &mut dyn std::fmt::Write, ascii: &str) -> std::fmt::Result {
1674    for _ in 0..(BYTES_PER_LINE - ascii.chars().count()) {
1675        write!(w, "   ")?;
1676    }
1677    writeln!(w, " │ {ascii}")
1678}
1679
1680/// Number of bytes to print per allocation hex dump line.
1681const BYTES_PER_LINE: usize = 16;
1682
1683/// Prints the line start address and returns the new line start address.
1684fn write_allocation_newline(
1685    w: &mut dyn std::fmt::Write,
1686    mut line_start: Size,
1687    ascii: &str,
1688    pos_width: usize,
1689    prefix: &str,
1690) -> Result<Size, std::fmt::Error> {
1691    write_allocation_endline(w, ascii)?;
1692    line_start += Size::from_bytes(BYTES_PER_LINE);
1693    write!(w, "{}0x{:02$x} │ ", prefix, line_start.bytes(), pos_width)?;
1694    Ok(line_start)
1695}
1696
1697/// The `prefix` argument allows callers to add an arbitrary prefix before each line (even if there
1698/// is only one line). Note that your prefix should contain a trailing space as the lines are
1699/// printed directly after it.
1700pub fn write_allocation_bytes<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>(
1701    tcx: TyCtxt<'tcx>,
1702    alloc: &Allocation<Prov, Extra, Bytes>,
1703    w: &mut dyn std::fmt::Write,
1704    prefix: &str,
1705) -> std::fmt::Result {
1706    let num_lines = alloc.size().bytes_usize().saturating_sub(BYTES_PER_LINE);
1707    // Number of chars needed to represent all line numbers.
1708    let pos_width = hex_number_length(alloc.size().bytes());
1709
1710    if num_lines > 0 {
1711        write!(w, "{}0x{:02$x} │ ", prefix, 0, pos_width)?;
1712    } else {
1713        write!(w, "{prefix}")?;
1714    }
1715
1716    let mut i = Size::ZERO;
1717    let mut line_start = Size::ZERO;
1718
1719    let ptr_size = tcx.data_layout.pointer_size();
1720
1721    let mut ascii = String::new();
1722
1723    let oversized_ptr = |target: &mut String, width| {
1724        if target.len() > width {
1725            write!(target, " ({} ptr bytes)", ptr_size.bytes()).unwrap();
1726        }
1727    };
1728
1729    while i < alloc.size() {
1730        // The line start already has a space. While we could remove that space from the line start
1731        // printing and unconditionally print a space here, that would cause the single-line case
1732        // to have a single space before it, which looks weird.
1733        if i != line_start {
1734            write!(w, " ")?;
1735        }
1736        if let Some(prov) = alloc.provenance().get_ptr(i) {
1737            // Memory with provenance must be defined
1738            assert!(alloc.init_mask().is_range_initialized(alloc_range(i, ptr_size)).is_ok());
1739            let j = i.bytes_usize();
1740            let offset = alloc
1741                .inspect_with_uninit_and_ptr_outside_interpreter(j..j + ptr_size.bytes_usize());
1742            let offset = read_target_uint(tcx.data_layout.endian, offset).unwrap();
1743            let offset = Size::from_bytes(offset);
1744            let provenance_width = |bytes| bytes * 3;
1745            let ptr = Pointer::new(prov, offset);
1746            let mut target = format!("{ptr:?}");
1747            if target.len() > provenance_width(ptr_size.bytes_usize() - 1) {
1748                // This is too long, try to save some space.
1749                target = format!("{ptr:#?}");
1750            }
1751            if ((i - line_start) + ptr_size).bytes_usize() > BYTES_PER_LINE {
1752                // This branch handles the situation where a provenance starts in the current line
1753                // but ends in the next one.
1754                let remainder = Size::from_bytes(BYTES_PER_LINE) - (i - line_start);
1755                let overflow = ptr_size - remainder;
1756                let remainder_width = provenance_width(remainder.bytes_usize()) - 2;
1757                let overflow_width = provenance_width(overflow.bytes_usize() - 1) + 1;
1758                ascii.push('╾'); // HEAVY LEFT AND LIGHT RIGHT
1759                for _ in 1..remainder.bytes() {
1760                    ascii.push('─'); // LIGHT HORIZONTAL
1761                }
1762                if overflow_width > remainder_width && overflow_width >= target.len() {
1763                    // The case where the provenance fits into the part in the next line
1764                    write!(w, "╾{0:─^1$}", "", remainder_width)?;
1765                    line_start =
1766                        write_allocation_newline(w, line_start, &ascii, pos_width, prefix)?;
1767                    ascii.clear();
1768                    write!(w, "{target:─^overflow_width$}╼")?;
1769                } else {
1770                    oversized_ptr(&mut target, remainder_width);
1771                    write!(w, "╾{target:─^remainder_width$}")?;
1772                    line_start =
1773                        write_allocation_newline(w, line_start, &ascii, pos_width, prefix)?;
1774                    write!(w, "{0:─^1$}╼", "", overflow_width)?;
1775                    ascii.clear();
1776                }
1777                for _ in 0..overflow.bytes() - 1 {
1778                    ascii.push('─');
1779                }
1780                ascii.push('╼'); // LIGHT LEFT AND HEAVY RIGHT
1781                i += ptr_size;
1782                continue;
1783            } else {
1784                // This branch handles a provenance that starts and ends in the current line.
1785                let provenance_width = provenance_width(ptr_size.bytes_usize() - 1);
1786                oversized_ptr(&mut target, provenance_width);
1787                ascii.push('╾');
1788                write!(w, "╾{target:─^provenance_width$}╼")?;
1789                for _ in 0..ptr_size.bytes() - 2 {
1790                    ascii.push('─');
1791                }
1792                ascii.push('╼');
1793                i += ptr_size;
1794            }
1795        } else if let Some((prov, idx)) = alloc.provenance().get_byte(i, &tcx) {
1796            // Memory with provenance must be defined
1797            assert!(
1798                alloc.init_mask().is_range_initialized(alloc_range(i, Size::from_bytes(1))).is_ok()
1799            );
1800            ascii.push('━'); // HEAVY HORIZONTAL
1801            // We have two characters to display this, which is obviously not enough.
1802            // Format is similar to "oversized" above.
1803            let j = i.bytes_usize();
1804            let c = alloc.inspect_with_uninit_and_ptr_outside_interpreter(j..j + 1)[0];
1805            write!(w, "╾{c:02x}{prov:#?} (ptr fragment {idx})╼")?;
1806            i += Size::from_bytes(1);
1807        } else if alloc
1808            .init_mask()
1809            .is_range_initialized(alloc_range(i, Size::from_bytes(1)))
1810            .is_ok()
1811        {
1812            let j = i.bytes_usize();
1813
1814            // Checked definedness (and thus range) and provenance. This access also doesn't
1815            // influence interpreter execution but is only for debugging.
1816            let c = alloc.inspect_with_uninit_and_ptr_outside_interpreter(j..j + 1)[0];
1817            write!(w, "{c:02x}")?;
1818            if c.is_ascii_control() || c >= 0x80 {
1819                ascii.push('.');
1820            } else {
1821                ascii.push(char::from(c));
1822            }
1823            i += Size::from_bytes(1);
1824        } else {
1825            write!(w, "__")?;
1826            ascii.push('░');
1827            i += Size::from_bytes(1);
1828        }
1829        // Print a new line header if the next line still has some bytes to print.
1830        if i == line_start + Size::from_bytes(BYTES_PER_LINE) && i != alloc.size() {
1831            line_start = write_allocation_newline(w, line_start, &ascii, pos_width, prefix)?;
1832            ascii.clear();
1833        }
1834    }
1835    write_allocation_endline(w, &ascii)?;
1836
1837    Ok(())
1838}
1839
1840///////////////////////////////////////////////////////////////////////////
1841// Constants
1842
1843fn pretty_print_byte_str(fmt: &mut Formatter<'_>, byte_str: &[u8]) -> fmt::Result {
1844    write!(fmt, "b\"{}\"", byte_str.escape_ascii())
1845}
1846
1847fn comma_sep<'tcx>(
1848    tcx: TyCtxt<'tcx>,
1849    fmt: &mut Formatter<'_>,
1850    elems: Vec<(ConstValue, Ty<'tcx>)>,
1851) -> fmt::Result {
1852    let mut first = true;
1853    for (ct, ty) in elems {
1854        if !first {
1855            fmt.write_str(", ")?;
1856        }
1857        pretty_print_const_value_tcx(tcx, ct, ty, fmt)?;
1858        first = false;
1859    }
1860    Ok(())
1861}
1862
1863fn pretty_print_const_value_tcx<'tcx>(
1864    tcx: TyCtxt<'tcx>,
1865    ct: ConstValue,
1866    ty: Ty<'tcx>,
1867    fmt: &mut Formatter<'_>,
1868) -> fmt::Result {
1869    use crate::ty::print::PrettyPrinter;
1870
1871    if tcx.sess.verbose_internals() {
1872        fmt.write_str(&format!("ConstValue({ct:?}: {ty})"))?;
1873        return Ok(());
1874    }
1875
1876    let u8_type = tcx.types.u8;
1877    match (ct, ty.kind()) {
1878        // Byte/string slices, printed as (byte) string literals.
1879        (_, ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Str) => {
1880            if let Some(data) = ct.try_get_slice_bytes_for_diagnostics(tcx) {
1881                fmt.write_str(&format!("{:?}", String::from_utf8_lossy(data)))?;
1882                return Ok(());
1883            }
1884        }
1885        (_, ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Slice(t) if *t == u8_type) => {
1886            if let Some(data) = ct.try_get_slice_bytes_for_diagnostics(tcx) {
1887                pretty_print_byte_str(fmt, data)?;
1888                return Ok(());
1889            }
1890        }
1891        (ConstValue::Indirect { alloc_id, offset }, ty::Array(t, n)) if *t == u8_type => {
1892            let n = n.try_to_target_usize(tcx).unwrap();
1893            let alloc = tcx.global_alloc(alloc_id).unwrap_memory();
1894            // cast is ok because we already checked for pointer size (32 or 64 bit) above
1895            let range = AllocRange { start: offset, size: Size::from_bytes(n) };
1896            let byte_str = alloc.inner().get_bytes_strip_provenance(&tcx, range).unwrap();
1897            fmt.write_str("*")?;
1898            pretty_print_byte_str(fmt, byte_str)?;
1899            return Ok(());
1900        }
1901        // Aggregates, printed as array/tuple/struct/variant construction syntax.
1902        //
1903        // NB: the `has_non_region_param` check ensures that we can use
1904        // the `destructure_const` query with an empty `ty::ParamEnv` without
1905        // introducing ICEs (e.g. via `layout_of`) from missing bounds.
1906        // E.g. `transmute([0usize; 2]): (u8, *mut T)` needs to know `T: Sized`
1907        // to be able to destructure the tuple into `(0u8, *mut T)`
1908        (_, ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) if !ty.has_non_region_param() => {
1909            let ct = tcx.lift(ct).unwrap();
1910            let ty = tcx.lift(ty).unwrap();
1911            if let Some(contents) = tcx.try_destructure_mir_constant_for_user_output(ct, ty) {
1912                let fields: Vec<(ConstValue, Ty<'_>)> = contents.fields.to_vec();
1913                match *ty.kind() {
1914                    ty::Array(..) => {
1915                        fmt.write_str("[")?;
1916                        comma_sep(tcx, fmt, fields)?;
1917                        fmt.write_str("]")?;
1918                    }
1919                    ty::Tuple(..) => {
1920                        fmt.write_str("(")?;
1921                        comma_sep(tcx, fmt, fields)?;
1922                        if contents.fields.len() == 1 {
1923                            fmt.write_str(",")?;
1924                        }
1925                        fmt.write_str(")")?;
1926                    }
1927                    ty::Adt(def, _) if def.variants().is_empty() => {
1928                        fmt.write_str(&format!("{{unreachable(): {ty}}}"))?;
1929                    }
1930                    ty::Adt(def, args) => {
1931                        let variant_idx = contents
1932                            .variant
1933                            .expect("destructed mir constant of adt without variant idx");
1934                        let variant_def = &def.variant(variant_idx);
1935                        let args = tcx.lift(args).unwrap();
1936                        let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
1937                        p.print_alloc_ids = true;
1938                        p.pretty_print_value_path(variant_def.def_id, args)?;
1939                        fmt.write_str(&p.into_buffer())?;
1940
1941                        match variant_def.ctor_kind() {
1942                            Some(CtorKind::Const) => {}
1943                            Some(CtorKind::Fn) => {
1944                                fmt.write_str("(")?;
1945                                comma_sep(tcx, fmt, fields)?;
1946                                fmt.write_str(")")?;
1947                            }
1948                            None => {
1949                                fmt.write_str(" {{ ")?;
1950                                let mut first = true;
1951                                for (field_def, (ct, ty)) in iter::zip(&variant_def.fields, fields)
1952                                {
1953                                    if !first {
1954                                        fmt.write_str(", ")?;
1955                                    }
1956                                    write!(fmt, "{}: ", field_def.name)?;
1957                                    pretty_print_const_value_tcx(tcx, ct, ty, fmt)?;
1958                                    first = false;
1959                                }
1960                                fmt.write_str(" }}")?;
1961                            }
1962                        }
1963                    }
1964                    _ => unreachable!(),
1965                }
1966                return Ok(());
1967            }
1968        }
1969        (ConstValue::Scalar(scalar), _) => {
1970            let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
1971            p.print_alloc_ids = true;
1972            let ty = tcx.lift(ty).unwrap();
1973            p.pretty_print_const_scalar(scalar, ty)?;
1974            fmt.write_str(&p.into_buffer())?;
1975            return Ok(());
1976        }
1977        (ConstValue::ZeroSized, ty::FnDef(d, s)) => {
1978            let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
1979            p.print_alloc_ids = true;
1980            p.pretty_print_value_path(*d, s)?;
1981            fmt.write_str(&p.into_buffer())?;
1982            return Ok(());
1983        }
1984        // FIXME(oli-obk): also pretty print arrays and other aggregate constants by reading
1985        // their fields instead of just dumping the memory.
1986        _ => {}
1987    }
1988    // Fall back to debug pretty printing for invalid constants.
1989    write!(fmt, "{ct:?}: {ty}")
1990}
1991
1992pub(crate) fn pretty_print_const_value<'tcx>(
1993    ct: ConstValue,
1994    ty: Ty<'tcx>,
1995    fmt: &mut Formatter<'_>,
1996) -> fmt::Result {
1997    ty::tls::with(|tcx| {
1998        let ct = tcx.lift(ct).unwrap();
1999        let ty = tcx.lift(ty).unwrap();
2000        pretty_print_const_value_tcx(tcx, ct, ty, fmt)
2001    })
2002}
2003
2004///////////////////////////////////////////////////////////////////////////
2005// Miscellaneous
2006
2007/// Calc converted u64 decimal into hex and return its length in chars.
2008///
2009/// ```ignore (cannot-test-private-function)
2010/// assert_eq!(1, hex_number_length(0));
2011/// assert_eq!(1, hex_number_length(1));
2012/// assert_eq!(2, hex_number_length(16));
2013/// ```
2014fn hex_number_length(x: u64) -> usize {
2015    if x == 0 {
2016        return 1;
2017    }
2018    let mut length = 0;
2019    let mut x_left = x;
2020    while x_left > 0 {
2021        x_left /= 16;
2022        length += 1;
2023    }
2024    length
2025}