1 use rustc_data_structures::intern::Interned;
2 use rustc_hir::def_id::CrateNum;
3 use rustc_hir::definitions::DisambiguatedDefPathData;
4 use rustc_middle::ty::{
6 print::{PrettyPrinter, Print, Printer},
7 subst::{GenericArg, GenericArgKind},
12 struct AbsolutePathPrinter<'tcx> {
17 impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
18 type Error = std::fmt::Error;
23 type DynExistential = Self;
26 fn tcx(&self) -> TyCtxt<'tcx> {
30 fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
34 fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
36 // Types without identity.
50 | ty::Dynamic(_, _, _) => self.pretty_print_type(ty),
52 // Placeholders (all printed as `_` to uniformize them).
53 ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => {
58 // Types with identity (print the module path).
59 ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), substs)
60 | ty::FnDef(def_id, substs)
61 | ty::Opaque(def_id, substs)
62 | ty::Projection(ty::ProjectionTy { item_def_id: def_id, substs })
63 | ty::Closure(def_id, substs)
64 | ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),
65 ty::Foreign(def_id) => self.print_def_path(def_id, &[]),
67 ty::GeneratorWitness(_) => bug!("type_name: unexpected `GeneratorWitness`"),
71 fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
72 self.pretty_print_const(ct, false)
75 fn print_dyn_existential(
77 predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
78 ) -> Result<Self::DynExistential, Self::Error> {
79 self.pretty_print_dyn_existential(predicates)
82 fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
83 self.path.push_str(self.tcx.crate_name(cnum).as_str());
90 trait_ref: Option<ty::TraitRef<'tcx>>,
91 ) -> Result<Self::Path, Self::Error> {
92 self.pretty_path_qualified(self_ty, trait_ref)
97 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
98 _disambiguated_data: &DisambiguatedDefPathData,
100 trait_ref: Option<ty::TraitRef<'tcx>>,
101 ) -> Result<Self::Path, Self::Error> {
102 self.pretty_path_append_impl(
104 cx = print_prefix(cx)?;
106 cx.path.push_str("::");
117 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
118 disambiguated_data: &DisambiguatedDefPathData,
119 ) -> Result<Self::Path, Self::Error> {
120 self = print_prefix(self)?;
122 write!(self.path, "::{}", disambiguated_data.data).unwrap();
127 fn path_generic_args(
129 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
130 args: &[GenericArg<'tcx>],
131 ) -> Result<Self::Path, Self::Error> {
132 self = print_prefix(self)?;
134 args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
135 if args.clone().next().is_some() {
136 self.generic_delimiters(|cx| cx.comma_sep(args))
143 impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
144 fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
147 fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, Self::Error>
149 T: Print<'tcx, Self, Output = Self, Error = Self::Error>,
151 if let Some(first) = elems.next() {
152 self = first.print(self)?;
154 self.path.push_str(", ");
155 self = elem.print(self)?;
161 fn generic_delimiters(
163 f: impl FnOnce(Self) -> Result<Self, Self::Error>,
164 ) -> Result<Self, Self::Error> {
174 fn should_print_verbose(&self) -> bool {
175 // `std::any::type_name` should never print verbose type names
180 impl Write for AbsolutePathPrinter<'_> {
181 fn write_str(&mut self, s: &str) -> std::fmt::Result {
182 self.path.push_str(s);
187 pub fn type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> String {
188 AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path