From 9ff8ae097e88937a4aa6cf62d73471e479bac1df Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Tue, 11 Jan 2022 22:20:01 +0000 Subject: [PATCH 1/2] rustdoc: fix intra-link for generic trait impls --- .../passes/collect_intra_doc_links.rs | 13 ++++++++++++- .../rustdoc/intra-doc/generic-trait-impl.rs | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/test/rustdoc/intra-doc/generic-trait-impl.rs diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 9d1a8b3f80fec..0bc10404dc0f0 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -903,7 +903,18 @@ fn traits_implemented_by<'a>( ty ); // Fast path: if this is a primitive simple `==` will work - let saw_impl = impl_type == ty; + // NOTE: the `match` is necessary; see #92662. + // this allows us to ignore generics because the user input + // may not include the generic placeholders + // e.g. this allows us to match Foo (user comment) with Foo (actual type) + let saw_impl = impl_type == ty + || match (impl_type.kind(), ty.kind()) { + (ty::Adt(impl_def, _), ty::Adt(ty_def, _)) => { + debug!("impl def_id: {:?}, ty def_id: {:?}", impl_def.did, ty_def.did); + impl_def.did == ty_def.did + } + _ => false, + }; if saw_impl { Some(trait_) } else { None } }) diff --git a/src/test/rustdoc/intra-doc/generic-trait-impl.rs b/src/test/rustdoc/intra-doc/generic-trait-impl.rs new file mode 100644 index 0000000000000..c3d3f8b386607 --- /dev/null +++ b/src/test/rustdoc/intra-doc/generic-trait-impl.rs @@ -0,0 +1,19 @@ +#![deny(rustdoc::broken_intra_doc_links)] + +// Test intra-doc links on trait implementations with generics + +use std::marker::PhantomData; + +pub trait Bar { + fn bar(&self); +} + +pub struct Foo(PhantomData); + +impl Bar for Foo { + fn bar(&self) {} +} + +// @has generic_trait_impl/fn.main.html '//a[@href="struct.Foo.html#method.bar"]' 'Foo::bar' +/// link to [`Foo::bar`] +pub fn main() {} From ae20500d76e36b5e8692af499830c4707f713bc0 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Thu, 13 Jan 2022 08:10:08 +0000 Subject: [PATCH 2/2] rustdoc: add intra-doc trait impl test for extern types --- src/test/rustdoc/intra-doc/extern-type.rs | 24 +++++++++++++++++-- .../rustdoc/intra-doc/generic-trait-impl.rs | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/test/rustdoc/intra-doc/extern-type.rs b/src/test/rustdoc/intra-doc/extern-type.rs index f37ae62dde1aa..ab088ab789d43 100644 --- a/src/test/rustdoc/intra-doc/extern-type.rs +++ b/src/test/rustdoc/intra-doc/extern-type.rs @@ -4,14 +4,34 @@ extern { pub type ExternType; } +pub trait T { + fn test(&self) {} +} + +pub trait G { + fn g(&self, n: N) {} +} + impl ExternType { - pub fn f(&self) { + pub fn f(&self) {} +} - } +impl T for ExternType { + fn test(&self) {} +} + +impl G for ExternType { + fn g(&self, n: usize) {} } // @has 'extern_type/foreigntype.ExternType.html' // @has 'extern_type/fn.links_to_extern_type.html' \ // 'href="foreigntype.ExternType.html#method.f"' +// @has 'extern_type/fn.links_to_extern_type.html' \ +// 'href="foreigntype.ExternType.html#method.test"' +// @has 'extern_type/fn.links_to_extern_type.html' \ +// 'href="foreigntype.ExternType.html#method.g"' /// See also [ExternType::f] +/// See also [ExternType::test] +/// See also [ExternType::g] pub fn links_to_extern_type() {} diff --git a/src/test/rustdoc/intra-doc/generic-trait-impl.rs b/src/test/rustdoc/intra-doc/generic-trait-impl.rs index c3d3f8b386607..ba8595abfa959 100644 --- a/src/test/rustdoc/intra-doc/generic-trait-impl.rs +++ b/src/test/rustdoc/intra-doc/generic-trait-impl.rs @@ -1,6 +1,7 @@ #![deny(rustdoc::broken_intra_doc_links)] // Test intra-doc links on trait implementations with generics +// regression test for issue #92662 use std::marker::PhantomData;