1use std::cell::Cell;
2use std::fmt::{self, Write as _};
3use std::iter;
4use std::ops::{Deref, DerefMut};
5
6use rustc_abi::{ExternAbi, Size};
7use rustc_apfloat::Float;
8use rustc_apfloat::ieee::{Double, Half, Quad, Single};
9use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
10use rustc_data_structures::unord::UnordMap;
11use rustc_hir as hir;
12use rustc_hir::LangItem;
13use rustc_hir::def::{self, CtorKind, DefKind, Namespace};
14use rustc_hir::def_id::{CRATE_DEF_ID, DefIdMap, DefIdSet, LOCAL_CRATE, ModDefId};
15use rustc_hir::definitions::{DefKey, DefPathDataName};
16use rustc_macros::{Lift, extension};
17use rustc_session::Limit;
18use rustc_session::cstore::{ExternCrate, ExternCrateSource};
19use rustc_span::{FileNameDisplayPreference, Ident, Symbol, kw, sym};
20use rustc_type_ir::{Upcast as _, elaborate};
21use smallvec::SmallVec;
22
23use super::*;
25use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar};
26use crate::query::{IntoQueryParam, Providers};
27use crate::ty::{
28 ConstInt, Expr, GenericArgKind, ParamConst, ScalarInt, Term, TermKind, TraitPredicate,
29 TypeFoldable, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
30};
31
32macro_rules! p {
33 (@$lit:literal) => {
34 write!(scoped_cx!(), $lit)?
35 };
36 (@write($($data:expr),+)) => {
37 write!(scoped_cx!(), $($data),+)?
38 };
39 (@print($x:expr)) => {
40 $x.print(scoped_cx!())?
41 };
42 (@$method:ident($($arg:expr),*)) => {
43 scoped_cx!().$method($($arg),*)?
44 };
45 ($($elem:tt $(($($args:tt)*))?),+) => {{
46 $(p!(@ $elem $(($($args)*))?);)+
47 }};
48}
49macro_rules! define_scoped_cx {
50 ($cx:ident) => {
51 macro_rules! scoped_cx {
52 () => {
53 $cx
54 };
55 }
56 };
57}
58
59thread_local! {
60 static FORCE_IMPL_FILENAME_LINE: Cell<bool> = const { Cell::new(false) };
61 static SHOULD_PREFIX_WITH_CRATE: Cell<bool> = const { Cell::new(false) };
62 static NO_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
63 static FORCE_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
64 static REDUCED_QUERIES: Cell<bool> = const { Cell::new(false) };
65 static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
66 static RTN_MODE: Cell<RtnMode> = const { Cell::new(RtnMode::ForDiagnostic) };
67}
68
69#[derive(Copy, Clone, PartialEq, Eq, Debug)]
71pub enum RtnMode {
72 ForDiagnostic,
74 ForSignature,
76 ForSuggestion,
78}
79
80macro_rules! define_helper {
81 ($($(#[$a:meta])* fn $name:ident($helper:ident, $tl:ident);)+) => {
82 $(
83 #[must_use]
84 pub struct $helper(bool);
85
86 impl $helper {
87 pub fn new() -> $helper {
88 $helper($tl.with(|c| c.replace(true)))
89 }
90 }
91
92 $(#[$a])*
93 pub macro $name($e:expr) {
94 {
95 let _guard = $helper::new();
96 $e
97 }
98 }
99
100 impl Drop for $helper {
101 fn drop(&mut self) {
102 $tl.with(|c| c.set(self.0))
103 }
104 }
105
106 pub fn $name() -> bool {
107 $tl.with(|c| c.get())
108 }
109 )+
110 }
111}
112
113define_helper!(
114 fn with_reduced_queries(ReducedQueriesGuard, REDUCED_QUERIES);
122 fn with_forced_impl_filename_line(ForcedImplGuard, FORCE_IMPL_FILENAME_LINE);
127 fn with_crate_prefix(CratePrefixGuard, SHOULD_PREFIX_WITH_CRATE);
129 fn with_no_trimmed_paths(NoTrimmedGuard, NO_TRIMMED_PATH);
133 fn with_forced_trimmed_paths(ForceTrimmedGuard, FORCE_TRIMMED_PATH);
134 fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH);
137);
138
139#[must_use]
140pub struct RtnModeHelper(RtnMode);
141
142impl RtnModeHelper {
143 pub fn with(mode: RtnMode) -> RtnModeHelper {
144 RtnModeHelper(RTN_MODE.with(|c| c.replace(mode)))
145 }
146}
147
148impl Drop for RtnModeHelper {
149 fn drop(&mut self) {
150 RTN_MODE.with(|c| c.set(self.0))
151 }
152}
153
154pub macro with_types_for_suggestion($e:expr) {{
159 let _guard = $crate::ty::print::pretty::RtnModeHelper::with(RtnMode::ForSuggestion);
160 $e
161}}
162
163pub macro with_types_for_signature($e:expr) {{
167 let _guard = $crate::ty::print::pretty::RtnModeHelper::with(RtnMode::ForSignature);
168 $e
169}}
170
171pub macro with_no_queries($e:expr) {{
173 $crate::ty::print::with_reduced_queries!($crate::ty::print::with_forced_impl_filename_line!(
174 $crate::ty::print::with_no_trimmed_paths!($crate::ty::print::with_no_visible_paths!(
175 $crate::ty::print::with_forced_impl_filename_line!($e)
176 ))
177 ))
178}}
179
180#[derive(Copy, Clone, Debug, PartialEq, Eq)]
181pub enum WrapBinderMode {
182 ForAll,
183 Unsafe,
184}
185impl WrapBinderMode {
186 pub fn start_str(self) -> &'static str {
187 match self {
188 WrapBinderMode::ForAll => "for<",
189 WrapBinderMode::Unsafe => "unsafe<",
190 }
191 }
192}
193
194#[derive(Copy, Clone, Default)]
202pub struct RegionHighlightMode<'tcx> {
203 highlight_regions: [Option<(ty::Region<'tcx>, usize)>; 3],
206
207 highlight_bound_region: Option<(ty::BoundRegionKind, usize)>,
215}
216
217impl<'tcx> RegionHighlightMode<'tcx> {
218 pub fn maybe_highlighting_region(
221 &mut self,
222 region: Option<ty::Region<'tcx>>,
223 number: Option<usize>,
224 ) {
225 if let Some(k) = region {
226 if let Some(n) = number {
227 self.highlighting_region(k, n);
228 }
229 }
230 }
231
232 pub fn highlighting_region(&mut self, region: ty::Region<'tcx>, number: usize) {
234 let num_slots = self.highlight_regions.len();
235 let first_avail_slot =
236 self.highlight_regions.iter_mut().find(|s| s.is_none()).unwrap_or_else(|| {
237 bug!("can only highlight {} placeholders at a time", num_slots,)
238 });
239 *first_avail_slot = Some((region, number));
240 }
241
242 pub fn highlighting_region_vid(
244 &mut self,
245 tcx: TyCtxt<'tcx>,
246 vid: ty::RegionVid,
247 number: usize,
248 ) {
249 self.highlighting_region(ty::Region::new_var(tcx, vid), number)
250 }
251
252 fn region_highlighted(&self, region: ty::Region<'tcx>) -> Option<usize> {
254 self.highlight_regions.iter().find_map(|h| match h {
255 Some((r, n)) if *r == region => Some(*n),
256 _ => None,
257 })
258 }
259
260 pub fn highlighting_bound_region(&mut self, br: ty::BoundRegionKind, number: usize) {
264 assert!(self.highlight_bound_region.is_none());
265 self.highlight_bound_region = Some((br, number));
266 }
267}
268
269pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
271 fn print_value_path(
273 &mut self,
274 def_id: DefId,
275 args: &'tcx [GenericArg<'tcx>],
276 ) -> Result<(), PrintError> {
277 self.print_def_path(def_id, args)
278 }
279
280 fn print_in_binder<T>(&mut self, value: &ty::Binder<'tcx, T>) -> Result<(), PrintError>
281 where
282 T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
283 {
284 value.as_ref().skip_binder().print(self)
285 }
286
287 fn wrap_binder<T, F: FnOnce(&T, &mut Self) -> Result<(), fmt::Error>>(
288 &mut self,
289 value: &ty::Binder<'tcx, T>,
290 _mode: WrapBinderMode,
291 f: F,
292 ) -> Result<(), PrintError>
293 where
294 T: TypeFoldable<TyCtxt<'tcx>>,
295 {
296 f(value.as_ref().skip_binder(), self)
297 }
298
299 fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
301 where
302 T: Print<'tcx, Self>,
303 {
304 if let Some(first) = elems.next() {
305 first.print(self)?;
306 for elem in elems {
307 self.write_str(", ")?;
308 elem.print(self)?;
309 }
310 }
311 Ok(())
312 }
313
314 fn typed_value(
316 &mut self,
317 f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
318 t: impl FnOnce(&mut Self) -> Result<(), PrintError>,
319 conversion: &str,
320 ) -> Result<(), PrintError> {
321 self.write_str("{")?;
322 f(self)?;
323 self.write_str(conversion)?;
324 t(self)?;
325 self.write_str("}")?;
326 Ok(())
327 }
328
329 fn parenthesized(
331 &mut self,
332 f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
333 ) -> Result<(), PrintError> {
334 self.write_str("(")?;
335 f(self)?;
336 self.write_str(")")?;
337 Ok(())
338 }
339
340 fn maybe_parenthesized(
342 &mut self,
343 f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
344 parenthesized: bool,
345 ) -> Result<(), PrintError> {
346 if parenthesized {
347 self.parenthesized(f)?;
348 } else {
349 f(self)?;
350 }
351 Ok(())
352 }
353
354 fn generic_delimiters(
356 &mut self,
357 f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
358 ) -> Result<(), PrintError>;
359
360 fn should_print_region(&self, region: ty::Region<'tcx>) -> bool;
364
365 fn reset_type_limit(&mut self) {}
366
367 fn try_print_visible_def_path(&mut self, def_id: DefId) -> Result<bool, PrintError> {
373 if with_no_visible_paths() {
374 return Ok(false);
375 }
376
377 let mut callers = Vec::new();
378 self.try_print_visible_def_path_recur(def_id, &mut callers)
379 }
380
381 fn force_print_trimmed_def_path(&mut self, def_id: DefId) -> Result<bool, PrintError> {
387 let key = self.tcx().def_key(def_id);
388 let visible_parent_map = self.tcx().visible_parent_map(());
389 let kind = self.tcx().def_kind(def_id);
390
391 let get_local_name = |this: &Self, name, def_id, key: DefKey| {
392 if let Some(visible_parent) = visible_parent_map.get(&def_id)
393 && let actual_parent = this.tcx().opt_parent(def_id)
394 && let DefPathData::TypeNs(_) = key.disambiguated_data.data
395 && Some(*visible_parent) != actual_parent
396 {
397 this.tcx()
398 .module_children(ModDefId::new_unchecked(*visible_parent))
400 .iter()
401 .filter(|child| child.res.opt_def_id() == Some(def_id))
402 .find(|child| child.vis.is_public() && child.ident.name != kw::Underscore)
403 .map(|child| child.ident.name)
404 .unwrap_or(name)
405 } else {
406 name
407 }
408 };
409 if let DefKind::Variant = kind
410 && let Some(symbol) = self.tcx().trimmed_def_paths(()).get(&def_id)
411 {
412 self.write_str(get_local_name(self, *symbol, def_id, key).as_str())?;
414 return Ok(true);
415 }
416 if let Some(symbol) = key.get_opt_name() {
417 if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = kind
418 && let Some(parent) = self.tcx().opt_parent(def_id)
419 && let parent_key = self.tcx().def_key(parent)
420 && let Some(symbol) = parent_key.get_opt_name()
421 {
422 self.write_str(get_local_name(self, symbol, parent, parent_key).as_str())?;
424 self.write_str("::")?;
425 } else if let DefKind::Variant = kind
426 && let Some(parent) = self.tcx().opt_parent(def_id)
427 && let parent_key = self.tcx().def_key(parent)
428 && let Some(symbol) = parent_key.get_opt_name()
429 {
430 self.write_str(get_local_name(self, symbol, parent, parent_key).as_str())?;
435 self.write_str("::")?;
436 } else if let DefKind::Struct
437 | DefKind::Union
438 | DefKind::Enum
439 | DefKind::Trait
440 | DefKind::TyAlias
441 | DefKind::Fn
442 | DefKind::Const
443 | DefKind::Static { .. } = kind
444 {
445 } else {
446 return Ok(false);
448 }
449 self.write_str(get_local_name(self, symbol, def_id, key).as_str())?;
450 return Ok(true);
451 }
452 Ok(false)
453 }
454
455 fn try_print_trimmed_def_path(&mut self, def_id: DefId) -> Result<bool, PrintError> {
457 if with_forced_trimmed_paths() && self.force_print_trimmed_def_path(def_id)? {
458 return Ok(true);
459 }
460 if self.tcx().sess.opts.unstable_opts.trim_diagnostic_paths
461 && self.tcx().sess.opts.trimmed_def_paths
462 && !with_no_trimmed_paths()
463 && !with_crate_prefix()
464 && let Some(symbol) = self.tcx().trimmed_def_paths(()).get(&def_id)
465 {
466 write!(self, "{}", Ident::with_dummy_span(*symbol))?;
467 Ok(true)
468 } else {
469 Ok(false)
470 }
471 }
472
473 fn try_print_visible_def_path_recur(
487 &mut self,
488 def_id: DefId,
489 callers: &mut Vec<DefId>,
490 ) -> Result<bool, PrintError> {
491 debug!("try_print_visible_def_path: def_id={:?}", def_id);
492
493 if let Some(cnum) = def_id.as_crate_root() {
496 if cnum == LOCAL_CRATE {
497 self.path_crate(cnum)?;
498 return Ok(true);
499 }
500
501 match self.tcx().extern_crate(cnum) {
512 Some(&ExternCrate { src, dependency_of, span, .. }) => match (src, dependency_of) {
513 (ExternCrateSource::Extern(def_id), LOCAL_CRATE) => {
514 if span.is_dummy() {
521 self.path_crate(cnum)?;
522 return Ok(true);
523 }
524
525 with_no_visible_paths!(self.print_def_path(def_id, &[])?);
531
532 return Ok(true);
533 }
534 (ExternCrateSource::Path, LOCAL_CRATE) => {
535 self.path_crate(cnum)?;
536 return Ok(true);
537 }
538 _ => {}
539 },
540 None => {
541 self.path_crate(cnum)?;
542 return Ok(true);
543 }
544 }
545 }
546
547 if def_id.is_local() {
548 return Ok(false);
549 }
550
551 let visible_parent_map = self.tcx().visible_parent_map(());
552
553 let mut cur_def_key = self.tcx().def_key(def_id);
554 debug!("try_print_visible_def_path: cur_def_key={:?}", cur_def_key);
555
556 if let DefPathData::Ctor = cur_def_key.disambiguated_data.data {
558 let parent = DefId {
559 krate: def_id.krate,
560 index: cur_def_key
561 .parent
562 .expect("`DefPathData::Ctor` / `VariantData` missing a parent"),
563 };
564
565 cur_def_key = self.tcx().def_key(parent);
566 }
567
568 let Some(visible_parent) = visible_parent_map.get(&def_id).cloned() else {
569 return Ok(false);
570 };
571
572 let actual_parent = self.tcx().opt_parent(def_id);
573 debug!(
574 "try_print_visible_def_path: visible_parent={:?} actual_parent={:?}",
575 visible_parent, actual_parent,
576 );
577
578 let mut data = cur_def_key.disambiguated_data.data;
579 debug!(
580 "try_print_visible_def_path: data={:?} visible_parent={:?} actual_parent={:?}",
581 data, visible_parent, actual_parent,
582 );
583
584 match data {
585 DefPathData::TypeNs(Some(ref mut name)) if Some(visible_parent) != actual_parent => {
617 let reexport = self
620 .tcx()
621 .module_children(ModDefId::new_unchecked(visible_parent))
623 .iter()
624 .filter(|child| child.res.opt_def_id() == Some(def_id))
625 .find(|child| child.vis.is_public() && child.ident.name != kw::Underscore)
626 .map(|child| child.ident.name);
627
628 if let Some(new_name) = reexport {
629 *name = new_name;
630 } else {
631 return Ok(false);
633 }
634 }
635 DefPathData::CrateRoot => {
637 data = DefPathData::TypeNs(Some(self.tcx().crate_name(def_id.krate)));
638 }
639 _ => {}
640 }
641 debug!("try_print_visible_def_path: data={:?}", data);
642
643 if callers.contains(&visible_parent) {
644 return Ok(false);
645 }
646 callers.push(visible_parent);
647 match self.try_print_visible_def_path_recur(visible_parent, callers)? {
652 false => return Ok(false),
653 true => {}
654 }
655 callers.pop();
656 self.path_append(|_| Ok(()), &DisambiguatedDefPathData { data, disambiguator: 0 })?;
657 Ok(true)
658 }
659
660 fn pretty_path_qualified(
661 &mut self,
662 self_ty: Ty<'tcx>,
663 trait_ref: Option<ty::TraitRef<'tcx>>,
664 ) -> Result<(), PrintError> {
665 if trait_ref.is_none() {
666 match self_ty.kind() {
670 ty::Adt(..)
671 | ty::Foreign(_)
672 | ty::Bool
673 | ty::Char
674 | ty::Str
675 | ty::Int(_)
676 | ty::Uint(_)
677 | ty::Float(_) => {
678 return self_ty.print(self);
679 }
680
681 _ => {}
682 }
683 }
684
685 self.generic_delimiters(|cx| {
686 define_scoped_cx!(cx);
687
688 p!(print(self_ty));
689 if let Some(trait_ref) = trait_ref {
690 p!(" as ", print(trait_ref.print_only_trait_path()));
691 }
692 Ok(())
693 })
694 }
695
696 fn pretty_path_append_impl(
697 &mut self,
698 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
699 self_ty: Ty<'tcx>,
700 trait_ref: Option<ty::TraitRef<'tcx>>,
701 ) -> Result<(), PrintError> {
702 print_prefix(self)?;
703
704 self.generic_delimiters(|cx| {
705 define_scoped_cx!(cx);
706
707 p!("impl ");
708 if let Some(trait_ref) = trait_ref {
709 p!(print(trait_ref.print_only_trait_path()), " for ");
710 }
711 p!(print(self_ty));
712
713 Ok(())
714 })
715 }
716
717 fn pretty_print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
718 define_scoped_cx!(self);
719
720 match *ty.kind() {
721 ty::Bool => p!("bool"),
722 ty::Char => p!("char"),
723 ty::Int(t) => p!(write("{}", t.name_str())),
724 ty::Uint(t) => p!(write("{}", t.name_str())),
725 ty::Float(t) => p!(write("{}", t.name_str())),
726 ty::Pat(ty, pat) => {
727 p!("(", print(ty), ") is ", write("{pat:?}"))
728 }
729 ty::RawPtr(ty, mutbl) => {
730 p!(write("*{} ", mutbl.ptr_str()));
731 p!(print(ty))
732 }
733 ty::Ref(r, ty, mutbl) => {
734 p!("&");
735 if self.should_print_region(r) {
736 p!(print(r), " ");
737 }
738 p!(print(ty::TypeAndMut { ty, mutbl }))
739 }
740 ty::Never => p!("!"),
741 ty::Tuple(tys) => {
742 p!("(", comma_sep(tys.iter()));
743 if tys.len() == 1 {
744 p!(",");
745 }
746 p!(")")
747 }
748 ty::FnDef(def_id, args) => {
749 if with_reduced_queries() {
750 p!(print_def_path(def_id, args));
751 } else {
752 let mut sig = self.tcx().fn_sig(def_id).instantiate(self.tcx(), args);
753 if self.tcx().codegen_fn_attrs(def_id).safe_target_features {
754 p!("#[target_features] ");
755 sig = sig.map_bound(|mut sig| {
756 sig.safety = hir::Safety::Safe;
757 sig
758 });
759 }
760 p!(print(sig), " {{", print_value_path(def_id, args), "}}");
761 }
762 }
763 ty::FnPtr(ref sig_tys, hdr) => p!(print(sig_tys.with(hdr))),
764 ty::UnsafeBinder(ref bound_ty) => {
765 self.wrap_binder(bound_ty, WrapBinderMode::Unsafe, |ty, cx| {
766 cx.pretty_print_type(*ty)
767 })?;
768 }
769 ty::Infer(infer_ty) => {
770 if self.should_print_verbose() {
771 p!(write("{:?}", ty.kind()));
772 return Ok(());
773 }
774
775 if let ty::TyVar(ty_vid) = infer_ty {
776 if let Some(name) = self.ty_infer_name(ty_vid) {
777 p!(write("{}", name))
778 } else {
779 p!(write("{}", infer_ty))
780 }
781 } else {
782 p!(write("{}", infer_ty))
783 }
784 }
785 ty::Error(_) => p!("{{type error}}"),
786 ty::Param(ref param_ty) => p!(print(param_ty)),
787 ty::Bound(debruijn, bound_ty) => match bound_ty.kind {
788 ty::BoundTyKind::Anon => {
789 rustc_type_ir::debug_bound_var(self, debruijn, bound_ty.var)?
790 }
791 ty::BoundTyKind::Param(_, s) => match self.should_print_verbose() {
792 true => p!(write("{:?}", ty.kind())),
793 false => p!(write("{s}")),
794 },
795 },
796 ty::Adt(def, args) => {
797 p!(print_def_path(def.did(), args));
798 }
799 ty::Dynamic(data, r, repr) => {
800 let print_r = self.should_print_region(r);
801 if print_r {
802 p!("(");
803 }
804 match repr {
805 ty::Dyn => p!("dyn "),
806 ty::DynStar => p!("dyn* "),
807 }
808 p!(print(data));
809 if print_r {
810 p!(" + ", print(r), ")");
811 }
812 }
813 ty::Foreign(def_id) => {
814 p!(print_def_path(def_id, &[]));
815 }
816 ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ref data) => {
817 p!(print(data))
818 }
819 ty::Placeholder(placeholder) => match placeholder.bound.kind {
820 ty::BoundTyKind::Anon => p!(write("{placeholder:?}")),
821 ty::BoundTyKind::Param(_, name) => match self.should_print_verbose() {
822 true => p!(write("{:?}", ty.kind())),
823 false => p!(write("{name}")),
824 },
825 },
826 ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
827 if self.should_print_verbose() {
836 p!(write("Opaque({:?}, {})", def_id, args.print_as_list()));
838 return Ok(());
839 }
840
841 let parent = self.tcx().parent(def_id);
842 match self.tcx().def_kind(parent) {
843 DefKind::TyAlias | DefKind::AssocTy => {
844 if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: d, .. }) =
847 *self.tcx().type_of(parent).instantiate_identity().kind()
848 {
849 if d == def_id {
850 p!(print_def_path(parent, args));
853 return Ok(());
854 }
855 }
856 p!(print_def_path(def_id, args));
858 return Ok(());
859 }
860 _ => {
861 if with_reduced_queries() {
862 p!(print_def_path(def_id, &[]));
863 return Ok(());
864 } else {
865 return self.pretty_print_opaque_impl_type(def_id, args);
866 }
867 }
868 }
869 }
870 ty::Str => p!("str"),
871 ty::Coroutine(did, args) => {
872 p!("{{");
873 let coroutine_kind = self.tcx().coroutine_kind(did).unwrap();
874 let should_print_movability = self.should_print_verbose()
875 || matches!(coroutine_kind, hir::CoroutineKind::Coroutine(_));
876
877 if should_print_movability {
878 match coroutine_kind.movability() {
879 hir::Movability::Movable => {}
880 hir::Movability::Static => p!("static "),
881 }
882 }
883
884 if !self.should_print_verbose() {
885 p!(write("{}", coroutine_kind));
886 if coroutine_kind.is_fn_like() {
887 let did_of_the_fn_item = self.tcx().parent(did);
894 p!(" of ", print_def_path(did_of_the_fn_item, args), "()");
895 } else if let Some(local_did) = did.as_local() {
896 let span = self.tcx().def_span(local_did);
897 p!(write(
898 "@{}",
899 self.tcx().sess.source_map().span_to_embeddable_string(span)
902 ));
903 } else {
904 p!("@", print_def_path(did, args));
905 }
906 } else {
907 p!(print_def_path(did, args));
908 p!(
909 " upvar_tys=",
910 print(args.as_coroutine().tupled_upvars_ty()),
911 " resume_ty=",
912 print(args.as_coroutine().resume_ty()),
913 " yield_ty=",
914 print(args.as_coroutine().yield_ty()),
915 " return_ty=",
916 print(args.as_coroutine().return_ty()),
917 " witness=",
918 print(args.as_coroutine().witness())
919 );
920 }
921
922 p!("}}")
923 }
924 ty::CoroutineWitness(did, args) => {
925 p!(write("{{"));
926 if !self.tcx().sess.verbose_internals() {
927 p!("coroutine witness");
928 if let Some(did) = did.as_local() {
929 let span = self.tcx().def_span(did);
930 p!(write(
931 "@{}",
932 self.tcx().sess.source_map().span_to_embeddable_string(span)
935 ));
936 } else {
937 p!(write("@"), print_def_path(did, args));
938 }
939 } else {
940 p!(print_def_path(did, args));
941 }
942
943 p!("}}")
944 }
945 ty::Closure(did, args) => {
946 p!(write("{{"));
947 if !self.should_print_verbose() {
948 p!(write("closure"));
949 if self.should_truncate() {
950 write!(self, "@...}}")?;
951 return Ok(());
952 } else {
953 if let Some(did) = did.as_local() {
954 if self.tcx().sess.opts.unstable_opts.span_free_formats {
955 p!("@", print_def_path(did.to_def_id(), args));
956 } else {
957 let span = self.tcx().def_span(did);
958 let preference = if with_forced_trimmed_paths() {
959 FileNameDisplayPreference::Short
960 } else {
961 FileNameDisplayPreference::Remapped
962 };
963 p!(write(
964 "@{}",
965 self.tcx().sess.source_map().span_to_string(span, preference)
968 ));
969 }
970 } else {
971 p!(write("@"), print_def_path(did, args));
972 }
973 }
974 } else {
975 p!(print_def_path(did, args));
976 p!(
977 " closure_kind_ty=",
978 print(args.as_closure().kind_ty()),
979 " closure_sig_as_fn_ptr_ty=",
980 print(args.as_closure().sig_as_fn_ptr_ty()),
981 " upvar_tys=",
982 print(args.as_closure().tupled_upvars_ty())
983 );
984 }
985 p!("}}");
986 }
987 ty::CoroutineClosure(did, args) => {
988 p!(write("{{"));
989 if !self.should_print_verbose() {
990 match self.tcx().coroutine_kind(self.tcx().coroutine_for_closure(did)).unwrap()
991 {
992 hir::CoroutineKind::Desugared(
993 hir::CoroutineDesugaring::Async,
994 hir::CoroutineSource::Closure,
995 ) => p!("async closure"),
996 hir::CoroutineKind::Desugared(
997 hir::CoroutineDesugaring::AsyncGen,
998 hir::CoroutineSource::Closure,
999 ) => p!("async gen closure"),
1000 hir::CoroutineKind::Desugared(
1001 hir::CoroutineDesugaring::Gen,
1002 hir::CoroutineSource::Closure,
1003 ) => p!("gen closure"),
1004 _ => unreachable!(
1005 "coroutine from coroutine-closure should have CoroutineSource::Closure"
1006 ),
1007 }
1008 if let Some(did) = did.as_local() {
1009 if self.tcx().sess.opts.unstable_opts.span_free_formats {
1010 p!("@", print_def_path(did.to_def_id(), args));
1011 } else {
1012 let span = self.tcx().def_span(did);
1013 let preference = if with_forced_trimmed_paths() {
1014 FileNameDisplayPreference::Short
1015 } else {
1016 FileNameDisplayPreference::Remapped
1017 };
1018 p!(write(
1019 "@{}",
1020 self.tcx().sess.source_map().span_to_string(span, preference)
1023 ));
1024 }
1025 } else {
1026 p!(write("@"), print_def_path(did, args));
1027 }
1028 } else {
1029 p!(print_def_path(did, args));
1030 p!(
1031 " closure_kind_ty=",
1032 print(args.as_coroutine_closure().kind_ty()),
1033 " signature_parts_ty=",
1034 print(args.as_coroutine_closure().signature_parts_ty()),
1035 " upvar_tys=",
1036 print(args.as_coroutine_closure().tupled_upvars_ty()),
1037 " coroutine_captures_by_ref_ty=",
1038 print(args.as_coroutine_closure().coroutine_captures_by_ref_ty()),
1039 " coroutine_witness_ty=",
1040 print(args.as_coroutine_closure().coroutine_witness_ty())
1041 );
1042 }
1043 p!("}}");
1044 }
1045 ty::Array(ty, sz) => p!("[", print(ty), "; ", print(sz), "]"),
1046 ty::Slice(ty) => p!("[", print(ty), "]"),
1047 }
1048
1049 Ok(())
1050 }
1051
1052 fn pretty_print_opaque_impl_type(
1053 &mut self,
1054 def_id: DefId,
1055 args: ty::GenericArgsRef<'tcx>,
1056 ) -> Result<(), PrintError> {
1057 let tcx = self.tcx();
1058
1059 let bounds = tcx.explicit_item_bounds(def_id);
1062
1063 let mut traits = FxIndexMap::default();
1064 let mut fn_traits = FxIndexMap::default();
1065 let mut has_sized_bound = false;
1066 let mut has_negative_sized_bound = false;
1067 let mut lifetimes = SmallVec::<[ty::Region<'tcx>; 1]>::new();
1068
1069 for (predicate, _) in bounds.iter_instantiated_copied(tcx, args) {
1070 let bound_predicate = predicate.kind();
1071
1072 match bound_predicate.skip_binder() {
1073 ty::ClauseKind::Trait(pred) => {
1074 if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
1076 match pred.polarity {
1077 ty::PredicatePolarity::Positive => {
1078 has_sized_bound = true;
1079 continue;
1080 }
1081 ty::PredicatePolarity::Negative => has_negative_sized_bound = true,
1082 }
1083 }
1084
1085 self.insert_trait_and_projection(
1086 bound_predicate.rebind(pred),
1087 None,
1088 &mut traits,
1089 &mut fn_traits,
1090 );
1091 }
1092 ty::ClauseKind::Projection(pred) => {
1093 let proj = bound_predicate.rebind(pred);
1094 let trait_ref = proj.map_bound(|proj| TraitPredicate {
1095 trait_ref: proj.projection_term.trait_ref(tcx),
1096 polarity: ty::PredicatePolarity::Positive,
1097 });
1098
1099 self.insert_trait_and_projection(
1100 trait_ref,
1101 Some((proj.item_def_id(), proj.term())),
1102 &mut traits,
1103 &mut fn_traits,
1104 );
1105 }
1106 ty::ClauseKind::TypeOutlives(outlives) => {
1107 lifetimes.push(outlives.1);
1108 }
1109 _ => {}
1110 }
1111 }
1112
1113 write!(self, "impl ")?;
1114
1115 let mut first = true;
1116 let paren_needed = fn_traits.len() > 1 || traits.len() > 0 || !has_sized_bound;
1118
1119 for ((bound_args_and_self_ty, is_async), entry) in fn_traits {
1120 write!(self, "{}", if first { "" } else { " + " })?;
1121 write!(self, "{}", if paren_needed { "(" } else { "" })?;
1122
1123 let trait_def_id = if is_async {
1124 tcx.async_fn_trait_kind_to_def_id(entry.kind).expect("expected AsyncFn lang items")
1125 } else {
1126 tcx.fn_trait_kind_to_def_id(entry.kind).expect("expected Fn lang items")
1127 };
1128
1129 if let Some(return_ty) = entry.return_ty {
1130 self.wrap_binder(
1131 &bound_args_and_self_ty,
1132 WrapBinderMode::ForAll,
1133 |(args, _), cx| {
1134 define_scoped_cx!(cx);
1135 p!(write("{}", tcx.item_name(trait_def_id)));
1136 p!("(");
1137
1138 for (idx, ty) in args.iter().enumerate() {
1139 if idx > 0 {
1140 p!(", ");
1141 }
1142 p!(print(ty));
1143 }
1144
1145 p!(")");
1146 if let Some(ty) = return_ty.skip_binder().as_type() {
1147 if !ty.is_unit() {
1148 p!(" -> ", print(return_ty));
1149 }
1150 }
1151 p!(write("{}", if paren_needed { ")" } else { "" }));
1152
1153 first = false;
1154 Ok(())
1155 },
1156 )?;
1157 } else {
1158 traits.insert(
1160 bound_args_and_self_ty.map_bound(|(args, self_ty)| ty::TraitPredicate {
1161 polarity: ty::PredicatePolarity::Positive,
1162 trait_ref: ty::TraitRef::new(
1163 tcx,
1164 trait_def_id,
1165 [self_ty, Ty::new_tup(tcx, args)],
1166 ),
1167 }),
1168 FxIndexMap::default(),
1169 );
1170 }
1171 }
1172
1173 for (trait_pred, assoc_items) in traits {
1175 write!(self, "{}", if first { "" } else { " + " })?;
1176
1177 self.wrap_binder(&trait_pred, WrapBinderMode::ForAll, |trait_pred, cx| {
1178 define_scoped_cx!(cx);
1179
1180 if trait_pred.polarity == ty::PredicatePolarity::Negative {
1181 p!("!");
1182 }
1183 p!(print(trait_pred.trait_ref.print_only_trait_name()));
1184
1185 let generics = tcx.generics_of(trait_pred.def_id());
1186 let own_args = generics.own_args_no_defaults(tcx, trait_pred.trait_ref.args);
1187
1188 if !own_args.is_empty() || !assoc_items.is_empty() {
1189 let mut first = true;
1190
1191 for ty in own_args {
1192 if first {
1193 p!("<");
1194 first = false;
1195 } else {
1196 p!(", ");
1197 }
1198 p!(print(ty));
1199 }
1200
1201 for (assoc_item_def_id, term) in assoc_items {
1202 let term = if let Some(ty) = term.skip_binder().as_type()
1205 && let ty::Alias(ty::Projection, proj) = ty.kind()
1206 && let Some(assoc) = tcx.opt_associated_item(proj.def_id)
1207 && assoc
1208 .trait_container(tcx)
1209 .is_some_and(|def_id| tcx.is_lang_item(def_id, LangItem::Coroutine))
1210 && assoc.name == rustc_span::sym::Return
1211 {
1212 if let ty::Coroutine(_, args) = args.type_at(0).kind() {
1213 let return_ty = args.as_coroutine().return_ty();
1214 if !return_ty.is_ty_var() {
1215 return_ty.into()
1216 } else {
1217 continue;
1218 }
1219 } else {
1220 continue;
1221 }
1222 } else {
1223 term.skip_binder()
1224 };
1225
1226 if first {
1227 p!("<");
1228 first = false;
1229 } else {
1230 p!(", ");
1231 }
1232
1233 p!(write("{} = ", tcx.associated_item(assoc_item_def_id).name));
1234
1235 match term.unpack() {
1236 TermKind::Ty(ty) => p!(print(ty)),
1237 TermKind::Const(c) => p!(print(c)),
1238 };
1239 }
1240
1241 if !first {
1242 p!(">");
1243 }
1244 }
1245
1246 first = false;
1247 Ok(())
1248 })?;
1249 }
1250
1251 let add_sized = has_sized_bound && (first || has_negative_sized_bound);
1252 let add_maybe_sized = !has_sized_bound && !has_negative_sized_bound;
1253 if add_sized || add_maybe_sized {
1254 if !first {
1255 write!(self, " + ")?;
1256 }
1257 if add_maybe_sized {
1258 write!(self, "?")?;
1259 }
1260 write!(self, "Sized")?;
1261 }
1262
1263 if !with_forced_trimmed_paths() {
1264 for re in lifetimes {
1265 write!(self, " + ")?;
1266 self.print_region(re)?;
1267 }
1268 }
1269
1270 Ok(())
1271 }
1272
1273 fn insert_trait_and_projection(
1276 &mut self,
1277 trait_pred: ty::PolyTraitPredicate<'tcx>,
1278 proj_ty: Option<(DefId, ty::Binder<'tcx, Term<'tcx>>)>,
1279 traits: &mut FxIndexMap<
1280 ty::PolyTraitPredicate<'tcx>,
1281 FxIndexMap<DefId, ty::Binder<'tcx, Term<'tcx>>>,
1282 >,
1283 fn_traits: &mut FxIndexMap<
1284 (ty::Binder<'tcx, (&'tcx ty::List<Ty<'tcx>>, Ty<'tcx>)>, bool),
1285 OpaqueFnEntry<'tcx>,
1286 >,
1287 ) {
1288 let tcx = self.tcx();
1289 let trait_def_id = trait_pred.def_id();
1290
1291 let fn_trait_and_async = if let Some(kind) = tcx.fn_trait_kind_from_def_id(trait_def_id) {
1292 Some((kind, false))
1293 } else if let Some(kind) = tcx.async_fn_trait_kind_from_def_id(trait_def_id) {
1294 Some((kind, true))
1295 } else {
1296 None
1297 };
1298
1299 if trait_pred.polarity() == ty::PredicatePolarity::Positive
1300 && let Some((kind, is_async)) = fn_trait_and_async
1301 && let ty::Tuple(types) = *trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
1302 {
1303 let entry = fn_traits
1304 .entry((trait_pred.rebind((types, trait_pred.skip_binder().self_ty())), is_async))
1305 .or_insert_with(|| OpaqueFnEntry { kind, return_ty: None });
1306 if kind.extends(entry.kind) {
1307 entry.kind = kind;
1308 }
1309 if let Some((proj_def_id, proj_ty)) = proj_ty
1310 && tcx.item_name(proj_def_id) == sym::Output
1311 {
1312 entry.return_ty = Some(proj_ty);
1313 }
1314 return;
1315 }
1316
1317 traits.entry(trait_pred).or_default().extend(proj_ty);
1319 }
1320
1321 fn pretty_print_inherent_projection(
1322 &mut self,
1323 alias_ty: ty::AliasTerm<'tcx>,
1324 ) -> Result<(), PrintError> {
1325 let def_key = self.tcx().def_key(alias_ty.def_id);
1326 self.path_generic_args(
1327 |cx| {
1328 cx.path_append(
1329 |cx| cx.path_qualified(alias_ty.self_ty(), None),
1330 &def_key.disambiguated_data,
1331 )
1332 },
1333 &alias_ty.args[1..],
1334 )
1335 }
1336
1337 fn pretty_print_rpitit(
1338 &mut self,
1339 def_id: DefId,
1340 args: ty::GenericArgsRef<'tcx>,
1341 ) -> Result<(), PrintError> {
1342 let fn_args = if self.tcx().features().return_type_notation()
1343 && let Some(ty::ImplTraitInTraitData::Trait { fn_def_id, .. }) =
1344 self.tcx().opt_rpitit_info(def_id)
1345 && let ty::Alias(_, alias_ty) =
1346 self.tcx().fn_sig(fn_def_id).skip_binder().output().skip_binder().kind()
1347 && alias_ty.def_id == def_id
1348 && let generics = self.tcx().generics_of(fn_def_id)
1349 && generics.own_params.iter().all(|param| matches!(param.kind, ty::GenericParamDefKind::Lifetime))
1351 {
1352 let num_args = generics.count();
1353 Some((fn_def_id, &args[..num_args]))
1354 } else {
1355 None
1356 };
1357
1358 match (fn_args, RTN_MODE.with(|c| c.get())) {
1359 (Some((fn_def_id, fn_args)), RtnMode::ForDiagnostic) => {
1360 self.pretty_print_opaque_impl_type(def_id, args)?;
1361 write!(self, " {{ ")?;
1362 self.print_def_path(fn_def_id, fn_args)?;
1363 write!(self, "(..) }}")?;
1364 }
1365 (Some((fn_def_id, fn_args)), RtnMode::ForSuggestion) => {
1366 self.print_def_path(fn_def_id, fn_args)?;
1367 write!(self, "(..)")?;
1368 }
1369 _ => {
1370 self.pretty_print_opaque_impl_type(def_id, args)?;
1371 }
1372 }
1373
1374 Ok(())
1375 }
1376
1377 fn ty_infer_name(&self, _: ty::TyVid) -> Option<Symbol> {
1378 None
1379 }
1380
1381 fn const_infer_name(&self, _: ty::ConstVid) -> Option<Symbol> {
1382 None
1383 }
1384
1385 fn pretty_print_dyn_existential(
1386 &mut self,
1387 predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
1388 ) -> Result<(), PrintError> {
1389 let mut first = true;
1391
1392 if let Some(bound_principal) = predicates.principal() {
1393 self.wrap_binder(&bound_principal, WrapBinderMode::ForAll, |principal, cx| {
1394 define_scoped_cx!(cx);
1395 p!(print_def_path(principal.def_id, &[]));
1396
1397 let mut resugared = false;
1398
1399 let fn_trait_kind = cx.tcx().fn_trait_kind_from_def_id(principal.def_id);
1401 if !cx.should_print_verbose() && fn_trait_kind.is_some() {
1402 if let ty::Tuple(tys) = principal.args.type_at(0).kind() {
1403 let mut projections = predicates.projection_bounds();
1404 if let (Some(proj), None) = (projections.next(), projections.next()) {
1405 p!(pretty_fn_sig(
1406 tys,
1407 false,
1408 proj.skip_binder().term.as_type().expect("Return type was a const")
1409 ));
1410 resugared = true;
1411 }
1412 }
1413 }
1414
1415 if !resugared {
1418 let principal_with_self =
1419 principal.with_self_ty(cx.tcx(), cx.tcx().types.trait_object_dummy_self);
1420
1421 let args = cx
1422 .tcx()
1423 .generics_of(principal_with_self.def_id)
1424 .own_args_no_defaults(cx.tcx(), principal_with_self.args);
1425
1426 let bound_principal_with_self = bound_principal
1427 .with_self_ty(cx.tcx(), cx.tcx().types.trait_object_dummy_self);
1428
1429 let clause: ty::Clause<'tcx> = bound_principal_with_self.upcast(cx.tcx());
1430 let super_projections: Vec<_> = elaborate::elaborate(cx.tcx(), [clause])
1431 .filter_only_self()
1432 .filter_map(|clause| clause.as_projection_clause())
1433 .collect();
1434
1435 let mut projections: Vec<_> = predicates
1436 .projection_bounds()
1437 .filter(|&proj| {
1438 let proj_is_implied = super_projections.iter().any(|&super_proj| {
1440 let super_proj = super_proj.map_bound(|super_proj| {
1441 ty::ExistentialProjection::erase_self_ty(cx.tcx(), super_proj)
1442 });
1443
1444 let proj = cx.tcx().erase_regions(proj);
1449 let proj = cx.tcx().anonymize_bound_vars(proj);
1450 let super_proj = cx.tcx().erase_regions(super_proj);
1451 let super_proj = cx.tcx().anonymize_bound_vars(super_proj);
1452
1453 proj == super_proj
1454 });
1455 !proj_is_implied
1456 })
1457 .map(|proj| {
1458 proj.skip_binder()
1461 })
1462 .collect();
1463
1464 projections
1465 .sort_by_cached_key(|proj| cx.tcx().item_name(proj.def_id).to_string());
1466
1467 if !args.is_empty() || !projections.is_empty() {
1468 p!(generic_delimiters(|cx| {
1469 cx.comma_sep(args.iter().copied())?;
1470 if !args.is_empty() && !projections.is_empty() {
1471 write!(cx, ", ")?;
1472 }
1473 cx.comma_sep(projections.iter().copied())
1474 }));
1475 }
1476 }
1477 Ok(())
1478 })?;
1479
1480 first = false;
1481 }
1482
1483 define_scoped_cx!(self);
1484
1485 let mut auto_traits: Vec<_> = predicates.auto_traits().collect();
1489
1490 auto_traits.sort_by_cached_key(|did| with_no_trimmed_paths!(self.tcx().def_path_str(*did)));
1498
1499 for def_id in auto_traits {
1500 if !first {
1501 p!(" + ");
1502 }
1503 first = false;
1504
1505 p!(print_def_path(def_id, &[]));
1506 }
1507
1508 Ok(())
1509 }
1510
1511 fn pretty_fn_sig(
1512 &mut self,
1513 inputs: &[Ty<'tcx>],
1514 c_variadic: bool,
1515 output: Ty<'tcx>,
1516 ) -> Result<(), PrintError> {
1517 define_scoped_cx!(self);
1518
1519 p!("(", comma_sep(inputs.iter().copied()));
1520 if c_variadic {
1521 if !inputs.is_empty() {
1522 p!(", ");
1523 }
1524 p!("...");
1525 }
1526 p!(")");
1527 if !output.is_unit() {
1528 p!(" -> ", print(output));
1529 }
1530
1531 Ok(())
1532 }
1533
1534 fn pretty_print_const(
1535 &mut self,
1536 ct: ty::Const<'tcx>,
1537 print_ty: bool,
1538 ) -> Result<(), PrintError> {
1539 define_scoped_cx!(self);
1540
1541 if self.should_print_verbose() {
1542 p!(write("{:?}", ct));
1543 return Ok(());
1544 }
1545
1546 match ct.kind() {
1547 ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args }) => {
1548 match self.tcx().def_kind(def) {
1549 DefKind::Const | DefKind::AssocConst => {
1550 p!(print_value_path(def, args))
1551 }
1552 DefKind::AnonConst => {
1553 if def.is_local()
1554 && let span = self.tcx().def_span(def)
1555 && let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span)
1556 {
1557 p!(write("{}", snip))
1558 } else {
1559 p!(write(
1565 "{}::{}",
1566 self.tcx().crate_name(def.krate),
1567 self.tcx().def_path(def).to_string_no_crate_verbose()
1568 ))
1569 }
1570 }
1571 defkind => bug!("`{:?}` has unexpected defkind {:?}", ct, defkind),
1572 }
1573 }
1574 ty::ConstKind::Infer(infer_ct) => match infer_ct {
1575 ty::InferConst::Var(ct_vid) if let Some(name) = self.const_infer_name(ct_vid) => {
1576 p!(write("{}", name))
1577 }
1578 _ => write!(self, "_")?,
1579 },
1580 ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)),
1581 ty::ConstKind::Value(cv) => {
1582 return self.pretty_print_const_valtree(cv, print_ty);
1583 }
1584
1585 ty::ConstKind::Bound(debruijn, bound_var) => {
1586 rustc_type_ir::debug_bound_var(self, debruijn, bound_var)?
1587 }
1588 ty::ConstKind::Placeholder(placeholder) => p!(write("{placeholder:?}")),
1589 ty::ConstKind::Expr(expr) => self.pretty_print_const_expr(expr, print_ty)?,
1592 ty::ConstKind::Error(_) => p!("{{const error}}"),
1593 };
1594 Ok(())
1595 }
1596
1597 fn pretty_print_const_expr(
1598 &mut self,
1599 expr: Expr<'tcx>,
1600 print_ty: bool,
1601 ) -> Result<(), PrintError> {
1602 define_scoped_cx!(self);
1603 match expr.kind {
1604 ty::ExprKind::Binop(op) => {
1605 let (_, _, c1, c2) = expr.binop_args();
1606
1607 let precedence = |binop: crate::mir::BinOp| binop.to_hir_binop().precedence();
1608 let op_precedence = precedence(op);
1609 let formatted_op = op.to_hir_binop().as_str();
1610 let (lhs_parenthesized, rhs_parenthesized) = match (c1.kind(), c2.kind()) {
1611 (
1612 ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(lhs_op), .. }),
1613 ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(rhs_op), .. }),
1614 ) => (precedence(lhs_op) < op_precedence, precedence(rhs_op) < op_precedence),
1615 (
1616 ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(lhs_op), .. }),
1617 ty::ConstKind::Expr(_),
1618 ) => (precedence(lhs_op) < op_precedence, true),
1619 (
1620 ty::ConstKind::Expr(_),
1621 ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(rhs_op), .. }),
1622 ) => (true, precedence(rhs_op) < op_precedence),
1623 (ty::ConstKind::Expr(_), ty::ConstKind::Expr(_)) => (true, true),
1624 (
1625 ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(lhs_op), .. }),
1626 _,
1627 ) => (precedence(lhs_op) < op_precedence, false),
1628 (
1629 _,
1630 ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::Binop(rhs_op), .. }),
1631 ) => (false, precedence(rhs_op) < op_precedence),
1632 (ty::ConstKind::Expr(_), _) => (true, false),
1633 (_, ty::ConstKind::Expr(_)) => (false, true),
1634 _ => (false, false),
1635 };
1636
1637 self.maybe_parenthesized(
1638 |this| this.pretty_print_const(c1, print_ty),
1639 lhs_parenthesized,
1640 )?;
1641 p!(write(" {formatted_op} "));
1642 self.maybe_parenthesized(
1643 |this| this.pretty_print_const(c2, print_ty),
1644 rhs_parenthesized,
1645 )?;
1646 }
1647 ty::ExprKind::UnOp(op) => {
1648 let (_, ct) = expr.unop_args();
1649
1650 use crate::mir::UnOp;
1651 let formatted_op = match op {
1652 UnOp::Not => "!",
1653 UnOp::Neg => "-",
1654 UnOp::PtrMetadata => "PtrMetadata",
1655 };
1656 let parenthesized = match ct.kind() {
1657 _ if op == UnOp::PtrMetadata => true,
1658 ty::ConstKind::Expr(ty::Expr { kind: ty::ExprKind::UnOp(c_op), .. }) => {
1659 c_op != op
1660 }
1661 ty::ConstKind::Expr(_) => true,
1662 _ => false,
1663 };
1664 p!(write("{formatted_op}"));
1665 self.maybe_parenthesized(
1666 |this| this.pretty_print_const(ct, print_ty),
1667 parenthesized,
1668 )?
1669 }
1670 ty::ExprKind::FunctionCall => {
1671 let (_, fn_def, fn_args) = expr.call_args();
1672
1673 write!(self, "(")?;
1674 self.pretty_print_const(fn_def, print_ty)?;
1675 p!(")(", comma_sep(fn_args), ")");
1676 }
1677 ty::ExprKind::Cast(kind) => {
1678 let (_, value, to_ty) = expr.cast_args();
1679
1680 use ty::abstract_const::CastKind;
1681 if kind == CastKind::As || (kind == CastKind::Use && self.should_print_verbose()) {
1682 let parenthesized = match value.kind() {
1683 ty::ConstKind::Expr(ty::Expr {
1684 kind: ty::ExprKind::Cast { .. }, ..
1685 }) => false,
1686 ty::ConstKind::Expr(_) => true,
1687 _ => false,
1688 };
1689 self.maybe_parenthesized(
1690 |this| {
1691 this.typed_value(
1692 |this| this.pretty_print_const(value, print_ty),
1693 |this| this.pretty_print_type(to_ty),
1694 " as ",
1695 )
1696 },
1697 parenthesized,
1698 )?;
1699 } else {
1700 self.pretty_print_const(value, print_ty)?
1701 }
1702 }
1703 }
1704 Ok(())
1705 }
1706
1707 fn pretty_print_const_scalar(
1708 &mut self,
1709 scalar: Scalar,
1710 ty: Ty<'tcx>,
1711 ) -> Result<(), PrintError> {
1712 match scalar {
1713 Scalar::Ptr(ptr, _size) => self.pretty_print_const_scalar_ptr(ptr, ty),
1714 Scalar::Int(int) => {
1715 self.pretty_print_const_scalar_int(int, ty, true)
1716 }
1717 }
1718 }
1719
1720 fn pretty_print_const_scalar_ptr(
1721 &mut self,
1722 ptr: Pointer,
1723 ty: Ty<'tcx>,
1724 ) -> Result<(), PrintError> {
1725 define_scoped_cx!(self);
1726
1727 let (prov, offset) = ptr.into_parts();
1728 match ty.kind() {
1729 ty::Ref(_, inner, _) => {
1731 if let ty::Array(elem, ct_len) = inner.kind()
1732 && let ty::Uint(ty::UintTy::U8) = elem.kind()
1733 && let Some(len) = ct_len.try_to_target_usize(self.tcx())
1734 {
1735 match self.tcx().try_get_global_alloc(prov.alloc_id()) {
1736 Some(GlobalAlloc::Memory(alloc)) => {
1737 let range = AllocRange { start: offset, size: Size::from_bytes(len) };
1738 if let Ok(byte_str) =
1739 alloc.inner().get_bytes_strip_provenance(&self.tcx(), range)
1740 {
1741 p!(pretty_print_byte_str(byte_str))
1742 } else {
1743 p!("<too short allocation>")
1744 }
1745 }
1746 Some(GlobalAlloc::Static(def_id)) => {
1748 p!(write("<static({:?})>", def_id))
1749 }
1750 Some(GlobalAlloc::Function { .. }) => p!("<function>"),
1751 Some(GlobalAlloc::VTable(..)) => p!("<vtable>"),
1752 None => p!("<dangling pointer>"),
1753 }
1754 return Ok(());
1755 }
1756 }
1757 ty::FnPtr(..) => {
1758 if let Some(GlobalAlloc::Function { instance, .. }) =
1761 self.tcx().try_get_global_alloc(prov.alloc_id())
1762 {
1763 self.typed_value(
1764 |this| this.print_value_path(instance.def_id(), instance.args),
1765 |this| this.print_type(ty),
1766 " as ",
1767 )?;
1768 return Ok(());
1769 }
1770 }
1771 _ => {}
1772 }
1773 self.pretty_print_const_pointer(ptr, ty)?;
1775 Ok(())
1776 }
1777
1778 fn pretty_print_const_scalar_int(
1779 &mut self,
1780 int: ScalarInt,
1781 ty: Ty<'tcx>,
1782 print_ty: bool,
1783 ) -> Result<(), PrintError> {
1784 define_scoped_cx!(self);
1785
1786 match ty.kind() {
1787 ty::Bool if int == ScalarInt::FALSE => p!("false"),
1789 ty::Bool if int == ScalarInt::TRUE => p!("true"),
1790 ty::Float(fty) => match fty {
1792 ty::FloatTy::F16 => {
1793 let val = Half::try_from(int).unwrap();
1794 p!(write("{}{}f16", val, if val.is_finite() { "" } else { "_" }))
1795 }
1796 ty::FloatTy::F32 => {
1797 let val = Single::try_from(int).unwrap();
1798 p!(write("{}{}f32", val, if val.is_finite() { "" } else { "_" }))
1799 }
1800 ty::FloatTy::F64 => {
1801 let val = Double::try_from(int).unwrap();
1802 p!(write("{}{}f64", val, if val.is_finite() { "" } else { "_" }))
1803 }
1804 ty::FloatTy::F128 => {
1805 let val = Quad::try_from(int).unwrap();
1806 p!(write("{}{}f128", val, if val.is_finite() { "" } else { "_" }))
1807 }
1808 },
1809 ty::Uint(_) | ty::Int(_) => {
1811 let int =
1812 ConstInt::new(int, matches!(ty.kind(), ty::Int(_)), ty.is_ptr_sized_integral());
1813 if print_ty { p!(write("{:#?}", int)) } else { p!(write("{:?}", int)) }
1814 }
1815 ty::Char if char::try_from(int).is_ok() => {
1817 p!(write("{:?}", char::try_from(int).unwrap()))
1818 }
1819 ty::Ref(..) | ty::RawPtr(_, _) | ty::FnPtr(..) => {
1821 let data = int.to_bits(self.tcx().data_layout.pointer_size);
1822 self.typed_value(
1823 |this| {
1824 write!(this, "0x{data:x}")?;
1825 Ok(())
1826 },
1827 |this| this.print_type(ty),
1828 " as ",
1829 )?;
1830 }
1831 ty::Pat(base_ty, pat) if self.tcx().validate_scalar_in_layout(int, ty) => {
1832 self.pretty_print_const_scalar_int(int, *base_ty, print_ty)?;
1833 p!(write(" is {pat:?}"));
1834 }
1835 _ => {
1837 let print = |this: &mut Self| {
1838 if int.size() == Size::ZERO {
1839 write!(this, "transmute(())")?;
1840 } else {
1841 write!(this, "transmute(0x{int:x})")?;
1842 }
1843 Ok(())
1844 };
1845 if print_ty {
1846 self.typed_value(print, |this| this.print_type(ty), ": ")?
1847 } else {
1848 print(self)?
1849 };
1850 }
1851 }
1852 Ok(())
1853 }
1854
1855 fn pretty_print_const_pointer<Prov: Provenance>(
1858 &mut self,
1859 _: Pointer<Prov>,
1860 ty: Ty<'tcx>,
1861 ) -> Result<(), PrintError> {
1862 self.typed_value(
1863 |this| {
1864 this.write_str("&_")?;
1865 Ok(())
1866 },
1867 |this| this.print_type(ty),
1868 ": ",
1869 )
1870 }
1871
1872 fn pretty_print_byte_str(&mut self, byte_str: &'tcx [u8]) -> Result<(), PrintError> {
1873 write!(self, "b\"{}\"", byte_str.escape_ascii())?;
1874 Ok(())
1875 }
1876
1877 fn pretty_print_const_valtree(
1878 &mut self,
1879 cv: ty::Value<'tcx>,
1880 print_ty: bool,
1881 ) -> Result<(), PrintError> {
1882 define_scoped_cx!(self);
1883
1884 if self.should_print_verbose() {
1885 p!(write("ValTree({:?}: ", cv.valtree), print(cv.ty), ")");
1886 return Ok(());
1887 }
1888
1889 let u8_type = self.tcx().types.u8;
1890 match (*cv.valtree, *cv.ty.kind()) {
1891 (ty::ValTreeKind::Branch(_), ty::Ref(_, inner_ty, _)) => match inner_ty.kind() {
1892 ty::Slice(t) if *t == u8_type => {
1893 let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
1894 bug!(
1895 "expected to convert valtree {:?} to raw bytes for type {:?}",
1896 cv.valtree,
1897 t
1898 )
1899 });
1900 return self.pretty_print_byte_str(bytes);
1901 }
1902 ty::Str => {
1903 let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
1904 bug!("expected to convert valtree to raw bytes for type {:?}", cv.ty)
1905 });
1906 p!(write("{:?}", String::from_utf8_lossy(bytes)));
1907 return Ok(());
1908 }
1909 _ => {
1910 let cv = ty::Value { valtree: cv.valtree, ty: inner_ty };
1911 p!("&");
1912 p!(pretty_print_const_valtree(cv, print_ty));
1913 return Ok(());
1914 }
1915 },
1916 (ty::ValTreeKind::Branch(_), ty::Array(t, _)) if t == u8_type => {
1917 let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
1918 bug!("expected to convert valtree to raw bytes for type {:?}", t)
1919 });
1920 p!("*");
1921 p!(pretty_print_byte_str(bytes));
1922 return Ok(());
1923 }
1924 (ty::ValTreeKind::Branch(_), ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) => {
1926 let contents = self.tcx().destructure_const(ty::Const::new_value(
1927 self.tcx(),
1928 cv.valtree,
1929 cv.ty,
1930 ));
1931 let fields = contents.fields.iter().copied();
1932 match *cv.ty.kind() {
1933 ty::Array(..) => {
1934 p!("[", comma_sep(fields), "]");
1935 }
1936 ty::Tuple(..) => {
1937 p!("(", comma_sep(fields));
1938 if contents.fields.len() == 1 {
1939 p!(",");
1940 }
1941 p!(")");
1942 }
1943 ty::Adt(def, _) if def.variants().is_empty() => {
1944 self.typed_value(
1945 |this| {
1946 write!(this, "unreachable()")?;
1947 Ok(())
1948 },
1949 |this| this.print_type(cv.ty),
1950 ": ",
1951 )?;
1952 }
1953 ty::Adt(def, args) => {
1954 let variant_idx =
1955 contents.variant.expect("destructed const of adt without variant idx");
1956 let variant_def = &def.variant(variant_idx);
1957 p!(print_value_path(variant_def.def_id, args));
1958 match variant_def.ctor_kind() {
1959 Some(CtorKind::Const) => {}
1960 Some(CtorKind::Fn) => {
1961 p!("(", comma_sep(fields), ")");
1962 }
1963 None => {
1964 p!(" {{ ");
1965 let mut first = true;
1966 for (field_def, field) in iter::zip(&variant_def.fields, fields) {
1967 if !first {
1968 p!(", ");
1969 }
1970 p!(write("{}: ", field_def.name), print(field));
1971 first = false;
1972 }
1973 p!(" }}");
1974 }
1975 }
1976 }
1977 _ => unreachable!(),
1978 }
1979 return Ok(());
1980 }
1981 (ty::ValTreeKind::Leaf(leaf), ty::Ref(_, inner_ty, _)) => {
1982 p!(write("&"));
1983 return self.pretty_print_const_scalar_int(*leaf, inner_ty, print_ty);
1984 }
1985 (ty::ValTreeKind::Leaf(leaf), _) => {
1986 return self.pretty_print_const_scalar_int(*leaf, cv.ty, print_ty);
1987 }
1988 (_, ty::FnDef(def_id, args)) => {
1989 p!(print_value_path(def_id, args));
1991 return Ok(());
1992 }
1993 _ => {}
1996 }
1997
1998 if cv.valtree.is_zst() {
2000 p!(write("<ZST>"));
2001 } else {
2002 p!(write("{:?}", cv.valtree));
2003 }
2004 if print_ty {
2005 p!(": ", print(cv.ty));
2006 }
2007 Ok(())
2008 }
2009
2010 fn pretty_closure_as_impl(
2011 &mut self,
2012 closure: ty::ClosureArgs<TyCtxt<'tcx>>,
2013 ) -> Result<(), PrintError> {
2014 let sig = closure.sig();
2015 let kind = closure.kind_ty().to_opt_closure_kind().unwrap_or(ty::ClosureKind::Fn);
2016
2017 write!(self, "impl ")?;
2018 self.wrap_binder(&sig, WrapBinderMode::ForAll, |sig, cx| {
2019 define_scoped_cx!(cx);
2020
2021 p!(write("{kind}("));
2022 for (i, arg) in sig.inputs()[0].tuple_fields().iter().enumerate() {
2023 if i > 0 {
2024 p!(", ");
2025 }
2026 p!(print(arg));
2027 }
2028 p!(")");
2029
2030 if !sig.output().is_unit() {
2031 p!(" -> ", print(sig.output()));
2032 }
2033
2034 Ok(())
2035 })
2036 }
2037
2038 fn pretty_print_bound_constness(
2039 &mut self,
2040 constness: ty::BoundConstness,
2041 ) -> Result<(), PrintError> {
2042 define_scoped_cx!(self);
2043
2044 match constness {
2045 ty::BoundConstness::Const => {
2046 p!("const ");
2047 }
2048 ty::BoundConstness::Maybe => {
2049 p!("~const ");
2050 }
2051 }
2052 Ok(())
2053 }
2054
2055 fn should_print_verbose(&self) -> bool {
2056 self.tcx().sess.verbose_internals()
2057 }
2058}
2059
2060pub(crate) fn pretty_print_const<'tcx>(
2061 c: ty::Const<'tcx>,
2062 fmt: &mut fmt::Formatter<'_>,
2063 print_types: bool,
2064) -> fmt::Result {
2065 ty::tls::with(|tcx| {
2066 let literal = tcx.lift(c).unwrap();
2067 let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS);
2068 cx.print_alloc_ids = true;
2069 cx.pretty_print_const(literal, print_types)?;
2070 fmt.write_str(&cx.into_buffer())?;
2071 Ok(())
2072 })
2073}
2074
2075pub struct FmtPrinter<'a, 'tcx>(Box<FmtPrinterData<'a, 'tcx>>);
2077
2078pub struct FmtPrinterData<'a, 'tcx> {
2079 tcx: TyCtxt<'tcx>,
2080 fmt: String,
2081
2082 empty_path: bool,
2083 in_value: bool,
2084 pub print_alloc_ids: bool,
2085
2086 used_region_names: FxHashSet<Symbol>,
2088
2089 region_index: usize,
2090 binder_depth: usize,
2091 printed_type_count: usize,
2092 type_length_limit: Limit,
2093
2094 pub region_highlight_mode: RegionHighlightMode<'tcx>,
2095
2096 pub ty_infer_name_resolver: Option<Box<dyn Fn(ty::TyVid) -> Option<Symbol> + 'a>>,
2097 pub const_infer_name_resolver: Option<Box<dyn Fn(ty::ConstVid) -> Option<Symbol> + 'a>>,
2098}
2099
2100impl<'a, 'tcx> Deref for FmtPrinter<'a, 'tcx> {
2101 type Target = FmtPrinterData<'a, 'tcx>;
2102 fn deref(&self) -> &Self::Target {
2103 &self.0
2104 }
2105}
2106
2107impl DerefMut for FmtPrinter<'_, '_> {
2108 fn deref_mut(&mut self) -> &mut Self::Target {
2109 &mut self.0
2110 }
2111}
2112
2113impl<'a, 'tcx> FmtPrinter<'a, 'tcx> {
2114 pub fn new(tcx: TyCtxt<'tcx>, ns: Namespace) -> Self {
2115 let limit =
2116 if with_reduced_queries() { Limit::new(1048576) } else { tcx.type_length_limit() };
2117 Self::new_with_limit(tcx, ns, limit)
2118 }
2119
2120 pub fn print_string(
2121 tcx: TyCtxt<'tcx>,
2122 ns: Namespace,
2123 f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
2124 ) -> Result<String, PrintError> {
2125 let mut c = FmtPrinter::new(tcx, ns);
2126 f(&mut c)?;
2127 Ok(c.into_buffer())
2128 }
2129
2130 pub fn new_with_limit(tcx: TyCtxt<'tcx>, ns: Namespace, type_length_limit: Limit) -> Self {
2131 FmtPrinter(Box::new(FmtPrinterData {
2132 tcx,
2133 fmt: String::with_capacity(64),
2136 empty_path: false,
2137 in_value: ns == Namespace::ValueNS,
2138 print_alloc_ids: false,
2139 used_region_names: Default::default(),
2140 region_index: 0,
2141 binder_depth: 0,
2142 printed_type_count: 0,
2143 type_length_limit,
2144 region_highlight_mode: RegionHighlightMode::default(),
2145 ty_infer_name_resolver: None,
2146 const_infer_name_resolver: None,
2147 }))
2148 }
2149
2150 pub fn into_buffer(self) -> String {
2151 self.0.fmt
2152 }
2153}
2154
2155fn guess_def_namespace(tcx: TyCtxt<'_>, def_id: DefId) -> Namespace {
2158 match tcx.def_key(def_id).disambiguated_data.data {
2159 DefPathData::TypeNs(..) | DefPathData::CrateRoot | DefPathData::OpaqueTy => {
2160 Namespace::TypeNS
2161 }
2162
2163 DefPathData::ValueNs(..)
2164 | DefPathData::AnonConst
2165 | DefPathData::Closure
2166 | DefPathData::Ctor => Namespace::ValueNS,
2167
2168 DefPathData::MacroNs(..) => Namespace::MacroNS,
2169
2170 _ => Namespace::TypeNS,
2171 }
2172}
2173
2174impl<'t> TyCtxt<'t> {
2175 pub fn def_path_str(self, def_id: impl IntoQueryParam<DefId>) -> String {
2178 self.def_path_str_with_args(def_id, &[])
2179 }
2180
2181 pub fn def_path_str_with_args(
2182 self,
2183 def_id: impl IntoQueryParam<DefId>,
2184 args: &'t [GenericArg<'t>],
2185 ) -> String {
2186 let def_id = def_id.into_query_param();
2187 let ns = guess_def_namespace(self, def_id);
2188 debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
2189
2190 FmtPrinter::print_string(self, ns, |cx| cx.print_def_path(def_id, args)).unwrap()
2191 }
2192
2193 pub fn value_path_str_with_args(
2194 self,
2195 def_id: impl IntoQueryParam<DefId>,
2196 args: &'t [GenericArg<'t>],
2197 ) -> String {
2198 let def_id = def_id.into_query_param();
2199 let ns = guess_def_namespace(self, def_id);
2200 debug!("value_path_str: def_id={:?}, ns={:?}", def_id, ns);
2201
2202 FmtPrinter::print_string(self, ns, |cx| cx.print_value_path(def_id, args)).unwrap()
2203 }
2204}
2205
2206impl fmt::Write for FmtPrinter<'_, '_> {
2207 fn write_str(&mut self, s: &str) -> fmt::Result {
2208 self.fmt.push_str(s);
2209 Ok(())
2210 }
2211}
2212
2213impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
2214 fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
2215 self.tcx
2216 }
2217
2218 fn print_def_path(
2219 &mut self,
2220 def_id: DefId,
2221 args: &'tcx [GenericArg<'tcx>],
2222 ) -> Result<(), PrintError> {
2223 if args.is_empty() {
2224 match self.try_print_trimmed_def_path(def_id)? {
2225 true => return Ok(()),
2226 false => {}
2227 }
2228
2229 match self.try_print_visible_def_path(def_id)? {
2230 true => return Ok(()),
2231 false => {}
2232 }
2233 }
2234
2235 let key = self.tcx.def_key(def_id);
2236 if let DefPathData::Impl = key.disambiguated_data.data {
2237 let use_types = !def_id.is_local() || {
2240 let force_no_types = with_forced_impl_filename_line();
2242 !force_no_types
2243 };
2244
2245 if !use_types {
2246 let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id };
2250 let span = self.tcx.def_span(def_id);
2251
2252 self.print_def_path(parent_def_id, &[])?;
2253
2254 if !self.empty_path {
2257 write!(self, "::")?;
2258 }
2259 write!(
2260 self,
2261 "<impl at {}>",
2262 self.tcx.sess.source_map().span_to_embeddable_string(span)
2265 )?;
2266 self.empty_path = false;
2267
2268 return Ok(());
2269 }
2270 }
2271
2272 self.default_print_def_path(def_id, args)
2273 }
2274
2275 fn print_region(&mut self, region: ty::Region<'tcx>) -> Result<(), PrintError> {
2276 self.pretty_print_region(region)
2277 }
2278
2279 fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
2280 match ty.kind() {
2281 ty::Tuple(tys) if tys.len() == 0 && self.should_truncate() => {
2282 self.printed_type_count += 1;
2284 self.pretty_print_type(ty)
2285 }
2286 ty::Adt(..)
2287 | ty::Foreign(_)
2288 | ty::Pat(..)
2289 | ty::RawPtr(..)
2290 | ty::Ref(..)
2291 | ty::FnDef(..)
2292 | ty::FnPtr(..)
2293 | ty::UnsafeBinder(..)
2294 | ty::Dynamic(..)
2295 | ty::Closure(..)
2296 | ty::CoroutineClosure(..)
2297 | ty::Coroutine(..)
2298 | ty::CoroutineWitness(..)
2299 | ty::Tuple(_)
2300 | ty::Alias(..)
2301 | ty::Param(_)
2302 | ty::Bound(..)
2303 | ty::Placeholder(_)
2304 | ty::Error(_)
2305 if self.should_truncate() =>
2306 {
2307 write!(self, "...")?;
2310 Ok(())
2311 }
2312 _ => {
2313 self.printed_type_count += 1;
2314 self.pretty_print_type(ty)
2315 }
2316 }
2317 }
2318
2319 fn should_truncate(&mut self) -> bool {
2320 !self.type_length_limit.value_within_limit(self.printed_type_count)
2321 }
2322
2323 fn print_dyn_existential(
2324 &mut self,
2325 predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
2326 ) -> Result<(), PrintError> {
2327 self.pretty_print_dyn_existential(predicates)
2328 }
2329
2330 fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
2331 self.pretty_print_const(ct, false)
2332 }
2333
2334 fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
2335 self.empty_path = true;
2336 if cnum == LOCAL_CRATE {
2337 if self.tcx.sess.at_least_rust_2018() {
2338 if with_crate_prefix() {
2340 write!(self, "{}", kw::Crate)?;
2341 self.empty_path = false;
2342 }
2343 }
2344 } else {
2345 write!(self, "{}", self.tcx.crate_name(cnum))?;
2346 self.empty_path = false;
2347 }
2348 Ok(())
2349 }
2350
2351 fn path_qualified(
2352 &mut self,
2353 self_ty: Ty<'tcx>,
2354 trait_ref: Option<ty::TraitRef<'tcx>>,
2355 ) -> Result<(), PrintError> {
2356 self.pretty_path_qualified(self_ty, trait_ref)?;
2357 self.empty_path = false;
2358 Ok(())
2359 }
2360
2361 fn path_append_impl(
2362 &mut self,
2363 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
2364 _disambiguated_data: &DisambiguatedDefPathData,
2365 self_ty: Ty<'tcx>,
2366 trait_ref: Option<ty::TraitRef<'tcx>>,
2367 ) -> Result<(), PrintError> {
2368 self.pretty_path_append_impl(
2369 |cx| {
2370 print_prefix(cx)?;
2371 if !cx.empty_path {
2372 write!(cx, "::")?;
2373 }
2374
2375 Ok(())
2376 },
2377 self_ty,
2378 trait_ref,
2379 )?;
2380 self.empty_path = false;
2381 Ok(())
2382 }
2383
2384 fn path_append(
2385 &mut self,
2386 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
2387 disambiguated_data: &DisambiguatedDefPathData,
2388 ) -> Result<(), PrintError> {
2389 print_prefix(self)?;
2390
2391 if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
2393 return Ok(());
2394 }
2395
2396 let name = disambiguated_data.data.name();
2397 if !self.empty_path {
2398 write!(self, "::")?;
2399 }
2400
2401 if let DefPathDataName::Named(name) = name {
2402 if Ident::with_dummy_span(name).is_raw_guess() {
2403 write!(self, "r#")?;
2404 }
2405 }
2406
2407 let verbose = self.should_print_verbose();
2408 disambiguated_data.fmt_maybe_verbose(self, verbose)?;
2409
2410 self.empty_path = false;
2411
2412 Ok(())
2413 }
2414
2415 fn path_generic_args(
2416 &mut self,
2417 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
2418 args: &[GenericArg<'tcx>],
2419 ) -> Result<(), PrintError> {
2420 print_prefix(self)?;
2421
2422 if !args.is_empty() {
2423 if self.in_value {
2424 write!(self, "::")?;
2425 }
2426 self.generic_delimiters(|cx| cx.comma_sep(args.iter().copied()))
2427 } else {
2428 Ok(())
2429 }
2430 }
2431}
2432
2433impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
2434 fn ty_infer_name(&self, id: ty::TyVid) -> Option<Symbol> {
2435 self.0.ty_infer_name_resolver.as_ref().and_then(|func| func(id))
2436 }
2437
2438 fn reset_type_limit(&mut self) {
2439 self.printed_type_count = 0;
2440 }
2441
2442 fn const_infer_name(&self, id: ty::ConstVid) -> Option<Symbol> {
2443 self.0.const_infer_name_resolver.as_ref().and_then(|func| func(id))
2444 }
2445
2446 fn print_value_path(
2447 &mut self,
2448 def_id: DefId,
2449 args: &'tcx [GenericArg<'tcx>],
2450 ) -> Result<(), PrintError> {
2451 let was_in_value = std::mem::replace(&mut self.in_value, true);
2452 self.print_def_path(def_id, args)?;
2453 self.in_value = was_in_value;
2454
2455 Ok(())
2456 }
2457
2458 fn print_in_binder<T>(&mut self, value: &ty::Binder<'tcx, T>) -> Result<(), PrintError>
2459 where
2460 T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
2461 {
2462 self.pretty_print_in_binder(value)
2463 }
2464
2465 fn wrap_binder<T, C: FnOnce(&T, &mut Self) -> Result<(), PrintError>>(
2466 &mut self,
2467 value: &ty::Binder<'tcx, T>,
2468 mode: WrapBinderMode,
2469 f: C,
2470 ) -> Result<(), PrintError>
2471 where
2472 T: TypeFoldable<TyCtxt<'tcx>>,
2473 {
2474 self.pretty_wrap_binder(value, mode, f)
2475 }
2476
2477 fn typed_value(
2478 &mut self,
2479 f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
2480 t: impl FnOnce(&mut Self) -> Result<(), PrintError>,
2481 conversion: &str,
2482 ) -> Result<(), PrintError> {
2483 self.write_str("{")?;
2484 f(self)?;
2485 self.write_str(conversion)?;
2486 let was_in_value = std::mem::replace(&mut self.in_value, false);
2487 t(self)?;
2488 self.in_value = was_in_value;
2489 self.write_str("}")?;
2490 Ok(())
2491 }
2492
2493 fn generic_delimiters(
2494 &mut self,
2495 f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
2496 ) -> Result<(), PrintError> {
2497 write!(self, "<")?;
2498
2499 let was_in_value = std::mem::replace(&mut self.in_value, false);
2500 f(self)?;
2501 self.in_value = was_in_value;
2502
2503 write!(self, ">")?;
2504 Ok(())
2505 }
2506
2507 fn should_print_region(&self, region: ty::Region<'tcx>) -> bool {
2508 let highlight = self.region_highlight_mode;
2509 if highlight.region_highlighted(region).is_some() {
2510 return true;
2511 }
2512
2513 if self.should_print_verbose() {
2514 return true;
2515 }
2516
2517 if with_forced_trimmed_paths() {
2518 return false;
2519 }
2520
2521 let identify_regions = self.tcx.sess.opts.unstable_opts.identify_regions;
2522
2523 match *region {
2524 ty::ReEarlyParam(ref data) => data.has_name(),
2525
2526 ty::ReLateParam(ty::LateParamRegion { kind, .. }) => kind.is_named(),
2527 ty::ReBound(_, ty::BoundRegion { kind: br, .. })
2528 | ty::RePlaceholder(ty::Placeholder {
2529 bound: ty::BoundRegion { kind: br, .. }, ..
2530 }) => {
2531 if br.is_named() {
2532 return true;
2533 }
2534
2535 if let Some((region, _)) = highlight.highlight_bound_region {
2536 if br == region {
2537 return true;
2538 }
2539 }
2540
2541 false
2542 }
2543
2544 ty::ReVar(_) if identify_regions => true,
2545
2546 ty::ReVar(_) | ty::ReErased | ty::ReError(_) => false,
2547
2548 ty::ReStatic => true,
2549 }
2550 }
2551
2552 fn pretty_print_const_pointer<Prov: Provenance>(
2553 &mut self,
2554 p: Pointer<Prov>,
2555 ty: Ty<'tcx>,
2556 ) -> Result<(), PrintError> {
2557 let print = |this: &mut Self| {
2558 define_scoped_cx!(this);
2559 if this.print_alloc_ids {
2560 p!(write("{:?}", p));
2561 } else {
2562 p!("&_");
2563 }
2564 Ok(())
2565 };
2566 self.typed_value(print, |this| this.print_type(ty), ": ")
2567 }
2568}
2569
2570impl<'tcx> FmtPrinter<'_, 'tcx> {
2572 pub fn pretty_print_region(&mut self, region: ty::Region<'tcx>) -> Result<(), fmt::Error> {
2573 define_scoped_cx!(self);
2574
2575 let highlight = self.region_highlight_mode;
2577 if let Some(n) = highlight.region_highlighted(region) {
2578 p!(write("'{}", n));
2579 return Ok(());
2580 }
2581
2582 if self.should_print_verbose() {
2583 p!(write("{:?}", region));
2584 return Ok(());
2585 }
2586
2587 let identify_regions = self.tcx.sess.opts.unstable_opts.identify_regions;
2588
2589 match *region {
2594 ty::ReEarlyParam(data) => {
2595 p!(write("{}", data.name));
2596 return Ok(());
2597 }
2598 ty::ReLateParam(ty::LateParamRegion { kind, .. }) => {
2599 if let Some(name) = kind.get_name() {
2600 p!(write("{}", name));
2601 return Ok(());
2602 }
2603 }
2604 ty::ReBound(_, ty::BoundRegion { kind: br, .. })
2605 | ty::RePlaceholder(ty::Placeholder {
2606 bound: ty::BoundRegion { kind: br, .. }, ..
2607 }) => {
2608 if let ty::BoundRegionKind::Named(_, name) = br
2609 && br.is_named()
2610 {
2611 p!(write("{}", name));
2612 return Ok(());
2613 }
2614
2615 if let Some((region, counter)) = highlight.highlight_bound_region {
2616 if br == region {
2617 p!(write("'{}", counter));
2618 return Ok(());
2619 }
2620 }
2621 }
2622 ty::ReVar(region_vid) if identify_regions => {
2623 p!(write("{:?}", region_vid));
2624 return Ok(());
2625 }
2626 ty::ReVar(_) => {}
2627 ty::ReErased => {}
2628 ty::ReError(_) => {}
2629 ty::ReStatic => {
2630 p!("'static");
2631 return Ok(());
2632 }
2633 }
2634
2635 p!("'_");
2636
2637 Ok(())
2638 }
2639}
2640
2641struct RegionFolder<'a, 'tcx> {
2643 tcx: TyCtxt<'tcx>,
2644 current_index: ty::DebruijnIndex,
2645 region_map: UnordMap<ty::BoundRegion, ty::Region<'tcx>>,
2646 name: &'a mut (
2647 dyn FnMut(
2648 Option<ty::DebruijnIndex>, ty::DebruijnIndex, ty::BoundRegion,
2651 ) -> ty::Region<'tcx>
2652 + 'a
2653 ),
2654}
2655
2656impl<'a, 'tcx> ty::TypeFolder<TyCtxt<'tcx>> for RegionFolder<'a, 'tcx> {
2657 fn cx(&self) -> TyCtxt<'tcx> {
2658 self.tcx
2659 }
2660
2661 fn fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>(
2662 &mut self,
2663 t: ty::Binder<'tcx, T>,
2664 ) -> ty::Binder<'tcx, T> {
2665 self.current_index.shift_in(1);
2666 let t = t.super_fold_with(self);
2667 self.current_index.shift_out(1);
2668 t
2669 }
2670
2671 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
2672 match *t.kind() {
2673 _ if t.has_vars_bound_at_or_above(self.current_index) || t.has_placeholders() => {
2674 return t.super_fold_with(self);
2675 }
2676 _ => {}
2677 }
2678 t
2679 }
2680
2681 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
2682 let name = &mut self.name;
2683 let region = match *r {
2684 ty::ReBound(db, br) if db >= self.current_index => {
2685 *self.region_map.entry(br).or_insert_with(|| name(Some(db), self.current_index, br))
2686 }
2687 ty::RePlaceholder(ty::PlaceholderRegion {
2688 bound: ty::BoundRegion { kind, .. },
2689 ..
2690 }) => {
2691 match kind {
2694 ty::BoundRegionKind::Anon | ty::BoundRegionKind::ClosureEnv => r,
2695 _ => {
2696 let br = ty::BoundRegion { var: ty::BoundVar::ZERO, kind };
2698 *self
2699 .region_map
2700 .entry(br)
2701 .or_insert_with(|| name(None, self.current_index, br))
2702 }
2703 }
2704 }
2705 _ => return r,
2706 };
2707 if let ty::ReBound(debruijn1, br) = *region {
2708 assert_eq!(debruijn1, ty::INNERMOST);
2709 ty::Region::new_bound(self.tcx, self.current_index, br)
2710 } else {
2711 region
2712 }
2713 }
2714}
2715
2716impl<'tcx> FmtPrinter<'_, 'tcx> {
2719 pub fn name_all_regions<T>(
2720 &mut self,
2721 value: &ty::Binder<'tcx, T>,
2722 mode: WrapBinderMode,
2723 ) -> Result<(T, UnordMap<ty::BoundRegion, ty::Region<'tcx>>), fmt::Error>
2724 where
2725 T: TypeFoldable<TyCtxt<'tcx>>,
2726 {
2727 fn name_by_region_index(
2728 index: usize,
2729 available_names: &mut Vec<Symbol>,
2730 num_available: usize,
2731 ) -> Symbol {
2732 if let Some(name) = available_names.pop() {
2733 name
2734 } else {
2735 Symbol::intern(&format!("'z{}", index - num_available))
2736 }
2737 }
2738
2739 debug!("name_all_regions");
2740
2741 if self.binder_depth == 0 {
2747 self.prepare_region_info(value);
2748 }
2749
2750 debug!("self.used_region_names: {:?}", self.used_region_names);
2751
2752 let mut empty = true;
2753 let mut start_or_continue = |cx: &mut Self, start: &str, cont: &str| {
2754 let w = if empty {
2755 empty = false;
2756 start
2757 } else {
2758 cont
2759 };
2760 let _ = write!(cx, "{w}");
2761 };
2762 let do_continue = |cx: &mut Self, cont: Symbol| {
2763 let _ = write!(cx, "{cont}");
2764 };
2765
2766 let possible_names = ('a'..='z').rev().map(|s| Symbol::intern(&format!("'{s}")));
2767
2768 let mut available_names = possible_names
2769 .filter(|name| !self.used_region_names.contains(name))
2770 .collect::<Vec<_>>();
2771 debug!(?available_names);
2772 let num_available = available_names.len();
2773
2774 let mut region_index = self.region_index;
2775 let mut next_name = |this: &Self| {
2776 let mut name;
2777
2778 loop {
2779 name = name_by_region_index(region_index, &mut available_names, num_available);
2780 region_index += 1;
2781
2782 if !this.used_region_names.contains(&name) {
2783 break;
2784 }
2785 }
2786
2787 name
2788 };
2789
2790 let (new_value, map) = if self.should_print_verbose() {
2795 for var in value.bound_vars().iter() {
2796 start_or_continue(self, mode.start_str(), ", ");
2797 write!(self, "{var:?}")?;
2798 }
2799 if value.bound_vars().is_empty() && mode == WrapBinderMode::Unsafe {
2801 start_or_continue(self, mode.start_str(), "");
2802 }
2803 start_or_continue(self, "", "> ");
2804 (value.clone().skip_binder(), UnordMap::default())
2805 } else {
2806 let tcx = self.tcx;
2807
2808 let trim_path = with_forced_trimmed_paths();
2809 let mut name = |lifetime_idx: Option<ty::DebruijnIndex>,
2815 binder_level_idx: ty::DebruijnIndex,
2816 br: ty::BoundRegion| {
2817 let (name, kind) = match br.kind {
2818 ty::BoundRegionKind::Anon | ty::BoundRegionKind::ClosureEnv => {
2819 let name = next_name(self);
2820
2821 if let Some(lt_idx) = lifetime_idx {
2822 if lt_idx > binder_level_idx {
2823 let kind =
2824 ty::BoundRegionKind::Named(CRATE_DEF_ID.to_def_id(), name);
2825 return ty::Region::new_bound(
2826 tcx,
2827 ty::INNERMOST,
2828 ty::BoundRegion { var: br.var, kind },
2829 );
2830 }
2831 }
2832
2833 (name, ty::BoundRegionKind::Named(CRATE_DEF_ID.to_def_id(), name))
2834 }
2835 ty::BoundRegionKind::Named(def_id, kw::UnderscoreLifetime) => {
2836 let name = next_name(self);
2837
2838 if let Some(lt_idx) = lifetime_idx {
2839 if lt_idx > binder_level_idx {
2840 let kind = ty::BoundRegionKind::Named(def_id, name);
2841 return ty::Region::new_bound(
2842 tcx,
2843 ty::INNERMOST,
2844 ty::BoundRegion { var: br.var, kind },
2845 );
2846 }
2847 }
2848
2849 (name, ty::BoundRegionKind::Named(def_id, name))
2850 }
2851 ty::BoundRegionKind::Named(_, name) => {
2852 if let Some(lt_idx) = lifetime_idx {
2853 if lt_idx > binder_level_idx {
2854 let kind = br.kind;
2855 return ty::Region::new_bound(
2856 tcx,
2857 ty::INNERMOST,
2858 ty::BoundRegion { var: br.var, kind },
2859 );
2860 }
2861 }
2862
2863 (name, br.kind)
2864 }
2865 };
2866
2867 if !trim_path || mode == WrapBinderMode::Unsafe {
2869 start_or_continue(self, mode.start_str(), ", ");
2870 do_continue(self, name);
2871 }
2872 ty::Region::new_bound(tcx, ty::INNERMOST, ty::BoundRegion { var: br.var, kind })
2873 };
2874 let mut folder = RegionFolder {
2875 tcx,
2876 current_index: ty::INNERMOST,
2877 name: &mut name,
2878 region_map: UnordMap::default(),
2879 };
2880 let new_value = value.clone().skip_binder().fold_with(&mut folder);
2881 let region_map = folder.region_map;
2882
2883 if mode == WrapBinderMode::Unsafe && region_map.is_empty() {
2884 start_or_continue(self, mode.start_str(), "");
2885 }
2886 start_or_continue(self, "", "> ");
2887
2888 (new_value, region_map)
2889 };
2890
2891 self.binder_depth += 1;
2892 self.region_index = region_index;
2893 Ok((new_value, map))
2894 }
2895
2896 pub fn pretty_print_in_binder<T>(
2897 &mut self,
2898 value: &ty::Binder<'tcx, T>,
2899 ) -> Result<(), fmt::Error>
2900 where
2901 T: Print<'tcx, Self> + TypeFoldable<TyCtxt<'tcx>>,
2902 {
2903 let old_region_index = self.region_index;
2904 let (new_value, _) = self.name_all_regions(value, WrapBinderMode::ForAll)?;
2905 new_value.print(self)?;
2906 self.region_index = old_region_index;
2907 self.binder_depth -= 1;
2908 Ok(())
2909 }
2910
2911 pub fn pretty_wrap_binder<T, C: FnOnce(&T, &mut Self) -> Result<(), fmt::Error>>(
2912 &mut self,
2913 value: &ty::Binder<'tcx, T>,
2914 mode: WrapBinderMode,
2915 f: C,
2916 ) -> Result<(), fmt::Error>
2917 where
2918 T: TypeFoldable<TyCtxt<'tcx>>,
2919 {
2920 let old_region_index = self.region_index;
2921 let (new_value, _) = self.name_all_regions(value, mode)?;
2922 f(&new_value, self)?;
2923 self.region_index = old_region_index;
2924 self.binder_depth -= 1;
2925 Ok(())
2926 }
2927
2928 fn prepare_region_info<T>(&mut self, value: &ty::Binder<'tcx, T>)
2929 where
2930 T: TypeVisitable<TyCtxt<'tcx>>,
2931 {
2932 struct RegionNameCollector<'tcx> {
2933 used_region_names: FxHashSet<Symbol>,
2934 type_collector: SsoHashSet<Ty<'tcx>>,
2935 }
2936
2937 impl<'tcx> RegionNameCollector<'tcx> {
2938 fn new() -> Self {
2939 RegionNameCollector {
2940 used_region_names: Default::default(),
2941 type_collector: SsoHashSet::new(),
2942 }
2943 }
2944 }
2945
2946 impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for RegionNameCollector<'tcx> {
2947 fn visit_region(&mut self, r: ty::Region<'tcx>) {
2948 trace!("address: {:p}", r.0.0);
2949
2950 if let Some(name) = r.get_name() {
2954 self.used_region_names.insert(name);
2955 }
2956 }
2957
2958 fn visit_ty(&mut self, ty: Ty<'tcx>) {
2961 let not_previously_inserted = self.type_collector.insert(ty);
2962 if not_previously_inserted {
2963 ty.super_visit_with(self)
2964 }
2965 }
2966 }
2967
2968 let mut collector = RegionNameCollector::new();
2969 value.visit_with(&mut collector);
2970 self.used_region_names = collector.used_region_names;
2971 self.region_index = 0;
2972 }
2973}
2974
2975impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::Binder<'tcx, T>
2976where
2977 T: Print<'tcx, P> + TypeFoldable<TyCtxt<'tcx>>,
2978{
2979 fn print(&self, cx: &mut P) -> Result<(), PrintError> {
2980 cx.print_in_binder(self)
2981 }
2982}
2983
2984impl<'tcx, T, P: PrettyPrinter<'tcx>> Print<'tcx, P> for ty::OutlivesPredicate<'tcx, T>
2985where
2986 T: Print<'tcx, P>,
2987{
2988 fn print(&self, cx: &mut P) -> Result<(), PrintError> {
2989 define_scoped_cx!(cx);
2990 p!(print(self.0), ": ", print(self.1));
2991 Ok(())
2992 }
2993}
2994
2995#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift, Hash)]
2999pub struct TraitRefPrintOnlyTraitPath<'tcx>(ty::TraitRef<'tcx>);
3000
3001impl<'tcx> rustc_errors::IntoDiagArg for TraitRefPrintOnlyTraitPath<'tcx> {
3002 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
3003 ty::tls::with(|tcx| {
3004 let trait_ref = tcx.short_string(self, path);
3005 rustc_errors::DiagArgValue::Str(std::borrow::Cow::Owned(trait_ref))
3006 })
3007 }
3008}
3009
3010impl<'tcx> fmt::Debug for TraitRefPrintOnlyTraitPath<'tcx> {
3011 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3012 fmt::Display::fmt(self, f)
3013 }
3014}
3015
3016#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift, Hash)]
3019pub struct TraitRefPrintSugared<'tcx>(ty::TraitRef<'tcx>);
3020
3021impl<'tcx> rustc_errors::IntoDiagArg for TraitRefPrintSugared<'tcx> {
3022 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
3023 ty::tls::with(|tcx| {
3024 let trait_ref = tcx.short_string(self, path);
3025 rustc_errors::DiagArgValue::Str(std::borrow::Cow::Owned(trait_ref))
3026 })
3027 }
3028}
3029
3030impl<'tcx> fmt::Debug for TraitRefPrintSugared<'tcx> {
3031 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3032 fmt::Display::fmt(self, f)
3033 }
3034}
3035
3036#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
3040pub struct TraitRefPrintOnlyTraitName<'tcx>(ty::TraitRef<'tcx>);
3041
3042impl<'tcx> fmt::Debug for TraitRefPrintOnlyTraitName<'tcx> {
3043 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3044 fmt::Display::fmt(self, f)
3045 }
3046}
3047
3048#[extension(pub trait PrintTraitRefExt<'tcx>)]
3049impl<'tcx> ty::TraitRef<'tcx> {
3050 fn print_only_trait_path(self) -> TraitRefPrintOnlyTraitPath<'tcx> {
3051 TraitRefPrintOnlyTraitPath(self)
3052 }
3053
3054 fn print_trait_sugared(self) -> TraitRefPrintSugared<'tcx> {
3055 TraitRefPrintSugared(self)
3056 }
3057
3058 fn print_only_trait_name(self) -> TraitRefPrintOnlyTraitName<'tcx> {
3059 TraitRefPrintOnlyTraitName(self)
3060 }
3061}
3062
3063#[extension(pub trait PrintPolyTraitRefExt<'tcx>)]
3064impl<'tcx> ty::Binder<'tcx, ty::TraitRef<'tcx>> {
3065 fn print_only_trait_path(self) -> ty::Binder<'tcx, TraitRefPrintOnlyTraitPath<'tcx>> {
3066 self.map_bound(|tr| tr.print_only_trait_path())
3067 }
3068
3069 fn print_trait_sugared(self) -> ty::Binder<'tcx, TraitRefPrintSugared<'tcx>> {
3070 self.map_bound(|tr| tr.print_trait_sugared())
3071 }
3072}
3073
3074#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift)]
3075pub struct TraitPredPrintModifiersAndPath<'tcx>(ty::TraitPredicate<'tcx>);
3076
3077impl<'tcx> fmt::Debug for TraitPredPrintModifiersAndPath<'tcx> {
3078 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3079 fmt::Display::fmt(self, f)
3080 }
3081}
3082
3083#[extension(pub trait PrintTraitPredicateExt<'tcx>)]
3084impl<'tcx> ty::TraitPredicate<'tcx> {
3085 fn print_modifiers_and_trait_path(self) -> TraitPredPrintModifiersAndPath<'tcx> {
3086 TraitPredPrintModifiersAndPath(self)
3087 }
3088}
3089
3090#[derive(Copy, Clone, TypeFoldable, TypeVisitable, Lift, Hash)]
3091pub struct TraitPredPrintWithBoundConstness<'tcx>(
3092 ty::TraitPredicate<'tcx>,
3093 Option<ty::BoundConstness>,
3094);
3095
3096impl<'tcx> fmt::Debug for TraitPredPrintWithBoundConstness<'tcx> {
3097 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3098 fmt::Display::fmt(self, f)
3099 }
3100}
3101
3102#[extension(pub trait PrintPolyTraitPredicateExt<'tcx>)]
3103impl<'tcx> ty::PolyTraitPredicate<'tcx> {
3104 fn print_modifiers_and_trait_path(
3105 self,
3106 ) -> ty::Binder<'tcx, TraitPredPrintModifiersAndPath<'tcx>> {
3107 self.map_bound(TraitPredPrintModifiersAndPath)
3108 }
3109
3110 fn print_with_bound_constness(
3111 self,
3112 constness: Option<ty::BoundConstness>,
3113 ) -> ty::Binder<'tcx, TraitPredPrintWithBoundConstness<'tcx>> {
3114 self.map_bound(|trait_pred| TraitPredPrintWithBoundConstness(trait_pred, constness))
3115 }
3116}
3117
3118#[derive(Debug, Copy, Clone, Lift)]
3119pub struct PrintClosureAsImpl<'tcx> {
3120 pub closure: ty::ClosureArgs<TyCtxt<'tcx>>,
3121}
3122
3123macro_rules! forward_display_to_print {
3124 ($($ty:ty),+) => {
3125 $(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
3127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3128 ty::tls::with(|tcx| {
3129 let mut cx = FmtPrinter::new(tcx, Namespace::TypeNS);
3130 tcx.lift(*self)
3131 .expect("could not lift for printing")
3132 .print(&mut cx)?;
3133 f.write_str(&cx.into_buffer())?;
3134 Ok(())
3135 })
3136 }
3137 })+
3138 };
3139}
3140
3141macro_rules! define_print {
3142 (($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
3143 $(impl<'tcx, P: PrettyPrinter<'tcx>> Print<'tcx, P> for $ty {
3144 fn print(&$self, $cx: &mut P) -> Result<(), PrintError> {
3145 define_scoped_cx!($cx);
3146 let _: () = $print;
3147 Ok(())
3148 }
3149 })+
3150 };
3151}
3152
3153macro_rules! define_print_and_forward_display {
3154 (($self:ident, $cx:ident): $($ty:ty $print:block)+) => {
3155 define_print!(($self, $cx): $($ty $print)*);
3156 forward_display_to_print!($($ty),+);
3157 };
3158}
3159
3160forward_display_to_print! {
3161 ty::Region<'tcx>,
3162 Ty<'tcx>,
3163 &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
3164 ty::Const<'tcx>
3165}
3166
3167define_print! {
3168 (self, cx):
3169
3170 ty::FnSig<'tcx> {
3171 p!(write("{}", self.safety.prefix_str()));
3172
3173 if self.abi != ExternAbi::Rust {
3174 p!(write("extern {} ", self.abi));
3175 }
3176
3177 p!("fn", pretty_fn_sig(self.inputs(), self.c_variadic, self.output()));
3178 }
3179
3180 ty::TraitRef<'tcx> {
3181 p!(write("<{} as {}>", self.self_ty(), self.print_only_trait_path()))
3182 }
3183
3184 ty::AliasTy<'tcx> {
3185 let alias_term: ty::AliasTerm<'tcx> = (*self).into();
3186 p!(print(alias_term))
3187 }
3188
3189 ty::AliasTerm<'tcx> {
3190 match self.kind(cx.tcx()) {
3191 ty::AliasTermKind::InherentTy => p!(pretty_print_inherent_projection(*self)),
3192 ty::AliasTermKind::ProjectionTy => {
3193 if !(cx.should_print_verbose() || with_reduced_queries())
3194 && cx.tcx().is_impl_trait_in_trait(self.def_id)
3195 {
3196 p!(pretty_print_rpitit(self.def_id, self.args))
3197 } else {
3198 p!(print_def_path(self.def_id, self.args));
3199 }
3200 }
3201 | ty::AliasTermKind::WeakTy
3202 | ty::AliasTermKind::OpaqueTy
3203 | ty::AliasTermKind::UnevaluatedConst
3204 | ty::AliasTermKind::ProjectionConst => {
3205 p!(print_def_path(self.def_id, self.args));
3206 }
3207 }
3208 }
3209
3210 ty::TraitPredicate<'tcx> {
3211 p!(print(self.trait_ref.self_ty()), ": ");
3212 if let ty::PredicatePolarity::Negative = self.polarity {
3213 p!("!");
3214 }
3215 p!(print(self.trait_ref.print_trait_sugared()))
3216 }
3217
3218 ty::HostEffectPredicate<'tcx> {
3219 let constness = match self.constness {
3220 ty::BoundConstness::Const => { "const" }
3221 ty::BoundConstness::Maybe => { "~const" }
3222 };
3223 p!(print(self.trait_ref.self_ty()), ": {constness} ");
3224 p!(print(self.trait_ref.print_trait_sugared()))
3225 }
3226
3227 ty::TypeAndMut<'tcx> {
3228 p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
3229 }
3230
3231 ty::ClauseKind<'tcx> {
3232 match *self {
3233 ty::ClauseKind::Trait(ref data) => {
3234 p!(print(data))
3235 }
3236 ty::ClauseKind::RegionOutlives(predicate) => p!(print(predicate)),
3237 ty::ClauseKind::TypeOutlives(predicate) => p!(print(predicate)),
3238 ty::ClauseKind::Projection(predicate) => p!(print(predicate)),
3239 ty::ClauseKind::HostEffect(predicate) => p!(print(predicate)),
3240 ty::ClauseKind::ConstArgHasType(ct, ty) => {
3241 p!("the constant `", print(ct), "` has type `", print(ty), "`")
3242 },
3243 ty::ClauseKind::WellFormed(arg) => p!(print(arg), " well-formed"),
3244 ty::ClauseKind::ConstEvaluatable(ct) => {
3245 p!("the constant `", print(ct), "` can be evaluated")
3246 }
3247 }
3248 }
3249
3250 ty::PredicateKind<'tcx> {
3251 match *self {
3252 ty::PredicateKind::Clause(data) => {
3253 p!(print(data))
3254 }
3255 ty::PredicateKind::Subtype(predicate) => p!(print(predicate)),
3256 ty::PredicateKind::Coerce(predicate) => p!(print(predicate)),
3257 ty::PredicateKind::DynCompatible(trait_def_id) => {
3258 p!("the trait `", print_def_path(trait_def_id, &[]), "` is dyn-compatible")
3259 }
3260 ty::PredicateKind::ConstEquate(c1, c2) => {
3261 p!("the constant `", print(c1), "` equals `", print(c2), "`")
3262 }
3263 ty::PredicateKind::Ambiguous => p!("ambiguous"),
3264 ty::PredicateKind::NormalizesTo(data) => p!(print(data)),
3265 ty::PredicateKind::AliasRelate(t1, t2, dir) => p!(print(t1), write(" {} ", dir), print(t2)),
3266 }
3267 }
3268
3269 ty::ExistentialPredicate<'tcx> {
3270 match *self {
3271 ty::ExistentialPredicate::Trait(x) => p!(print(x)),
3272 ty::ExistentialPredicate::Projection(x) => p!(print(x)),
3273 ty::ExistentialPredicate::AutoTrait(def_id) => {
3274 p!(print_def_path(def_id, &[]));
3275 }
3276 }
3277 }
3278
3279 ty::ExistentialTraitRef<'tcx> {
3280 let dummy_self = Ty::new_fresh(cx.tcx(), 0);
3282 let trait_ref = self.with_self_ty(cx.tcx(), dummy_self);
3283 p!(print(trait_ref.print_only_trait_path()))
3284 }
3285
3286 ty::ExistentialProjection<'tcx> {
3287 let name = cx.tcx().associated_item(self.def_id).name;
3288 let args = &self.args[cx.tcx().generics_of(self.def_id).parent_count - 1..];
3291 p!(path_generic_args(|cx| write!(cx, "{name}"), args), " = ", print(self.term))
3292 }
3293
3294 ty::ProjectionPredicate<'tcx> {
3295 p!(print(self.projection_term), " == ");
3296 cx.reset_type_limit();
3297 p!(print(self.term))
3298 }
3299
3300 ty::SubtypePredicate<'tcx> {
3301 p!(print(self.a), " <: ");
3302 cx.reset_type_limit();
3303 p!(print(self.b))
3304 }
3305
3306 ty::CoercePredicate<'tcx> {
3307 p!(print(self.a), " -> ");
3308 cx.reset_type_limit();
3309 p!(print(self.b))
3310 }
3311
3312 ty::NormalizesTo<'tcx> {
3313 p!(print(self.alias), " normalizes-to ");
3314 cx.reset_type_limit();
3315 p!(print(self.term))
3316 }
3317}
3318
3319define_print_and_forward_display! {
3320 (self, cx):
3321
3322 &'tcx ty::List<Ty<'tcx>> {
3323 p!("{{", comma_sep(self.iter()), "}}")
3324 }
3325
3326 TraitRefPrintOnlyTraitPath<'tcx> {
3327 p!(print_def_path(self.0.def_id, self.0.args));
3328 }
3329
3330 TraitRefPrintSugared<'tcx> {
3331 if !with_reduced_queries()
3332 && cx.tcx().trait_def(self.0.def_id).paren_sugar
3333 && let ty::Tuple(args) = self.0.args.type_at(1).kind()
3334 {
3335 p!(write("{}", cx.tcx().item_name(self.0.def_id)), "(");
3336 for (i, arg) in args.iter().enumerate() {
3337 if i > 0 {
3338 p!(", ");
3339 }
3340 p!(print(arg));
3341 }
3342 p!(")");
3343 } else {
3344 p!(print_def_path(self.0.def_id, self.0.args));
3345 }
3346 }
3347
3348 TraitRefPrintOnlyTraitName<'tcx> {
3349 p!(print_def_path(self.0.def_id, &[]));
3350 }
3351
3352 TraitPredPrintModifiersAndPath<'tcx> {
3353 if let ty::PredicatePolarity::Negative = self.0.polarity {
3354 p!("!")
3355 }
3356 p!(print(self.0.trait_ref.print_trait_sugared()));
3357 }
3358
3359 TraitPredPrintWithBoundConstness<'tcx> {
3360 p!(print(self.0.trait_ref.self_ty()), ": ");
3361 if let Some(constness) = self.1 {
3362 p!(pretty_print_bound_constness(constness));
3363 }
3364 if let ty::PredicatePolarity::Negative = self.0.polarity {
3365 p!("!");
3366 }
3367 p!(print(self.0.trait_ref.print_trait_sugared()))
3368 }
3369
3370 PrintClosureAsImpl<'tcx> {
3371 p!(pretty_closure_as_impl(self.closure))
3372 }
3373
3374 ty::ParamTy {
3375 p!(write("{}", self.name))
3376 }
3377
3378 ty::ParamConst {
3379 p!(write("{}", self.name))
3380 }
3381
3382 ty::Term<'tcx> {
3383 match self.unpack() {
3384 ty::TermKind::Ty(ty) => p!(print(ty)),
3385 ty::TermKind::Const(c) => p!(print(c)),
3386 }
3387 }
3388
3389 ty::Predicate<'tcx> {
3390 p!(print(self.kind()))
3391 }
3392
3393 ty::Clause<'tcx> {
3394 p!(print(self.kind()))
3395 }
3396
3397 GenericArg<'tcx> {
3398 match self.unpack() {
3399 GenericArgKind::Lifetime(lt) => p!(print(lt)),
3400 GenericArgKind::Type(ty) => p!(print(ty)),
3401 GenericArgKind::Const(ct) => p!(print(ct)),
3402 }
3403 }
3404}
3405
3406fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, Namespace, DefId)) {
3407 for id in tcx.hir_free_items() {
3409 if matches!(tcx.def_kind(id.owner_id), DefKind::Use) {
3410 continue;
3411 }
3412
3413 let item = tcx.hir_item(id);
3414 let Some(ident) = item.kind.ident() else { continue };
3415
3416 let def_id = item.owner_id.to_def_id();
3417 let ns = tcx.def_kind(def_id).ns().unwrap_or(Namespace::TypeNS);
3418 collect_fn(&ident, ns, def_id);
3419 }
3420
3421 let queue = &mut Vec::new();
3423 let mut seen_defs: DefIdSet = Default::default();
3424
3425 for &cnum in tcx.crates(()).iter() {
3426 match tcx.extern_crate(cnum) {
3428 None => continue,
3429 Some(extern_crate) => {
3430 if !extern_crate.is_direct() {
3431 continue;
3432 }
3433 }
3434 }
3435
3436 queue.push(cnum.as_def_id());
3437 }
3438
3439 while let Some(def) = queue.pop() {
3441 for child in tcx.module_children(def).iter() {
3442 if !child.vis.is_public() {
3443 continue;
3444 }
3445
3446 match child.res {
3447 def::Res::Def(DefKind::AssocTy, _) => {}
3448 def::Res::Def(DefKind::TyAlias, _) => {}
3449 def::Res::Def(defkind, def_id) => {
3450 if let Some(ns) = defkind.ns() {
3451 collect_fn(&child.ident, ns, def_id);
3452 }
3453
3454 if matches!(defkind, DefKind::Mod | DefKind::Enum | DefKind::Trait)
3455 && seen_defs.insert(def_id)
3456 {
3457 queue.push(def_id);
3458 }
3459 }
3460 _ => {}
3461 }
3462 }
3463 }
3464}
3465
3466pub fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> DefIdMap<Symbol> {
3482 tcx.sess.record_trimmed_def_paths();
3489
3490 let unique_symbols_rev: &mut FxHashMap<(Namespace, Symbol), Option<DefId>> =
3493 &mut FxHashMap::default();
3494
3495 for symbol_set in tcx.resolutions(()).glob_map.values() {
3496 for symbol in symbol_set {
3497 unique_symbols_rev.insert((Namespace::TypeNS, *symbol), None);
3498 unique_symbols_rev.insert((Namespace::ValueNS, *symbol), None);
3499 unique_symbols_rev.insert((Namespace::MacroNS, *symbol), None);
3500 }
3501 }
3502
3503 for_each_def(tcx, |ident, ns, def_id| {
3504 use std::collections::hash_map::Entry::{Occupied, Vacant};
3505
3506 match unique_symbols_rev.entry((ns, ident.name)) {
3507 Occupied(mut v) => match v.get() {
3508 None => {}
3509 Some(existing) => {
3510 if *existing != def_id {
3511 v.insert(None);
3512 }
3513 }
3514 },
3515 Vacant(v) => {
3516 v.insert(Some(def_id));
3517 }
3518 }
3519 });
3520
3521 let mut map: DefIdMap<Symbol> = Default::default();
3523 for ((_, symbol), opt_def_id) in unique_symbols_rev.drain() {
3524 use std::collections::hash_map::Entry::{Occupied, Vacant};
3525
3526 if let Some(def_id) = opt_def_id {
3527 match map.entry(def_id) {
3528 Occupied(mut v) => {
3529 if *v.get() != symbol && v.get().as_str() > symbol.as_str() {
3538 v.insert(symbol);
3539 }
3540 }
3541 Vacant(v) => {
3542 v.insert(symbol);
3543 }
3544 }
3545 }
3546 }
3547
3548 map
3549}
3550
3551pub fn provide(providers: &mut Providers) {
3552 *providers = Providers { trimmed_def_paths, ..*providers };
3553}
3554
3555pub struct OpaqueFnEntry<'tcx> {
3556 kind: ty::ClosureKind,
3557 return_ty: Option<ty::Binder<'tcx, Term<'tcx>>>,
3558}