self: PrintCx<'_, '_, 'tcx, Self>,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
- ns: Namespace,
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
) -> Result<Self::Path, Self::Error> {
- self.default_print_def_path(def_id, substs, ns, projections)
+ self.default_print_def_path(def_id, substs, projections)
}
fn print_impl_path(
self: PrintCx<'_, '_, 'tcx, Self>,
impl_def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
- ns: Namespace,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
- self.default_print_impl_path(impl_def_id, substs, ns, self_ty, trait_ref)
+ self.default_print_impl_path(impl_def_id, substs, self_ty, trait_ref)
}
fn print_region(
self: PrintCx<'_, '_, 'tcx, Self>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
- ns: Namespace,
) -> Result<Self::Path, Self::Error>;
fn path_append_impl<'gcx, 'tcx>(
) -> Result<Self::Path, Self::Error>,
params: &[ty::GenericParamDef],
substs: SubstsRef<'tcx>,
- ns: Namespace,
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
) -> Result<Self::Path, Self::Error>;
}
})
}
+ /// Like `print_def_path` but for value paths.
+ fn print_value_path(
+ self: PrintCx<'_, '_, 'tcx, Self>,
+ def_id: DefId,
+ substs: Option<SubstsRef<'tcx>>,
+ ) -> Result<Self::Path, Self::Error> {
+ self.print_def_path(def_id, substs, iter::empty())
+ }
+
/// Print `<...>` around what `f` prints.
fn generic_delimiters<'gcx, 'tcx>(
- mut self: PrintCx<'_, 'gcx, 'tcx, Self>,
+ self: PrintCx<'_, 'gcx, 'tcx, Self>,
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, Self::Error>,
- ) -> Result<Self, Self::Error> {
- write!(self.printer, "<")?;
- let mut printer = f(self)?;
- write!(printer, ">")?;
- Ok(printer)
- }
+ ) -> Result<Self, Self::Error>;
/// Return `true` if the region should be printed in path generic args
/// even when it's `'_`, such as in e.g. `Foo<'_, '_, '_>`.
let ns = self.guess_def_namespace(def_id);
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
let mut s = String::new();
- let _ = PrintCx::with(self, FmtPrinter::new(&mut s), |cx| {
- cx.print_def_path(def_id, None, ns, iter::empty())
+ let _ = PrintCx::with(self, FmtPrinter::new(&mut s, ns), |cx| {
+ cx.print_def_path(def_id, None, iter::empty())
});
s
}
self,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
- ns: Namespace,
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
) -> Result<P::Path, P::Error> {
- debug!("default_print_def_path: def_id={:?}, substs={:?}, ns={:?}", def_id, substs, ns);
+ debug!("default_print_def_path: def_id={:?}, substs={:?}", def_id, substs);
let key = self.tcx.def_key(def_id);
debug!("default_print_def_path: key={:?}", key);
if let Some(substs) = substs {
impl_trait_ref = impl_trait_ref.subst(self.tcx, substs);
}
- self.print_impl_path(def_id, substs, ns, self_ty, impl_trait_ref)
+ self.print_impl_path(def_id, substs, self_ty, impl_trait_ref)
}
_ => {
parent_generics.has_self && parent_generics.parent_count == 0;
if let (Some(substs), true) = (substs, parent_has_own_self) {
let trait_ref = ty::TraitRef::new(parent_def_id, substs);
- cx.path_qualified(trait_ref.self_ty(), Some(trait_ref), ns)
+ cx.path_qualified(trait_ref.self_ty(), Some(trait_ref))
} else {
- cx.print_def_path(parent_def_id, substs, ns, iter::empty())
+ cx.print_def_path(parent_def_id, substs, iter::empty())
}
} else {
- cx.print_def_path(parent_def_id, None, ns, iter::empty())
+ cx.print_def_path(parent_def_id, None, iter::empty())
}
};
let print_path = |cx: PrintCx<'_, 'gcx, 'tcx, P>| {
if let (Some(generics), Some(substs)) = (generics, substs) {
let has_own_self = generics.has_self && generics.parent_count == 0;
let params = &generics.params[has_own_self as usize..];
- self.path_generic_args(print_path, params, substs, ns, projections)
+ self.path_generic_args(print_path, params, substs, projections)
} else {
print_path(self)
}
self,
impl_def_id: DefId,
_substs: Option<SubstsRef<'tcx>>,
- ns: Namespace,
self_ty: Ty<'tcx>,
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<P::Path, P::Error> {
// trait-type, then fallback to a format that identifies
// the module more clearly.
self.path_append_impl(
- |cx| cx.print_def_path(parent_def_id, None, ns, iter::empty()),
+ |cx| cx.print_def_path(parent_def_id, None, iter::empty()),
self_ty,
impl_trait_ref,
)
} else {
// Otherwise, try to give a good form that would be valid language
// syntax. Preferably using associated item notation.
- self.path_qualified(self_ty, impl_trait_ref, ns)
+ self.path_qualified(self_ty, impl_trait_ref)
}
}
}
pub struct FmtPrinter<F: fmt::Write> {
pub(crate) fmt: F,
empty: bool,
+ in_value: bool,
pub region_highlight_mode: RegionHighlightMode,
}
impl<F: fmt::Write> FmtPrinter<F> {
- pub fn new(fmt: F) -> Self {
+ pub fn new(fmt: F, ns: Namespace) -> Self {
FmtPrinter {
fmt,
empty: true,
+ in_value: ns == Namespace::ValueNS,
region_highlight_mode: RegionHighlightMode::default(),
}
}
}) => {
debug!("try_print_visible_def_path: def_id={:?}", def_id);
return Ok((if !span.is_dummy() {
- self.print_def_path(def_id, None, Namespace::TypeNS, iter::empty())?
+ self.print_def_path(def_id, None, iter::empty())?
} else {
self.path_crate(cnum)?
}, true));
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
- ns: Namespace,
) -> Result<P::Path, P::Error> {
if trait_ref.is_none() {
// Inherent impls. Try to print `Foo::bar` for an inherent
// impl on `Foo`, but fallback to `<Foo>::bar` if self-type is
// anything other than a simple path.
match self_ty.sty {
- ty::Adt(adt_def, substs) => {
- return self.print_def_path(adt_def.did, Some(substs), ns, iter::empty());
- }
- ty::Foreign(did) => {
- return self.print_def_path(did, None, ns, iter::empty());
- }
-
+ ty::Adt(..) | ty::Foreign(_) |
ty::Bool | ty::Char | ty::Str |
ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
return self_ty.print_display(self);
nest!(cx, |cx| self_ty.print_display(cx));
if let Some(trait_ref) = trait_ref {
write!(cx.printer, " as ")?;
- nest!(cx, |cx| cx.print_def_path(
- trait_ref.def_id,
- Some(trait_ref.substs),
- Namespace::TypeNS,
- iter::empty(),
- ));
+ nest!(cx, |cx| trait_ref.print_display(cx));
}
Ok(cx.printer)
})
) -> Result<P::Path, P::Error>,
params: &[ty::GenericParamDef],
substs: SubstsRef<'tcx>,
- ns: Namespace,
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
) -> Result<P::Path, P::Error> {
nest!(self, |cx| print_prefix(cx));
return Ok(self.printer);
}
- // FIXME(eddyb) move this into `generic_delimiters`.
- if ns == Namespace::ValueNS {
- write!(self.printer, "::")?;
- }
-
self.generic_delimiters(|mut cx| {
let mut empty = true;
let mut maybe_comma = |cx: &mut Self| {
mut self: PrintCx<'_, '_, 'tcx, Self>,
def_id: DefId,
substs: Option<SubstsRef<'tcx>>,
- ns: Namespace,
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
) -> Result<Self::Path, Self::Error> {
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
return if let (Some(generics), Some(substs)) = (generics, substs) {
let has_own_self = generics.has_self && generics.parent_count == 0;
let params = &generics.params[has_own_self as usize..];
- self.path_generic_args(|cx| Ok(cx.printer), params, substs, ns, projections)
+ self.path_generic_args(|cx| Ok(cx.printer), params, substs, projections)
} else {
Ok(self.printer)
};
let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id };
let span = self.tcx.def_span(def_id);
return self.path_append(
- |cx| cx.print_def_path(parent_def_id, None, ns, iter::empty()),
+ |cx| cx.print_def_path(parent_def_id, None, iter::empty()),
&format!("<impl at {:?}>", span),
);
}
}
- self.default_print_def_path(def_id, substs, ns, projections)
+ self.default_print_def_path(def_id, substs, projections)
}
fn print_region(
self: PrintCx<'_, '_, 'tcx, Self>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
- ns: Namespace,
) -> Result<Self::Path, Self::Error> {
- self.pretty_path_qualified(self_ty, trait_ref, ns)
+ self.pretty_path_qualified(self_ty, trait_ref)
}
fn path_append_impl<'gcx, 'tcx>(
self.pretty_path_append_impl(|cx| {
let mut printer = print_prefix(cx)?;
- if !printer.empty {
+ // HACK(eddyb) this accounts for `generic_delimiters`
+ // printing `::<` instead of `<` if `in_value` is set.
+ if !printer.empty && !printer.in_value {
write!(printer, "::")?;
}
) -> Result<Self::Path, Self::Error>,
params: &[ty::GenericParamDef],
substs: SubstsRef<'tcx>,
- ns: Namespace,
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
) -> Result<Self::Path, Self::Error> {
- self.pretty_path_generic_args(print_prefix, params, substs, ns, projections)
+ self.pretty_path_generic_args(print_prefix, params, substs, projections)
}
}
})
}
+ fn print_value_path(
+ mut self: PrintCx<'_, '_, 'tcx, Self>,
+ def_id: DefId,
+ substs: Option<SubstsRef<'tcx>>,
+ ) -> Result<Self::Path, Self::Error> {
+ let was_in_value = std::mem::replace(&mut self.printer.in_value, true);
+ let mut printer = self.print_def_path(def_id, substs, iter::empty())?;
+ printer.in_value = was_in_value;
+
+ Ok(printer)
+ }
+
+ fn generic_delimiters<'gcx, 'tcx>(
+ mut self: PrintCx<'_, 'gcx, 'tcx, Self>,
+ f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, Self::Error>,
+ ) -> Result<Self, Self::Error> {
+ if !self.printer.empty && self.printer.in_value {
+ write!(self.printer, "::<")?;
+ } else {
+ write!(self.printer, "<")?;
+ }
+
+ let was_in_value = std::mem::replace(&mut self.printer.in_value, false);
+ let mut printer = f(self)?;
+ printer.in_value = was_in_value;
+
+ write!(printer, ">")?;
+ Ok(printer)
+ }
+
fn always_print_region_in_paths(
self: &PrintCx<'_, '_, '_, Self>,
region: ty::Region<'_>,
macro_rules! gen_display_debug_body {
( $with:path ) => {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- PrintCx::with_tls_tcx(FmtPrinter::new(f), |cx| {
+ PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |cx| {
$with(&cx.tcx.lift(self).expect("could not lift for printing"), cx)?;
Ok(())
})
substs: SubstsRef<'_>,
ns: Namespace,
) -> fmt::Result {
- PrintCx::with_tls_tcx(FmtPrinter::new(f), |cx| {
+ PrintCx::with_tls_tcx(FmtPrinter::new(f, ns), |cx| {
let substs = cx.tcx.lift(&substs).expect("could not lift for printing");
- cx.print_def_path(did, Some(substs), ns, iter::empty())?;
+ cx.print_def_path(did, Some(substs), iter::empty())?;
Ok(())
})
}
if let ty::Tuple(ref args) = principal.substs.type_at(0).sty {
let mut projections = self.projection_bounds();
if let (Some(proj), None) = (projections.next(), projections.next()) {
- nest!(|cx| cx.print_def_path(
- principal.def_id,
- None,
- Namespace::TypeNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_def_path(principal.def_id, None, iter::empty()));
nest!(|cx| cx.fn_sig(args, false, proj.ty));
resugared_principal = true;
}
nest!(|cx| cx.print_def_path(
principal.def_id,
Some(principal.substs),
- Namespace::TypeNS,
self.projection_bounds(),
));
}
}
first = false;
- nest!(|cx| cx.print_def_path(
- def_id,
- None,
- Namespace::TypeNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_def_path(def_id, None, iter::empty()));
}
}
}
impl fmt::Debug for ty::TraitDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- PrintCx::with_tls_tcx(FmtPrinter::new(f), |cx| {
- cx.print_def_path(
- self.def_id,
- None,
- Namespace::TypeNS,
- iter::empty(),
- )?;
+ PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |cx| {
+ cx.print_def_path(self.def_id, None, iter::empty())?;
Ok(())
})
}
impl fmt::Debug for ty::AdtDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- PrintCx::with_tls_tcx(FmtPrinter::new(f), |cx| {
- cx.print_def_path(
- self.did,
- None,
- Namespace::TypeNS,
- iter::empty(),
- )?;
+ PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |cx| {
+ cx.print_def_path(self.did, None, iter::empty())?;
Ok(())
})
}
impl fmt::Debug for ty::UpvarId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- PrintCx::with_tls_tcx(FmtPrinter::new(f), |mut cx| {
+ PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::ValueNS), |mut cx| {
define_scoped_cx!(cx);
p!(write("UpvarId({:?};`{}`;{:?})",
self.var_path.hir_id,
define_print! {
('tcx) ty::TraitRef<'tcx>, (self, cx) {
display {
- nest!(|cx| cx.print_def_path(
- self.def_id,
- Some(self.substs),
- Namespace::TypeNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_def_path(self.def_id, Some(self.substs), iter::empty()));
}
debug {
- nest!(|cx| cx.path_qualified(self.self_ty(), Some(*self), Namespace::TypeNS));
+ nest!(|cx| cx.path_qualified(self.self_ty(), Some(*self)));
}
}
}
ty::FnDef(def_id, substs) => {
let sig = self.tcx.fn_sig(def_id).subst(self.tcx, substs);
p!(print(sig), write(" {{"));
- nest!(|cx| cx.print_def_path(
- def_id,
- Some(substs),
- Namespace::ValueNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_value_path(def_id, Some(substs)));
p!(write("}}"))
}
ty::FnPtr(ref bare_fn) => {
}
}
ty::Adt(def, substs) => {
- nest!(|cx| cx.print_def_path(
- def.did,
- Some(substs),
- Namespace::TypeNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_def_path(def.did, Some(substs), iter::empty()));
}
ty::Dynamic(data, r) => {
let print_r = self.print_region_outputs_anything(r);
}
}
ty::Foreign(def_id) => {
- nest!(|cx| cx.print_def_path(
- def_id,
- None,
- Namespace::TypeNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_def_path(def_id, None, iter::empty()));
}
ty::Projection(ref data) => p!(print(data)),
ty::UnnormalizedProjection(ref data) => {
define_print! {
('tcx) ty::ProjectionTy<'tcx>, (self, cx) {
display {
- nest!(|cx| cx.print_def_path(
- self.item_def_id,
- Some(self.substs),
- Namespace::TypeNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_def_path(self.item_def_id, Some(self.substs), iter::empty()));
}
}
}
ty::Predicate::WellFormed(ty) => p!(print(ty), write(" well-formed")),
ty::Predicate::ObjectSafe(trait_def_id) => {
p!(write("the trait `"));
- nest!(|cx| cx.print_def_path(
- trait_def_id,
- None,
- Namespace::TypeNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_def_path(trait_def_id, None, iter::empty()));
p!(write("` is object-safe"))
}
ty::Predicate::ClosureKind(closure_def_id, _closure_substs, kind) => {
p!(write("the closure `"));
- nest!(|cx| cx.print_def_path(
- closure_def_id,
- None,
- Namespace::ValueNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_value_path(closure_def_id, None));
p!(write("` implements the trait `{}`", kind))
}
ty::Predicate::ConstEvaluatable(def_id, substs) => {
p!(write("the constant `"));
- nest!(|cx| cx.print_def_path(
- def_id,
- Some(substs),
- Namespace::ValueNS,
- iter::empty(),
- ));
+ nest!(|cx| cx.print_value_path(def_id, Some(substs)));
p!(write("` can be evaluated"))
}
}