Skip to content

Commit df6650f

Browse files
committed
rustc: move ...::<impl ...> printing into pretty_path_qualified.
1 parent 39fd54a commit df6650f

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,
@@ -301,7 +302,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
301302
parent_generics.has_self && parent_generics.parent_count == 0;
302303
if let (Some(substs), true) = (substs, parent_has_own_self) {
303304
let trait_ref = ty::TraitRef::new(parent_def_id, substs);
304-
self.path_qualified(trait_ref.self_ty(), Some(trait_ref), ns)
305+
self.path_qualified(None, trait_ref.self_ty(), Some(trait_ref), ns)
305306
} else {
306307
self.print_def_path(parent_def_id, substs, ns, iter::empty())
307308
}
@@ -357,21 +358,18 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
357358
Some(trait_ref) => self.tcx.parent(trait_ref.def_id) == Some(parent_def_id),
358359
};
359360

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

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

@@ -561,10 +559,24 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
561559

562560
pub fn pretty_path_qualified(
563561
&mut self,
562+
impl_prefix: Option<P::Path>,
564563
self_ty: Ty<'tcx>,
565564
trait_ref: Option<ty::TraitRef<'tcx>>,
566565
ns: Namespace,
567566
) -> P::Path {
567+
if let Some(prefix) = impl_prefix {
568+
// HACK(eddyb) going through `path_append` means symbol name
569+
// computation gets to handle its equivalent of `::` correctly.
570+
let _ = self.path_append(prefix, "<impl ")?;
571+
if let Some(trait_ref) = trait_ref {
572+
trait_ref.print_display(self)?;
573+
write!(self.printer, " for ")?;
574+
}
575+
self_ty.print_display(self)?;
576+
write!(self.printer, ">")?;
577+
return Ok(PrettyPath { empty: false });
578+
}
579+
568580
if trait_ref.is_none() {
569581
// Inherent impls. Try to print `Foo::bar` for an inherent
570582
// impl on `Foo`, but fallback to `<Foo>::bar` if self-type is
@@ -774,11 +786,12 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
774786
}
775787
fn path_qualified(
776788
self: &mut PrintCx<'_, '_, 'tcx, Self>,
789+
impl_prefix: Option<Self::Path>,
777790
self_ty: Ty<'tcx>,
778791
trait_ref: Option<ty::TraitRef<'tcx>>,
779792
ns: Namespace,
780793
) -> Self::Path {
781-
self.pretty_path_qualified(self_ty, trait_ref, ns)
794+
self.pretty_path_qualified(impl_prefix, self_ty, trait_ref, ns)
782795
}
783796
fn path_append(
784797
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), Namespace::TypeNS)?;
1010+
let _ = cx.path_qualified(None, self.self_ty(), Some(*self), Namespace::TypeNS)?;
10111011
Ok(())
10121012
}
10131013
}

src/librustc_codegen_utils/symbol_names.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl SymbolPath {
387387
}
388388

389389
fn finalize_pending_component(&mut self) {
390-
if !self.keep_within_component && !self.temp_buf.is_empty() {
390+
if !self.temp_buf.is_empty() {
391391
let _ = write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);
392392
self.temp_buf.clear();
393393
}
@@ -414,6 +414,7 @@ impl Printer for SymbolPath {
414414
}
415415
fn path_qualified(
416416
self: &mut PrintCx<'_, '_, 'tcx, Self>,
417+
impl_prefix: Option<Self::Path>,
417418
self_ty: Ty<'tcx>,
418419
trait_ref: Option<ty::TraitRef<'tcx>>,
419420
ns: Namespace,
@@ -423,25 +424,51 @@ impl Printer for SymbolPath {
423424
match self_ty.sty {
424425
ty::Adt(..) | ty::Foreign(_) |
425426
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);
427+
ty::Int(_) | ty::Uint(_) | ty::Float(_)
428+
if impl_prefix.is_none() && trait_ref.is_none() =>
429+
{
430+
return self.pretty_path_qualified(None, self_ty, trait_ref, ns);
428431
}
429432
_ => {}
430433
}
431434

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

src/librustdoc/clean/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4237,16 +4237,21 @@ where F: Fn(DefId) -> Def {
42374237
}
42384238
fn path_qualified(
42394239
self: &mut PrintCx<'_, '_, 'tcx, Self>,
4240+
impl_prefix: Option<Self::Path>,
42404241
self_ty: Ty<'tcx>,
42414242
trait_ref: Option<ty::TraitRef<'tcx>>,
42424243
_ns: Namespace,
42434244
) -> Self::Path {
4245+
let mut path = impl_prefix.unwrap_or(vec![]);
4246+
42444247
// This shouldn't ever be needed, but just in case:
42454248
if let Some(trait_ref) = trait_ref {
4246-
vec![format!("{:?}", trait_ref)]
4249+
path.push(format!("{:?}", trait_ref));
42474250
} else {
4248-
vec![format!("<{}>", self_ty)]
4251+
path.push(format!("<{}>", self_ty));
42494252
}
4253+
4254+
path
42504255
}
42514256
fn path_append(
42524257
self: &mut PrintCx<'_, '_, '_, Self>,

0 commit comments

Comments
 (0)