Skip to content

Commit e8fb697

Browse files
committed
rustc: move ...::<impl ...> printing into pretty_path_qualified.
1 parent bbedb51 commit e8fb697

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

src/librustc/ty/print.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ pub trait Printer: Sized {
173173
#[must_use]
174174
fn path_qualified(
175175
self: &mut PrintCx<'_, '_, 'tcx, Self>,
176+
impl_prefix: Option<Self::Path>,
176177
self_ty: Ty<'tcx>,
177178
trait_ref: Option<ty::TraitRef<'tcx>>,
178179
ns: Namespace,
@@ -300,7 +301,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
300301
parent_generics.has_self && parent_generics.parent_count == 0;
301302
if let (Some(substs), true) = (substs, parent_has_own_self) {
302303
let trait_ref = ty::TraitRef::new(parent_def_id, substs);
303-
self.path_qualified(trait_ref.self_ty(), Some(trait_ref), ns)
304+
self.path_qualified(None, trait_ref.self_ty(), Some(trait_ref), ns)
304305
} else {
305306
self.print_def_path(parent_def_id, substs, ns, iter::empty())
306307
}
@@ -356,21 +357,18 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
356357
Some(trait_ref) => self.tcx.parent(trait_ref.def_id) == Some(parent_def_id),
357358
};
358359

359-
if !in_self_mod && !in_trait_mod {
360+
let prefix_path = if !in_self_mod && !in_trait_mod {
360361
// If the impl is not co-located with either self-type or
361362
// trait-type, then fallback to a format that identifies
362363
// the module more clearly.
363-
let path = self.print_def_path(parent_def_id, None, ns, iter::empty());
364-
if let Some(trait_ref) = impl_trait_ref {
365-
return self.path_append(path, &format!("<impl {} for {}>", trait_ref, self_ty));
366-
} else {
367-
return self.path_append(path, &format!("<impl {}>", self_ty));
368-
}
369-
}
364+
Some(self.print_def_path(parent_def_id, None, ns, iter::empty()))
365+
} else {
366+
// Otherwise, try to give a good form that would be valid language
367+
// syntax. Preferably using associated item notation.
368+
None
369+
};
370370

371-
// Otherwise, try to give a good form that would be valid language
372-
// syntax. Preferably using associated item notation.
373-
self.path_qualified(self_ty, impl_trait_ref, ns)
371+
self.path_qualified(prefix_path, self_ty, impl_trait_ref, ns)
374372
}
375373
}
376374

@@ -560,10 +558,24 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
560558

561559
pub fn pretty_path_qualified(
562560
&mut self,
561+
impl_prefix: Option<P::Path>,
563562
self_ty: Ty<'tcx>,
564563
trait_ref: Option<ty::TraitRef<'tcx>>,
565564
ns: Namespace,
566565
) -> P::Path {
566+
if let Some(prefix) = impl_prefix {
567+
// HACK(eddyb) going through `path_append` means symbol name
568+
// computation gets to handle its equivalent of `::` correctly.
569+
let _ = self.path_append(prefix, "<impl ")?;
570+
if let Some(trait_ref) = trait_ref {
571+
trait_ref.print_display(self)?;
572+
write!(self.printer, " for ")?;
573+
}
574+
self_ty.print_display(self)?;
575+
write!(self.printer, ">")?;
576+
return Ok(PrettyPath { empty: false });
577+
}
578+
567579
if trait_ref.is_none() {
568580
// Inherent impls. Try to print `Foo::bar` for an inherent
569581
// impl on `Foo`, but fallback to `<Foo>::bar` if self-type is
@@ -768,11 +780,12 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
768780
}
769781
fn path_qualified(
770782
self: &mut PrintCx<'_, '_, 'tcx, Self>,
783+
impl_prefix: Option<Self::Path>,
771784
self_ty: Ty<'tcx>,
772785
trait_ref: Option<ty::TraitRef<'tcx>>,
773786
ns: Namespace,
774787
) -> Self::Path {
775-
self.pretty_path_qualified(self_ty, trait_ref, ns)
788+
self.pretty_path_qualified(impl_prefix, self_ty, trait_ref, ns)
776789
}
777790
fn path_append(
778791
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), Namespace::TypeNS)?;
1003+
let _ = cx.path_qualified(None, self.self_ty(), Some(*self), Namespace::TypeNS)?;
10041004
Ok(())
10051005
}
10061006
}

src/librustc_codegen_utils/symbol_names.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl SymbolPath {
382382
}
383383

384384
fn finalize_pending_component(&mut self) {
385-
if !self.keep_within_component && !self.temp_buf.is_empty() {
385+
if !self.temp_buf.is_empty() {
386386
let _ = write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);
387387
self.temp_buf.clear();
388388
}
@@ -415,6 +415,7 @@ impl Printer for SymbolPath {
415415
}
416416
fn path_qualified(
417417
self: &mut PrintCx<'_, '_, 'tcx, Self>,
418+
impl_prefix: Option<Self::Path>,
418419
self_ty: Ty<'tcx>,
419420
trait_ref: Option<ty::TraitRef<'tcx>>,
420421
ns: Namespace,
@@ -424,25 +425,51 @@ impl Printer for SymbolPath {
424425
match self_ty.sty {
425426
ty::Adt(..) | ty::Foreign(_) |
426427
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);
428+
ty::Int(_) | ty::Uint(_) | ty::Float(_)
429+
if impl_prefix.is_none() && trait_ref.is_none() =>
430+
{
431+
return self.pretty_path_qualified(None, self_ty, trait_ref, ns);
429432
}
430433
_ => {}
431434
}
432435

436+
// HACK(eddyb) make sure to finalize the last component of the
437+
// `impl` prefix, to avoid it fusing with the following text.
438+
let impl_prefix = impl_prefix.map(|prefix| {
439+
let mut prefix = self.path_append(prefix, "")?;
440+
441+
// HACK(eddyb) also avoid an unnecessary `::`.
442+
prefix.empty = true;
443+
444+
Ok(prefix)
445+
});
446+
433447
let kept_within_component = mem::replace(&mut self.printer.keep_within_component, true);
434-
let r = self.pretty_path_qualified(self_ty, trait_ref, ns);
448+
let r = self.pretty_path_qualified(impl_prefix, self_ty, trait_ref, ns);
435449
self.printer.keep_within_component = kept_within_component;
436450
r
437451
}
438452
fn path_append(
439453
self: &mut PrintCx<'_, '_, '_, Self>,
440-
_: Self::Path,
454+
path: Self::Path,
441455
text: &str,
442456
) -> Self::Path {
443-
self.printer.finalize_pending_component();
457+
let mut path = path?;
458+
459+
if self.keep_within_component {
460+
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
461+
if !path.empty {
462+
self.printer.write_str("::")?;
463+
} else {
464+
path.empty = text.is_empty();
465+
}
466+
} else {
467+
self.printer.finalize_pending_component();
468+
path.empty = false;
469+
}
470+
444471
self.printer.write_str(text)?;
445-
Ok(PrettyPath { empty: false })
472+
Ok(path)
446473
}
447474
fn path_generic_args(
448475
self: &mut PrintCx<'_, '_, 'tcx, Self>,

src/librustdoc/clean/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4057,16 +4057,21 @@ where F: Fn(DefId) -> Def {
40574057
}
40584058
fn path_qualified(
40594059
self: &mut PrintCx<'_, '_, 'tcx, Self>,
4060+
impl_prefix: Option<Self::Path>,
40604061
self_ty: Ty<'tcx>,
40614062
trait_ref: Option<ty::TraitRef<'tcx>>,
40624063
_ns: Namespace,
40634064
) -> Self::Path {
4065+
let mut path = impl_prefix.unwrap_or(vec![]);
4066+
40644067
// This shouldn't ever be needed, but just in case:
40654068
if let Some(trait_ref) = trait_ref {
4066-
vec![format!("{:?}", trait_ref)]
4069+
path.push(format!("{:?}", trait_ref));
40674070
} else {
4068-
vec![format!("<{}>", self_ty)]
4071+
path.push(format!("<{}>", self_ty));
40694072
}
4073+
4074+
path
40704075
}
40714076
fn path_append(
40724077
self: &mut PrintCx<'_, '_, '_, Self>,

0 commit comments

Comments
 (0)