]> git.lizzy.rs Git - rust.git/commitdiff
rustdoc: fix intra-link for generic trait impls
authorMahdi Dibaiee <mdibaiee@pm.me>
Tue, 11 Jan 2022 22:20:01 +0000 (22:20 +0000)
committerMahdi Dibaiee <mdibaiee@pm.me>
Thu, 13 Jan 2022 20:23:24 +0000 (20:23 +0000)
src/librustdoc/passes/collect_intra_doc_links.rs
src/test/rustdoc/intra-doc/generic-trait-impl.rs [new file with mode: 0644]

index 9d1a8b3f80fec09da93a8d117b425d6f0eaccb1a..0bc10404dc0f0111a52a8e22e316c76aade277a1 100644 (file)
@@ -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<T> (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 (file)
index 0000000..c3d3f8b
--- /dev/null
@@ -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<T> {
+    fn bar(&self);
+}
+
+pub struct Foo<U>(PhantomData<U>);
+
+impl<T, U> Bar<T> for Foo<U> {
+    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() {}