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

Only show notable traits if both types are the same #91366

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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."
notriddle marked this conversation as resolved.
Show resolved Hide resolved
/// This is different from Eq, because it knows that things like
notriddle marked this conversation as resolved.
Show resolved Hide resolved
/// `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<PrimitiveType> {
match *self {
Primitive(p) | BorrowedRef { type_: box Primitive(p), .. } => Some(p),
Expand Down
9 changes: 8 additions & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
20 changes: 20 additions & 0 deletions src/test/rustdoc/doc-notable_trait-slice.rs
Original file line number Diff line number Diff line change
@@ -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] {
&[]
}