From 85ba6c7b3489f5b23ca146d6ebde657bf8b52c36 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 29 Nov 2021 12:18:57 -0700 Subject: [PATCH 1/3] Only show notable traits if both types are the same Checking only their DefId doesn't work because all slices have the same fake DefId. Fixes #91347 --- src/librustdoc/clean/types.rs | 39 +++++++++++++++++++++ src/librustdoc/html/render/mod.rs | 9 ++++- src/test/rustdoc/doc-notable_trait-slice.rs | 20 +++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/test/rustdoc/doc-notable_trait-slice.rs diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 37acf68defd25..f7762fd2521ff 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1468,6 +1468,45 @@ crate enum Type { rustc_data_structures::static_assert_size!(Type, 72); impl Type { + /// When comparing types for equality, it can help to ignore `&` wrapping. + crate fn without_borrowed_ref(&self) -> &Type { + let mut result = self; + while let Type::BorrowedRef { type_, .. } = result { + result = &*type_; + } + result + } + + /// Check if two types are "potentially the same." + /// This is different from Eq, because it knows that things like + /// `Placeholder` are possible matches for everything. + crate fn is_same(&self, other: &Self, cache: &Cache) -> bool { + match (self, other) { + // Recursive cases. + (Type::Tuple(a), Type::Tuple(b)) => { + a.len() == b.len() && a.iter().zip(b).all(|(a, b)| a.is_same(&b, cache)) + } + (Type::Slice(a), Type::Slice(b)) => a.is_same(&b, cache), + (Type::Array(a, al), Type::Array(b, bl)) => al == bl && a.is_same(&b, cache), + (Type::RawPointer(mutability, type_), Type::RawPointer(b_mutability, b_type_)) => { + mutability == b_mutability && type_.is_same(&b_type_, cache) + } + ( + Type::BorrowedRef { mutability, type_, .. }, + Type::BorrowedRef { mutability: b_mutability, type_: b_type_, .. }, + ) => mutability == b_mutability && type_.is_same(&b_type_, cache), + // Placeholders and generics are equal to all other types. + (Type::Infer, _) | (_, Type::Infer) => true, + (Type::Generic(_), _) | (_, Type::Generic(_)) => true, + // Other cases, such as primitives, just use recursion. + (a, b) => a + .def_id(cache) + .and_then(|a| Some((a, b.def_id(cache)?))) + .map(|(a, b)| a == b) + .unwrap_or(false), + } + } + crate fn primitive_type(&self) -> Option { match *self { Primitive(p) | BorrowedRef { type_: box Primitive(p), .. } => Some(p), diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 08022d526fefb..8bf7d0416dd0a 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1235,10 +1235,17 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) -> fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String { let mut out = Buffer::html(); - if let Some(did) = decl.output.as_return().and_then(|t| t.def_id(cx.cache())) { + if let Some((did, ty)) = decl.output.as_return().and_then(|t| Some((t.def_id(cx.cache())?, t))) + { if let Some(impls) = cx.cache().impls.get(&did) { for i in impls { let impl_ = i.inner_impl(); + if !impl_.for_.without_borrowed_ref().is_same(ty.without_borrowed_ref(), cx.cache()) + { + // Two different types might have the same did, + // without actually being the same. + continue; + } if let Some(trait_) = &impl_.trait_ { let trait_did = trait_.def_id(); diff --git a/src/test/rustdoc/doc-notable_trait-slice.rs b/src/test/rustdoc/doc-notable_trait-slice.rs new file mode 100644 index 0000000000000..b0d414027216a --- /dev/null +++ b/src/test/rustdoc/doc-notable_trait-slice.rs @@ -0,0 +1,20 @@ +#![feature(doc_notable_trait)] + +#[doc(notable_trait)] +pub trait SomeTrait {} + +pub struct SomeStruct; +pub struct OtherStruct; +impl SomeTrait for &[SomeStruct] {} + +// @has doc_notable_trait_slice/fn.bare_fn_matches.html +// @has - '//code[@class="content"]' 'impl SomeTrait for &[SomeStruct]' +pub fn bare_fn_matches() -> &'static [SomeStruct] { + &[] +} + +// @has doc_notable_trait_slice/fn.bare_fn_no_matches.html +// @!has - '//code[@class="content"]' 'impl SomeTrait for &[SomeStruct]' +pub fn bare_fn_no_matches() -> &'static [OtherStruct] { + &[] +} From 7bb50d4f309ae0dcf6264e31815777dc5eb3ebaf Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 30 Nov 2021 14:22:03 -0700 Subject: [PATCH 2/3] Update src/librustdoc/clean/types.rs Co-authored-by: Guillaume Gomez --- src/librustdoc/clean/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index f7762fd2521ff..a6552bd010b96 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1477,7 +1477,7 @@ impl Type { result } - /// Check if two types are "potentially the same." + /// Check if two types are "potentially the same". /// This is different from Eq, because it knows that things like /// `Placeholder` are possible matches for everything. crate fn is_same(&self, other: &Self, cache: &Cache) -> bool { From d4f71d8f86ec00c736b121b22284298ecaf9a6e5 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 30 Nov 2021 14:22:15 -0700 Subject: [PATCH 3/3] Update src/librustdoc/clean/types.rs Co-authored-by: Guillaume Gomez --- src/librustdoc/clean/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index a6552bd010b96..11dd140504ae3 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1478,7 +1478,7 @@ impl Type { } /// Check if two types are "potentially the same". - /// This is different from Eq, because it knows that things like + /// This is different from `Eq`, because it knows that things like /// `Placeholder` are possible matches for everything. crate fn is_same(&self, other: &Self, cache: &Cache) -> bool { match (self, other) {