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