Skip to content

Commit 039f513

Browse files
committed
rustc: move <...>-less impl path special-case to pretty_path_qualified.
1 parent 3926829 commit 039f513

File tree

4 files changed

+43
-46
lines changed

4 files changed

+43
-46
lines changed

src/librustc/ty/print.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,9 @@ pub trait Printer: Sized {
173173
self: &mut PrintCx<'_, '_, 'tcx, Self>,
174174
self_ty: Ty<'tcx>,
175175
trait_ref: Option<ty::TraitRef<'tcx>>,
176+
ns: Namespace,
176177
) -> Self::Path;
177178
#[must_use]
178-
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path;
179-
#[must_use]
180179
fn path_append(
181180
self: &mut PrintCx<'_, '_, '_, Self>,
182181
path: Self::Path,
@@ -290,7 +289,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
290289
parent_generics.has_self && parent_generics.parent_count == 0;
291290
if let (Some(substs), true) = (substs, parent_has_own_self) {
292291
let trait_ref = ty::TraitRef::new(parent_def_id, substs);
293-
self.path_qualified(trait_ref.self_ty(), Some(trait_ref))
292+
self.path_qualified(trait_ref.self_ty(), Some(trait_ref), ns)
294293
} else {
295294
self.print_def_path(parent_def_id, substs, ns, iter::empty())
296295
}
@@ -366,35 +365,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
366365

367366
// Otherwise, try to give a good form that would be valid language
368367
// syntax. Preferably using associated item notation.
369-
370-
if let Some(trait_ref) = impl_trait_ref {
371-
// Trait impls.
372-
return self.path_qualified(self_ty, Some(trait_ref));
373-
}
374-
375-
// Inherent impls. Try to print `Foo::bar` for an inherent
376-
// impl on `Foo`, but fallback to `<Foo>::bar` if self-type is
377-
// anything other than a simple path.
378-
match self_ty.sty {
379-
ty::Adt(adt_def, substs) => {
380-
self.print_def_path(adt_def.did, Some(substs), ns, iter::empty())
381-
}
382-
383-
ty::Foreign(did) => self.print_def_path(did, None, ns, iter::empty()),
384-
385-
ty::Bool |
386-
ty::Char |
387-
ty::Int(_) |
388-
ty::Uint(_) |
389-
ty::Float(_) |
390-
ty::Str => {
391-
self.path_impl(&self_ty.to_string())
392-
}
393-
394-
_ => {
395-
self.path_qualified(self_ty, None)
396-
}
397-
}
368+
self.path_qualified(self_ty, impl_trait_ref, ns)
398369
}
399370
}
400371

@@ -586,7 +557,30 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
586557
&mut self,
587558
self_ty: Ty<'tcx>,
588559
trait_ref: Option<ty::TraitRef<'tcx>>,
560+
ns: Namespace,
589561
) -> P::Path {
562+
if trait_ref.is_none() {
563+
// Inherent impls. Try to print `Foo::bar` for an inherent
564+
// impl on `Foo`, but fallback to `<Foo>::bar` if self-type is
565+
// anything other than a simple path.
566+
match self_ty.sty {
567+
ty::Adt(adt_def, substs) => {
568+
return self.print_def_path(adt_def.did, Some(substs), ns, iter::empty());
569+
}
570+
ty::Foreign(did) => {
571+
return self.print_def_path(did, None, ns, iter::empty());
572+
}
573+
574+
ty::Bool | ty::Char | ty::Str |
575+
ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
576+
self_ty.print_display(self)?;
577+
return Ok(PrettyPath { empty: false });
578+
}
579+
580+
_ => {}
581+
}
582+
}
583+
590584
write!(self.printer, "<")?;
591585
self_ty.print_display(self)?;
592586
if let Some(trait_ref) = trait_ref {
@@ -775,12 +769,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
775769
self: &mut PrintCx<'_, '_, 'tcx, Self>,
776770
self_ty: Ty<'tcx>,
777771
trait_ref: Option<ty::TraitRef<'tcx>>,
772+
ns: Namespace,
778773
) -> Self::Path {
779-
self.pretty_path_qualified(self_ty, trait_ref)
780-
}
781-
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
782-
write!(self.printer, "{}", text)?;
783-
Ok(PrettyPath { empty: false })
774+
self.pretty_path_qualified(self_ty, trait_ref, ns)
784775
}
785776
fn path_append(
786777
self: &mut PrintCx<'_, '_, '_, Self>,

src/librustc/util/ppaux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ define_print! {
10001000
Ok(())
10011001
}
10021002
debug {
1003-
let _ = cx.path_qualified(self.self_ty(), Some(*self))?;
1003+
let _ = cx.path_qualified(self.self_ty(), Some(*self), Namespace::TypeNS)?;
10041004
Ok(())
10051005
}
10061006
}

src/librustc_codegen_utils/symbol_names.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,16 +417,24 @@ impl Printer for SymbolPath {
417417
self: &mut PrintCx<'_, '_, 'tcx, Self>,
418418
self_ty: Ty<'tcx>,
419419
trait_ref: Option<ty::TraitRef<'tcx>>,
420+
ns: Namespace,
420421
) -> Self::Path {
422+
// HACK(eddyb) avoid `keep_within_component` for the cases
423+
// that print without `<...>` around `self_ty`.
424+
match self_ty.sty {
425+
ty::Adt(..) | ty::Foreign(_) |
426+
ty::Bool | ty::Char | ty::Str |
427+
ty::Int(_) | ty::Uint(_) | ty::Float(_) if trait_ref.is_none() => {
428+
return self.pretty_path_qualified(self_ty, trait_ref, ns);
429+
}
430+
_ => {}
431+
}
432+
421433
let kept_within_component = mem::replace(&mut self.printer.keep_within_component, true);
422-
let r = self.pretty_path_qualified(self_ty, trait_ref);
434+
let r = self.pretty_path_qualified(self_ty, trait_ref, ns);
423435
self.printer.keep_within_component = kept_within_component;
424436
r
425437
}
426-
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
427-
self.printer.write_str(text)?;
428-
Ok(PrettyPath { empty: false })
429-
}
430438
fn path_append(
431439
self: &mut PrintCx<'_, '_, '_, Self>,
432440
_: Self::Path,

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,6 +4059,7 @@ where F: Fn(DefId) -> Def {
40594059
self: &mut PrintCx<'_, '_, 'tcx, Self>,
40604060
self_ty: Ty<'tcx>,
40614061
trait_ref: Option<ty::TraitRef<'tcx>>,
4062+
_ns: Namespace,
40624063
) -> Self::Path {
40634064
// This shouldn't ever be needed, but just in case:
40644065
if let Some(trait_ref) = trait_ref {
@@ -4067,9 +4068,6 @@ where F: Fn(DefId) -> Def {
40674068
vec![format!("<{}>", self_ty)]
40684069
}
40694070
}
4070-
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
4071-
vec![text.to_string()]
4072-
}
40734071
fn path_append(
40744072
self: &mut PrintCx<'_, '_, '_, Self>,
40754073
mut path: Self::Path,

0 commit comments

Comments
 (0)