Skip to content

Commit aec5a48

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

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,
@@ -291,7 +290,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
291290
parent_generics.has_self && parent_generics.parent_count == 0;
292291
if let (Some(substs), true) = (substs, parent_has_own_self) {
293292
let trait_ref = ty::TraitRef::new(parent_def_id, substs);
294-
self.path_qualified(trait_ref.self_ty(), Some(trait_ref))
293+
self.path_qualified(trait_ref.self_ty(), Some(trait_ref), ns)
295294
} else {
296295
self.print_def_path(parent_def_id, substs, ns, iter::empty())
297296
}
@@ -367,35 +366,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
367366

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

@@ -587,7 +558,30 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
587558
&mut self,
588559
self_ty: Ty<'tcx>,
589560
trait_ref: Option<ty::TraitRef<'tcx>>,
561+
ns: Namespace,
590562
) -> P::Path {
563+
if trait_ref.is_none() {
564+
// Inherent impls. Try to print `Foo::bar` for an inherent
565+
// impl on `Foo`, but fallback to `<Foo>::bar` if self-type is
566+
// anything other than a simple path.
567+
match self_ty.sty {
568+
ty::Adt(adt_def, substs) => {
569+
return self.print_def_path(adt_def.did, Some(substs), ns, iter::empty());
570+
}
571+
ty::Foreign(did) => {
572+
return self.print_def_path(did, None, ns, iter::empty());
573+
}
574+
575+
ty::Bool | ty::Char | ty::Str |
576+
ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
577+
self_ty.print_display(self)?;
578+
return Ok(PrettyPath { empty: false });
579+
}
580+
581+
_ => {}
582+
}
583+
}
584+
591585
write!(self.printer, "<")?;
592586
self_ty.print_display(self)?;
593587
if let Some(trait_ref) = trait_ref {
@@ -781,12 +775,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
781775
self: &mut PrintCx<'_, '_, 'tcx, Self>,
782776
self_ty: Ty<'tcx>,
783777
trait_ref: Option<ty::TraitRef<'tcx>>,
778+
ns: Namespace,
784779
) -> Self::Path {
785-
self.pretty_path_qualified(self_ty, trait_ref)
786-
}
787-
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
788-
write!(self.printer, "{}", text)?;
789-
Ok(PrettyPath { empty: false })
780+
self.pretty_path_qualified(self_ty, trait_ref, ns)
790781
}
791782
fn path_append(
792783
self: &mut PrintCx<'_, '_, '_, Self>,

src/librustc/util/ppaux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ define_print! {
10071007
Ok(())
10081008
}
10091009
debug {
1010-
let _ = cx.path_qualified(self.self_ty(), Some(*self))?;
1010+
let _ = cx.path_qualified(self.self_ty(), Some(*self), Namespace::TypeNS)?;
10111011
Ok(())
10121012
}
10131013
}

src/librustc_codegen_utils/symbol_names.rs

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

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4239,6 +4239,7 @@ where F: Fn(DefId) -> Def {
42394239
self: &mut PrintCx<'_, '_, 'tcx, Self>,
42404240
self_ty: Ty<'tcx>,
42414241
trait_ref: Option<ty::TraitRef<'tcx>>,
4242+
_ns: Namespace,
42424243
) -> Self::Path {
42434244
// This shouldn't ever be needed, but just in case:
42444245
if let Some(trait_ref) = trait_ref {
@@ -4247,9 +4248,6 @@ where F: Fn(DefId) -> Def {
42474248
vec![format!("<{}>", self_ty)]
42484249
}
42494250
}
4250-
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
4251-
vec![text.to_string()]
4252-
}
42534251
fn path_append(
42544252
self: &mut PrintCx<'_, '_, '_, Self>,
42554253
mut path: Self::Path,

0 commit comments

Comments
 (0)