1 use rustc::hir::map::{DefPathData, DisambiguatedDefPathData};
2 use rustc::mir::interpret::Allocation;
5 print::{PrettyPrinter, Print, Printer},
6 subst::{GenericArg, GenericArgKind},
9 use rustc_hir::def_id::CrateNum;
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 { 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::UnnormalizedProjection(ty::ProjectionTy { item_def_id: def_id, substs })
64 | ty::Closure(def_id, substs)
65 | ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),
66 ty::Foreign(def_id) => self.print_def_path(def_id, &[]),
68 ty::GeneratorWitness(_) => bug!("type_name: unexpected `GeneratorWitness`"),
72 fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
73 self.pretty_print_const(ct, false)
76 fn print_dyn_existential(
78 predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
79 ) -> Result<Self::DynExistential, Self::Error> {
86 self = p.print(self)?;
91 fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
92 self.path.push_str(&self.tcx.original_crate_name(cnum).as_str());
99 trait_ref: Option<ty::TraitRef<'tcx>>,
100 ) -> Result<Self::Path, Self::Error> {
101 self.pretty_path_qualified(self_ty, trait_ref)
106 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
107 _disambiguated_data: &DisambiguatedDefPathData,
109 trait_ref: Option<ty::TraitRef<'tcx>>,
110 ) -> Result<Self::Path, Self::Error> {
111 self.pretty_path_append_impl(
113 cx = print_prefix(cx)?;
115 cx.path.push_str("::");
126 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
127 disambiguated_data: &DisambiguatedDefPathData,
128 ) -> Result<Self::Path, Self::Error> {
129 self = print_prefix(self)?;
131 // Skip `::{{constructor}}` on tuple/unit structs.
132 match disambiguated_data.data {
133 DefPathData::Ctor => return Ok(self),
137 self.path.push_str("::");
139 self.path.push_str(&disambiguated_data.data.as_symbol().as_str());
143 fn path_generic_args(
145 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
146 args: &[GenericArg<'tcx>],
147 ) -> Result<Self::Path, Self::Error> {
148 self = print_prefix(self)?;
149 let args = args.iter().cloned().filter(|arg| match arg.unpack() {
150 GenericArgKind::Lifetime(_) => false,
153 if args.clone().next().is_some() {
154 self.generic_delimiters(|cx| cx.comma_sep(args))
160 impl PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
161 fn region_should_not_be_omitted(&self, _region: ty::Region<'_>) -> bool {
164 fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, Self::Error>
166 T: Print<'tcx, Self, Output = Self, Error = Self::Error>,
168 if let Some(first) = elems.next() {
169 self = first.print(self)?;
171 self.path.push_str(", ");
172 self = elem.print(self)?;
178 fn generic_delimiters(
180 f: impl FnOnce(Self) -> Result<Self, Self::Error>,
181 ) -> Result<Self, Self::Error> {
192 impl Write for AbsolutePathPrinter<'_> {
193 fn write_str(&mut self, s: &str) -> std::fmt::Result {
194 Ok(self.path.push_str(s))
198 /// Directly returns an `Allocation` containing an absolute path representation of the given type.
199 crate fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> &'tcx Allocation {
200 let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
201 let alloc = Allocation::from_byte_aligned_bytes(path.into_bytes());
202 tcx.intern_const_alloc(alloc)