Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ee6b228

Browse files
committed
rustdoc: handle generics better when matching notable traits
This commit makes the `clean::Type::is_same` non-commutative, so that a generic `impl` matches a concrete return, but a generic return does not match a concrete `impl`. It makes slice and vector Write for `u8` not match on every generic return value.
1 parent c601585 commit ee6b228

File tree

5 files changed

+96
-6
lines changed

5 files changed

+96
-6
lines changed

src/librustdoc/clean/types.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,11 +1471,37 @@ impl Type {
14711471
result
14721472
}
14731473

1474-
/// Check if two types are "potentially the same".
1474+
pub(crate) fn is_borrowed_ref(&self) -> bool {
1475+
matches!(self, Type::BorrowedRef { .. })
1476+
}
1477+
1478+
/// Check if two types are "the same" for documentation purposes.
1479+
///
14751480
/// This is different from `Eq`, because it knows that things like
14761481
/// `Placeholder` are possible matches for everything.
1482+
///
1483+
/// This relation is not commutative when generics are involved:
1484+
///
1485+
/// ```ignore(private)
1486+
/// # // see types/tests.rs:is_same_generic for the real test
1487+
/// use rustdoc::format::cache::Cache;
1488+
/// use rustdoc::clean::types::{Type, PrimitiveType};
1489+
/// let cache = Cache::new(false);
1490+
/// let generic = Type::Generic(rustc_span::symbol::sym::Any);
1491+
/// let unit = Type::Primitive(PrimitiveType::Unit);
1492+
/// assert!(!generic.is_same(&unit, &cache));
1493+
/// assert!(unit.is_same(&generic, &cache));
1494+
/// ```
1495+
///
1496+
/// An owned type is also the same as its borrowed variants (this is commutative),
1497+
/// but `&T` is not the same as `&mut T`.
14771498
pub(crate) fn is_same(&self, other: &Self, cache: &Cache) -> bool {
1478-
match (self, other) {
1499+
let (self_cleared, other_cleared) = if !self.is_borrowed_ref() || !other.is_borrowed_ref() {
1500+
(self.without_borrowed_ref(), other.without_borrowed_ref())
1501+
} else {
1502+
(self, other)
1503+
};
1504+
match (self_cleared, other_cleared) {
14791505
// Recursive cases.
14801506
(Type::Tuple(a), Type::Tuple(b)) => {
14811507
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| a.is_same(b, cache))
@@ -1489,9 +1515,21 @@ impl Type {
14891515
Type::BorrowedRef { mutability, type_, .. },
14901516
Type::BorrowedRef { mutability: b_mutability, type_: b_type_, .. },
14911517
) => mutability == b_mutability && type_.is_same(b_type_, cache),
1492-
// Placeholders and generics are equal to all other types.
1518+
// Placeholders are equal to all other types.
14931519
(Type::Infer, _) | (_, Type::Infer) => true,
1494-
(Type::Generic(_), _) | (_, Type::Generic(_)) => true,
1520+
// Generics match everything on the right, but not on the left.
1521+
(_, Type::Generic(_)) => true,
1522+
(Type::Generic(_), _) => false,
1523+
// Paths account for both the path itself and its generics.
1524+
(Type::Path { path: a }, Type::Path { path: b }) => {
1525+
a.def_id() == b.def_id()
1526+
&& a.generics()
1527+
.zip(b.generics())
1528+
.map(|(ag, bg)| {
1529+
ag.iter().zip(bg.iter()).all(|(at, bt)| at.is_same(bt, cache))
1530+
})
1531+
.unwrap_or(true)
1532+
}
14951533
// Other cases, such as primitives, just use recursion.
14961534
(a, b) => a
14971535
.def_id(cache)

src/librustdoc/clean/types/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,14 @@ fn should_not_trim() {
6969
run_test("\t line1 \n\t line2", "line1 \nline2");
7070
run_test(" \tline1 \n \tline2", "line1 \nline2");
7171
}
72+
73+
#[test]
74+
fn is_same_generic() {
75+
use crate::clean::types::{PrimitiveType, Type};
76+
use crate::formats::cache::Cache;
77+
let cache = Cache::new(false);
78+
let generic = Type::Generic(rustc_span::symbol::sym::Any);
79+
let unit = Type::Primitive(PrimitiveType::Unit);
80+
assert!(!generic.is_same(&unit, &cache));
81+
assert!(unit.is_same(&generic, &cache));
82+
}

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &mut Context<'_>) -> O
12911291
if let Some(impls) = cx.cache().impls.get(&did) {
12921292
for i in impls {
12931293
let impl_ = i.inner_impl();
1294-
if !impl_.for_.without_borrowed_ref().is_same(ty.without_borrowed_ref(), cx.cache()) {
1294+
if !ty.is_same(&impl_.for_, cx.cache()) {
12951295
// Two different types might have the same did,
12961296
// without actually being the same.
12971297
continue;
@@ -1327,7 +1327,7 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
13271327

13281328
for i in impls {
13291329
let impl_ = i.inner_impl();
1330-
if !impl_.for_.without_borrowed_ref().is_same(ty.without_borrowed_ref(), cx.cache()) {
1330+
if !ty.is_same(&impl_.for_, cx.cache()) {
13311331
// Two different types might have the same did,
13321332
// without actually being the same.
13331333
continue;

tests/rustdoc/notable-trait/doc-notable_trait-slice.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ pub fn bare_fn_matches() -> &'static [SomeStruct] {
1818
pub fn bare_fn_no_matches() -> &'static [OtherStruct] {
1919
&[]
2020
}
21+
22+
// @has doc_notable_trait_slice/fn.bare_fn_mut_no_matches.html
23+
// @count - '//script[@id="notable-traits-data"]' 0
24+
pub fn bare_fn_mut_no_matches() -> &'static mut [SomeStruct] {
25+
&mut []
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![feature(doc_notable_trait)]
2+
3+
// Notable traits SHOULD be shown when the `impl` has a generic type and the
4+
// return type has a concrete type.
5+
pub mod generic_return {
6+
pub struct Wrapper<T>(T);
7+
8+
#[doc(notable_trait)]
9+
pub trait NotableTrait {}
10+
11+
impl NotableTrait for Wrapper<u8> {}
12+
13+
// @has notable_trait_generics/generic_return/fn.returning.html
14+
// @!has - '//a[@class="tooltip"]/@data-notable-ty' 'Wrapper<T>'
15+
pub fn returning<T>() -> Wrapper<T> {
16+
loop {}
17+
}
18+
}
19+
20+
// Notable traits SHOULD NOT be shown when the `impl` has a concrete type and
21+
// the return type has a generic type.
22+
pub mod generic_impl {
23+
pub struct Wrapper<T>(T);
24+
25+
#[doc(notable_trait)]
26+
pub trait NotableTrait {}
27+
28+
impl<T> NotableTrait for Wrapper<T> {}
29+
30+
// @has notable_trait_generics/generic_impl/fn.returning.html
31+
// @has - '//a[@class="tooltip"]/@data-notable-ty' 'Wrapper<u8>'
32+
pub fn returning() -> Wrapper<u8> {
33+
loop {}
34+
}
35+
}

0 commit comments

Comments
 (0)