From 4e7a2dcabb0aa42f297284ead946d5787027d035 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Wed, 2 Dec 2020 10:07:29 -0500 Subject: [PATCH] Use `item_name` instead of pretty printing Pretty printing would add a `r#` prefix to raw identifiers, which was not correct. In general I think this change makes sense - pretty-printing is for showing to the *user*, `item_name` is suitable to pass to resolve. --- src/librustdoc/passes/collect_intra_doc_links.rs | 13 +++++++++---- src/test/rustdoc/intra-doc/raw-ident-self.rs | 13 +++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 src/test/rustdoc/intra-doc/raw-ident-self.rs diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 3dcc0f240a3f8..551c086a8d4e3 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -847,12 +847,17 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { // FIXME(jynelson): this shouldn't go through stringification, rustdoc should just use the DefId directly let self_name = self_id.and_then(|self_id| { + use ty::TyKind; if matches!(self.cx.tcx.def_kind(self_id), DefKind::Impl) { - // using `ty.to_string()` directly has issues with shortening paths + // using `ty.to_string()` (or any variant) has issues with raw idents let ty = self.cx.tcx.type_of(self_id); - let name = ty::print::with_crate_prefix(|| ty.to_string()); - debug!("using type_of(): {}", name); - Some(name) + let name = match ty.kind() { + TyKind::Adt(def, _) => Some(self.cx.tcx.item_name(def.did).to_string()), + other if other.is_primitive() => Some(ty.to_string()), + _ => None, + }; + debug!("using type_of(): {:?}", name); + name } else { let name = self.cx.tcx.opt_item_name(self_id).map(|sym| sym.to_string()); debug!("using item_name(): {:?}", name); diff --git a/src/test/rustdoc/intra-doc/raw-ident-self.rs b/src/test/rustdoc/intra-doc/raw-ident-self.rs new file mode 100644 index 0000000000000..d289797f14652 --- /dev/null +++ b/src/test/rustdoc/intra-doc/raw-ident-self.rs @@ -0,0 +1,13 @@ +#![deny(broken_intra_doc_links)] +pub mod r#impl { + pub struct S; + + impl S { + /// See [Self::b]. + // @has raw_ident_self/impl/struct.S.html + // @has - '//a[@href="../../raw_ident_self/impl/struct.S.html#method.b"]' 'Self::b' + pub fn a() {} + + pub fn b() {} + } +}