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

rustdoc: handle generics better when matching notable traits #108954

Merged
merged 5 commits into from
Mar 23, 2023
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
Prev Previous commit
Next Next commit
rustdoc: rename Type::is_same to is_doc_subtype_of
  • Loading branch information
notriddle committed Mar 13, 2023
commit 86179c4549e74acf22fc4da02b64f0bb2ce1f4aa
14 changes: 7 additions & 7 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ impl Type {
///
/// An owned type is also the same as its borrowed variants (this is commutative),
/// but `&T` is not the same as `&mut T`.
pub(crate) fn is_same(&self, other: &Self, cache: &Cache) -> bool {
pub(crate) fn is_doc_subtype_of(&self, other: &Self, cache: &Cache) -> bool {
let (self_cleared, other_cleared) = if !self.is_borrowed_ref() || !other.is_borrowed_ref() {
(self.without_borrowed_ref(), other.without_borrowed_ref())
} else {
Expand All @@ -1504,17 +1504,17 @@ impl Type {
match (self_cleared, other_cleared) {
// Recursive cases.
(Type::Tuple(a), Type::Tuple(b)) => {
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| a.is_same(b, cache))
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| a.is_doc_subtype_of(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::Slice(a), Type::Slice(b)) => a.is_doc_subtype_of(b, cache),
(Type::Array(a, al), Type::Array(b, bl)) => al == bl && a.is_doc_subtype_of(b, cache),
(Type::RawPointer(mutability, type_), Type::RawPointer(b_mutability, b_type_)) => {
mutability == b_mutability && type_.is_same(b_type_, cache)
mutability == b_mutability && type_.is_doc_subtype_of(b_type_, cache)
}
(
Type::BorrowedRef { mutability, type_, .. },
Type::BorrowedRef { mutability: b_mutability, type_: b_type_, .. },
) => mutability == b_mutability && type_.is_same(b_type_, cache),
) => mutability == b_mutability && type_.is_doc_subtype_of(b_type_, cache),
// Placeholders are equal to all other types.
(Type::Infer, _) | (_, Type::Infer) => true,
// Generics match everything on the right, but not on the left.
Expand All @@ -1526,7 +1526,7 @@ impl Type {
&& a.generics()
.zip(b.generics())
.map(|(ag, bg)| {
ag.iter().zip(bg.iter()).all(|(at, bt)| at.is_same(bt, cache))
ag.iter().zip(bg.iter()).all(|(at, bt)| at.is_doc_subtype_of(bt, cache))
})
.unwrap_or(true)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/clean/types/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ fn is_same_generic() {
let cache = Cache::new(false);
let generic = Type::Generic(rustc_span::symbol::sym::Any);
let unit = Type::Primitive(PrimitiveType::Unit);
assert!(!generic.is_same(&unit, &cache));
assert!(unit.is_same(&generic, &cache));
assert!(!generic.is_doc_subtype_of(&unit, &cache));
assert!(unit.is_doc_subtype_of(&generic, &cache));
}
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &mut Context<'_>) -> O
if let Some(impls) = cx.cache().impls.get(&did) {
for i in impls {
let impl_ = i.inner_impl();
if !ty.is_same(&impl_.for_, cx.cache()) {
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
// Two different types might have the same did,
// without actually being the same.
continue;
Expand Down Expand Up @@ -1327,7 +1327,7 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {

for i in impls {
let impl_ = i.inner_impl();
if !ty.is_same(&impl_.for_, cx.cache()) {
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
// Two different types might have the same did,
// without actually being the same.
continue;
Expand Down