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 = " ";
20pub(crate) const ALIGN: usize = 40;
22
23#[derive(Clone, Copy)]
26pub enum PassWhere {
27 BeforeCFG,
29
30 AfterCFG,
32
33 BeforeBlock(BasicBlock),
35
36 BeforeLocation(Location),
38
39 AfterLocation(Location),
41
42 AfterTerminator(BasicBlock),
44}
45
46#[derive(Copy, Clone)]
49pub struct PrettyPrintMirOptions {
50 pub include_extra_comments: bool,
52}
53
54impl PrettyPrintMirOptions {
55 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#[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#[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 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
147pub 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 let def_path =
172 ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
173 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
214fn 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 let shim_disambiguator = match source.instance {
245 ty::InstanceKind::DropGlue(_, Some(ty)) => {
246 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
308pub 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
334pub 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 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 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 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
385pub 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
412fn 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 for var_debug_info in &body.var_debug_info {
426 if var_debug_info.source_info.scope != parent {
427 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 for (local, local_decl) in body.local_decls.iter_enumerated() {
448 if (1..body.arg_count + 1).contains(&local.index()) {
449 continue;
451 }
452
453 if local_decl.source_info.scope != parent {
454 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
547fn 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 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 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 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 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 ")?, (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 (DefKind::AnonConst | DefKind::InlineConst, _) => {}
646 (DefKind::GlobalAsm, _) => {}
648 _ => bug!("Unexpected def kind {:?}", kind),
649 }
650
651 ty::print::with_forced_impl_filename_line! {
652 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 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 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
719fn 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 let cleanup_text = if data.is_cleanup { " (cleanup)" } else { "" };
738 writeln!(w, "{INDENT}{block:?}{cleanup_text}: {{")?;
739
740 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 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 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 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 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 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 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 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 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 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 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
1374fn 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 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, 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 ty::ConstKind::Error(_) => "Error".to_string(),
1458 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 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
1515pub 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 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 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 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 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 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
1623pub 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 return write!(w, " {{}}");
1661 }
1662 if tcx.sess.opts.unstable_opts.dump_mir_exclude_alloc_bytes {
1663 return write!(w, " {{ .. }}");
1664 }
1665 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
1680const BYTES_PER_LINE: usize = 16;
1682
1683fn 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
1697pub 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 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 if i != line_start {
1734 write!(w, " ")?;
1735 }
1736 if let Some(prov) = alloc.provenance().get_ptr(i) {
1737 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 target = format!("{ptr:#?}");
1750 }
1751 if ((i - line_start) + ptr_size).bytes_usize() > BYTES_PER_LINE {
1752 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('╾'); for _ in 1..remainder.bytes() {
1760 ascii.push('─'); }
1762 if overflow_width > remainder_width && overflow_width >= target.len() {
1763 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('╼'); i += ptr_size;
1782 continue;
1783 } else {
1784 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 assert!(
1798 alloc.init_mask().is_range_initialized(alloc_range(i, Size::from_bytes(1))).is_ok()
1799 );
1800 ascii.push('━'); 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 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 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
1840fn 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 (_, 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 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 (_, 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 _ => {}
1987 }
1988 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
2004fn 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}