rustc: move ...::<impl ...> printing into pretty_path_qualified.

This commit is contained in:
Eduard-Mihai Burtescu 2018-12-28 06:09:22 +02:00
parent 39fd54a418
commit df6650f38c
4 changed files with 68 additions and 23 deletions

View File

@ -173,6 +173,7 @@ pub trait Printer: Sized {
#[must_use]
fn path_qualified(
self: &mut PrintCx<'_, '_, 'tcx, Self>,
impl_prefix: Option<Self::Path>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
ns: Namespace,
@ -301,7 +302,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), ns)
self.path_qualified(None, trait_ref.self_ty(), Some(trait_ref), ns)
} else {
self.print_def_path(parent_def_id, substs, ns, iter::empty())
}
@ -357,21 +358,18 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
Some(trait_ref) => self.tcx.parent(trait_ref.def_id) == Some(parent_def_id),
};
if !in_self_mod && !in_trait_mod {
let prefix_path = if !in_self_mod && !in_trait_mod {
// If the impl is not co-located with either self-type or
// trait-type, then fallback to a format that identifies
// the module more clearly.
let path = self.print_def_path(parent_def_id, None, ns, iter::empty());
if let Some(trait_ref) = impl_trait_ref {
return self.path_append(path, &format!("<impl {} for {}>", trait_ref, self_ty));
} else {
return self.path_append(path, &format!("<impl {}>", self_ty));
}
}
Some(self.print_def_path(parent_def_id, None, ns, iter::empty()))
} else {
// Otherwise, try to give a good form that would be valid language
// syntax. Preferably using associated item notation.
None
};
// 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(prefix_path, self_ty, impl_trait_ref, ns)
}
}
@ -561,10 +559,24 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
pub fn pretty_path_qualified(
&mut self,
impl_prefix: Option<P::Path>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
ns: Namespace,
) -> P::Path {
if let Some(prefix) = impl_prefix {
// HACK(eddyb) going through `path_append` means symbol name
// computation gets to handle its equivalent of `::` correctly.
let _ = self.path_append(prefix, "<impl ")?;
if let Some(trait_ref) = trait_ref {
trait_ref.print_display(self)?;
write!(self.printer, " for ")?;
}
self_ty.print_display(self)?;
write!(self.printer, ">")?;
return Ok(PrettyPath { empty: false });
}
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
@ -774,11 +786,12 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
}
fn path_qualified(
self: &mut PrintCx<'_, '_, 'tcx, Self>,
impl_prefix: Option<Self::Path>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
ns: Namespace,
) -> Self::Path {
self.pretty_path_qualified(self_ty, trait_ref, ns)
self.pretty_path_qualified(impl_prefix, 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), Namespace::TypeNS)?;
let _ = cx.path_qualified(None, self.self_ty(), Some(*self), Namespace::TypeNS)?;
Ok(())
}
}

View File

@ -387,7 +387,7 @@ impl SymbolPath {
}
fn finalize_pending_component(&mut self) {
if !self.keep_within_component && !self.temp_buf.is_empty() {
if !self.temp_buf.is_empty() {
let _ = write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);
self.temp_buf.clear();
}
@ -414,6 +414,7 @@ impl Printer for SymbolPath {
}
fn path_qualified(
self: &mut PrintCx<'_, '_, 'tcx, Self>,
impl_prefix: Option<Self::Path>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
ns: Namespace,
@ -423,25 +424,51 @@ impl Printer for SymbolPath {
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);
ty::Int(_) | ty::Uint(_) | ty::Float(_)
if impl_prefix.is_none() && trait_ref.is_none() =>
{
return self.pretty_path_qualified(None, self_ty, trait_ref, ns);
}
_ => {}
}
// HACK(eddyb) make sure to finalize the last component of the
// `impl` prefix, to avoid it fusing with the following text.
let impl_prefix = impl_prefix.map(|prefix| {
let mut prefix = self.path_append(prefix, "")?;
// HACK(eddyb) also avoid an unnecessary `::`.
prefix.empty = true;
Ok(prefix)
});
let kept_within_component = mem::replace(&mut self.printer.keep_within_component, true);
let r = self.pretty_path_qualified(self_ty, trait_ref, ns);
let r = self.pretty_path_qualified(impl_prefix, self_ty, trait_ref, ns);
self.printer.keep_within_component = kept_within_component;
r
}
fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,
_: Self::Path,
path: Self::Path,
text: &str,
) -> Self::Path {
self.printer.finalize_pending_component();
let mut path = path?;
if self.keep_within_component {
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
if !path.empty {
self.printer.write_str("::")?;
} else {
path.empty = text.is_empty();
}
} else {
self.printer.finalize_pending_component();
path.empty = false;
}
self.printer.write_str(text)?;
Ok(PrettyPath { empty: false })
Ok(path)
}
fn path_generic_args(
self: &mut PrintCx<'_, '_, 'tcx, Self>,

View File

@ -4237,16 +4237,21 @@ where F: Fn(DefId) -> Def {
}
fn path_qualified(
self: &mut PrintCx<'_, '_, 'tcx, Self>,
impl_prefix: Option<Self::Path>,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
_ns: Namespace,
) -> Self::Path {
let mut path = impl_prefix.unwrap_or(vec![]);
// This shouldn't ever be needed, but just in case:
if let Some(trait_ref) = trait_ref {
vec![format!("{:?}", trait_ref)]
path.push(format!("{:?}", trait_ref));
} else {
vec![format!("<{}>", self_ty)]
path.push(format!("<{}>", self_ty));
}
path
}
fn path_append(
self: &mut PrintCx<'_, '_, '_, Self>,