Skip to content

Commit 2b8259a

Browse files
authored
Rollup merge of #91366 - notriddle:notriddle/only-show-notable-if-types-are-the-same, r=GuillaumeGomez
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
2 parents bc929f9 + d4f71d8 commit 2b8259a

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

Diff for: src/librustdoc/clean/types.rs

+39
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,45 @@ crate enum Type {
14681468
rustc_data_structures::static_assert_size!(Type, 72);
14691469

14701470
impl Type {
1471+
/// When comparing types for equality, it can help to ignore `&` wrapping.
1472+
crate fn without_borrowed_ref(&self) -> &Type {
1473+
let mut result = self;
1474+
while let Type::BorrowedRef { type_, .. } = result {
1475+
result = &*type_;
1476+
}
1477+
result
1478+
}
1479+
1480+
/// Check if two types are "potentially the same".
1481+
/// This is different from `Eq`, because it knows that things like
1482+
/// `Placeholder` are possible matches for everything.
1483+
crate fn is_same(&self, other: &Self, cache: &Cache) -> bool {
1484+
match (self, other) {
1485+
// Recursive cases.
1486+
(Type::Tuple(a), Type::Tuple(b)) => {
1487+
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| a.is_same(&b, cache))
1488+
}
1489+
(Type::Slice(a), Type::Slice(b)) => a.is_same(&b, cache),
1490+
(Type::Array(a, al), Type::Array(b, bl)) => al == bl && a.is_same(&b, cache),
1491+
(Type::RawPointer(mutability, type_), Type::RawPointer(b_mutability, b_type_)) => {
1492+
mutability == b_mutability && type_.is_same(&b_type_, cache)
1493+
}
1494+
(
1495+
Type::BorrowedRef { mutability, type_, .. },
1496+
Type::BorrowedRef { mutability: b_mutability, type_: b_type_, .. },
1497+
) => mutability == b_mutability && type_.is_same(&b_type_, cache),
1498+
// Placeholders and generics are equal to all other types.
1499+
(Type::Infer, _) | (_, Type::Infer) => true,
1500+
(Type::Generic(_), _) | (_, Type::Generic(_)) => true,
1501+
// Other cases, such as primitives, just use recursion.
1502+
(a, b) => a
1503+
.def_id(cache)
1504+
.and_then(|a| Some((a, b.def_id(cache)?)))
1505+
.map(|(a, b)| a == b)
1506+
.unwrap_or(false),
1507+
}
1508+
}
1509+
14711510
crate fn primitive_type(&self) -> Option<PrimitiveType> {
14721511
match *self {
14731512
Primitive(p) | BorrowedRef { type_: box Primitive(p), .. } => Some(p),

Diff for: src/librustdoc/html/render/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1235,10 +1235,17 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
12351235
fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
12361236
let mut out = Buffer::html();
12371237

1238-
if let Some(did) = decl.output.as_return().and_then(|t| t.def_id(cx.cache())) {
1238+
if let Some((did, ty)) = decl.output.as_return().and_then(|t| Some((t.def_id(cx.cache())?, t)))
1239+
{
12391240
if let Some(impls) = cx.cache().impls.get(&did) {
12401241
for i in impls {
12411242
let impl_ = i.inner_impl();
1243+
if !impl_.for_.without_borrowed_ref().is_same(ty.without_borrowed_ref(), cx.cache())
1244+
{
1245+
// Two different types might have the same did,
1246+
// without actually being the same.
1247+
continue;
1248+
}
12421249
if let Some(trait_) = &impl_.trait_ {
12431250
let trait_did = trait_.def_id();
12441251

Diff for: src/test/rustdoc/doc-notable_trait-slice.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![feature(doc_notable_trait)]
2+
3+
#[doc(notable_trait)]
4+
pub trait SomeTrait {}
5+
6+
pub struct SomeStruct;
7+
pub struct OtherStruct;
8+
impl SomeTrait for &[SomeStruct] {}
9+
10+
// @has doc_notable_trait_slice/fn.bare_fn_matches.html
11+
// @has - '//code[@class="content"]' 'impl SomeTrait for &[SomeStruct]'
12+
pub fn bare_fn_matches() -> &'static [SomeStruct] {
13+
&[]
14+
}
15+
16+
// @has doc_notable_trait_slice/fn.bare_fn_no_matches.html
17+
// @!has - '//code[@class="content"]' 'impl SomeTrait for &[SomeStruct]'
18+
pub fn bare_fn_no_matches() -> &'static [OtherStruct] {
19+
&[]
20+
}

0 commit comments

Comments
 (0)