rustc: move <...>-less impl path special-case to pretty_path_qualified.

This commit is contained in:
Eduard-Mihai Burtescu 2018-12-21 17:31:33 +02:00
parent a15bfc6f48
commit aec5a48481
4 changed files with 43 additions and 46 deletions

View file

@ -173,10 +173,9 @@ pub trait Printer: Sized {
self: &mut PrintCx<'_, '_, 'tcx, Self>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
ns: Namespace,
) -> Self::Path;
#[must_use]
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path;
#[must_use]
fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,
path: Self::Path,
@ -291,7 +290,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
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);
self.path_qualified(trait_ref.self_ty(), Some(trait_ref))
self.path_qualified(trait_ref.self_ty(), Some(trait_ref), ns)
} else {
self.print_def_path(parent_def_id, substs, ns, iter::empty())
}
@ -367,35 +366,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
// Otherwise, try to give a good form that would be valid language
// syntax. Preferably using associated item notation.
if let Some(trait_ref) = impl_trait_ref {
// Trait impls.
return self.path_qualified(self_ty, Some(trait_ref));
}
// 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) => {
self.print_def_path(adt_def.did, Some(substs), ns, iter::empty())
}
ty::Foreign(did) => self.print_def_path(did, None, ns, iter::empty()),
ty::Bool |
ty::Char |
ty::Int(_) |
ty::Uint(_) |
ty::Float(_) |
ty::Str => {
self.path_impl(&self_ty.to_string())
}
_ => {
self.path_qualified(self_ty, None)
}
}
self.path_qualified(self_ty, impl_trait_ref, ns)
}
}
@ -587,7 +558,30 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
&mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
ns: Namespace,
) -> P::Path {
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::Bool | ty::Char | ty::Str |
ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
self_ty.print_display(self)?;
return Ok(PrettyPath { empty: false });
}
_ => {}
}
}
write!(self.printer, "<")?;
self_ty.print_display(self)?;
if let Some(trait_ref) = trait_ref {
@ -781,12 +775,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
self: &mut PrintCx<'_, '_, 'tcx, Self>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
ns: Namespace,
) -> Self::Path {
self.pretty_path_qualified(self_ty, trait_ref)
}
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
write!(self.printer, "{}", text)?;
Ok(PrettyPath { empty: false })
self.pretty_path_qualified(self_ty, trait_ref, ns)
}
fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,

View file

@ -1007,7 +1007,7 @@ define_print! {
Ok(())
}
debug {
let _ = cx.path_qualified(self.self_ty(), Some(*self))?;
let _ = cx.path_qualified(self.self_ty(), Some(*self), Namespace::TypeNS)?;
Ok(())
}
}

View file

@ -416,16 +416,24 @@ impl Printer for SymbolPath {
self: &mut PrintCx<'_, '_, 'tcx, Self>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
ns: Namespace,
) -> Self::Path {
// HACK(eddyb) avoid `keep_within_component` for the cases
// that print without `<...>` around `self_ty`.
match self_ty.sty {
ty::Adt(..) | ty::Foreign(_) |
ty::Bool | ty::Char | ty::Str |
ty::Int(_) | ty::Uint(_) | ty::Float(_) if trait_ref.is_none() => {
return self.pretty_path_qualified(self_ty, trait_ref, ns);
}
_ => {}
}
let kept_within_component = mem::replace(&mut self.printer.keep_within_component, true);
let r = self.pretty_path_qualified(self_ty, trait_ref);
let r = self.pretty_path_qualified(self_ty, trait_ref, ns);
self.printer.keep_within_component = kept_within_component;
r
}
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
self.printer.write_str(text)?;
Ok(PrettyPath { empty: false })
}
fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,
_: Self::Path,

View file

@ -4239,6 +4239,7 @@ where F: Fn(DefId) -> Def {
self: &mut PrintCx<'_, '_, 'tcx, Self>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
_ns: Namespace,
) -> Self::Path {
// This shouldn't ever be needed, but just in case:
if let Some(trait_ref) = trait_ref {
@ -4247,9 +4248,6 @@ where F: Fn(DefId) -> Def {
vec![format!("<{}>", self_ty)]
}
}
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
vec![text.to_string()]
}
fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,
mut path: Self::Path,