Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: fix intra-link for generic trait impls #92792

Merged
merged 2 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,18 @@ fn traits_implemented_by<'a>(
ty
);
// Fast path: if this is a primitive simple `==` will work
mdibaiee marked this conversation as resolved.
Show resolved Hide resolved
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, _)) => {
camelid marked this conversation as resolved.
Show resolved Hide resolved
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 }
})
Expand Down
24 changes: 22 additions & 2 deletions src/test/rustdoc/intra-doc/extern-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@ extern {
pub type ExternType;
}

pub trait T {
fn test(&self) {}
}

pub trait G<N> {
fn g(&self, n: N) {}
}

impl ExternType {
pub fn f(&self) {
pub fn f(&self) {}
}

}
impl T for ExternType {
fn test(&self) {}
}

impl G<usize> 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() {}
20 changes: 20 additions & 0 deletions src/test/rustdoc/intra-doc/generic-trait-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![deny(rustdoc::broken_intra_doc_links)]

// Test intra-doc links on trait implementations with generics
mdibaiee marked this conversation as resolved.
Show resolved Hide resolved
// regression test for issue #92662

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() {}