Skip to content

Commit 0392e29

Browse files
authored
Rollup merge of #108954 - notriddle:notriddle/notable-trait-generic, r=camelid
rustdoc: handle generics better when matching notable traits This commit makes the `clean::Type::is_same` non-commutative (renaming it `is_doc_subtype_of`), 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. Fixes #100322 Fixes #55082 Preview: * https://notriddle.com/rustdoc-demo-html-3/notable-trait-generic/std/vec/struct.Vec.html#method.new * https://notriddle.com/rustdoc-demo-html-3/notable-trait-generic/std/vec/struct.Vec.html#method.from-12 * https://notriddle.com/rustdoc-demo-html-3/notable-trait-generic/std/iter/trait.Iterator.html#method.intersperse_with * https://notriddle.com/rustdoc-demo-html-3/notable-trait-generic/std/string/struct.String.html#method.as_bytes
2 parents a266f11 + 7f76084 commit 0392e29

15 files changed

+105
-12
lines changed

src/librustdoc/clean/types.rs

+51-10
Original file line numberDiff line numberDiff line change
@@ -1471,27 +1471,68 @@ 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.
1477-
pub(crate) fn is_same(&self, other: &Self, cache: &Cache) -> bool {
1478-
match (self, other) {
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`.
1498+
pub(crate) fn is_doc_subtype_of(&self, other: &Self, cache: &Cache) -> bool {
1499+
// Strip the references so that it can compare the actual types, unless both are references.
1500+
// If both are references, leave them alone and compare the mutabilities later.
1501+
let (self_cleared, other_cleared) = if !self.is_borrowed_ref() || !other.is_borrowed_ref() {
1502+
(self.without_borrowed_ref(), other.without_borrowed_ref())
1503+
} else {
1504+
(self, other)
1505+
};
1506+
match (self_cleared, other_cleared) {
14791507
// Recursive cases.
14801508
(Type::Tuple(a), Type::Tuple(b)) => {
1481-
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| a.is_same(b, cache))
1509+
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| a.is_doc_subtype_of(b, cache))
14821510
}
1483-
(Type::Slice(a), Type::Slice(b)) => a.is_same(b, cache),
1484-
(Type::Array(a, al), Type::Array(b, bl)) => al == bl && a.is_same(b, cache),
1511+
(Type::Slice(a), Type::Slice(b)) => a.is_doc_subtype_of(b, cache),
1512+
(Type::Array(a, al), Type::Array(b, bl)) => al == bl && a.is_doc_subtype_of(b, cache),
14851513
(Type::RawPointer(mutability, type_), Type::RawPointer(b_mutability, b_type_)) => {
1486-
mutability == b_mutability && type_.is_same(b_type_, cache)
1514+
mutability == b_mutability && type_.is_doc_subtype_of(b_type_, cache)
14871515
}
14881516
(
14891517
Type::BorrowedRef { mutability, type_, .. },
14901518
Type::BorrowedRef { mutability: b_mutability, type_: b_type_, .. },
1491-
) => mutability == b_mutability && type_.is_same(b_type_, cache),
1492-
// Placeholders and generics are equal to all other types.
1519+
) => mutability == b_mutability && type_.is_doc_subtype_of(b_type_, cache),
1520+
// Placeholders are equal to all other types.
14931521
(Type::Infer, _) | (_, Type::Infer) => true,
1494-
(Type::Generic(_), _) | (_, Type::Generic(_)) => true,
1522+
// Generics match everything on the right, but not on the left.
1523+
// If both sides are generic, this returns true.
1524+
(_, Type::Generic(_)) => true,
1525+
(Type::Generic(_), _) => false,
1526+
// Paths account for both the path itself and its generics.
1527+
(Type::Path { path: a }, Type::Path { path: b }) => {
1528+
a.def_id() == b.def_id()
1529+
&& a.generics()
1530+
.zip(b.generics())
1531+
.map(|(ag, bg)| {
1532+
ag.iter().zip(bg.iter()).all(|(at, bt)| at.is_doc_subtype_of(bt, cache))
1533+
})
1534+
.unwrap_or(true)
1535+
}
14951536
// Other cases, such as primitives, just use recursion.
14961537
(a, b) => a
14971538
.def_id(cache)

src/librustdoc/clean/types/tests.rs

+11
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_doc_subtype_of(&unit, &cache));
81+
assert!(unit.is_doc_subtype_of(&generic, &cache));
82+
}

src/librustdoc/html/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &mut Context<'_>) -> O
12941294
if let Some(impls) = cx.cache().impls.get(&did) {
12951295
for i in impls {
12961296
let impl_ = i.inner_impl();
1297-
if !impl_.for_.without_borrowed_ref().is_same(ty.without_borrowed_ref(), cx.cache()) {
1297+
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
12981298
// Two different types might have the same did,
12991299
// without actually being the same.
13001300
continue;
@@ -1330,7 +1330,7 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
13301330

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

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

+6
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![feature(doc_notable_trait)]
2+
3+
// Notable traits SHOULD NOT be shown when the `impl` has a concrete type and
4+
// the return type has a generic 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 be shown when the `impl` has a generic type and the
21+
// return type has a concrete 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)