-
Notifications
You must be signed in to change notification settings - Fork 13k
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: Sort negative impls to the top #79453
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -63,7 +63,9 @@ use rustc_span::symbol::{sym, Symbol}; | |||||||||||||||||||||||||
use serde::ser::SerializeSeq; | ||||||||||||||||||||||||||
use serde::{Serialize, Serializer}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
use crate::clean::{self, AttributesExt, Deprecation, GetDefId, RenderedLink, SelfTy, TypeKind}; | ||||||||||||||||||||||||||
use crate::clean::{ | ||||||||||||||||||||||||||
self, AttributesExt, Deprecation, GetDefId, ImplPolarity, RenderedLink, SelfTy, TypeKind, | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
use crate::config::{RenderInfo, RenderOptions}; | ||||||||||||||||||||||||||
use crate::docfs::{DocFS, PathError}; | ||||||||||||||||||||||||||
use crate::doctree; | ||||||||||||||||||||||||||
|
@@ -2532,6 +2534,16 @@ fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl) -> Ordering { | |||||||||||||||||||||||||
compare_names(&lhs, &rhs) | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
fn compare_impl_polarity(a: &Impl, b: &Impl) -> Ordering { | ||||||||||||||||||||||||||
match (a.inner_impl().polarity.as_ref(), b.inner_impl().polarity.as_ref()) { | ||||||||||||||||||||||||||
(Some(ImplPolarity::Positive), Some(ImplPolarity::Negative)) => Ordering::Greater, | ||||||||||||||||||||||||||
(Some(ImplPolarity::Negative), Some(ImplPolarity::Positive)) => Ordering::Less, | ||||||||||||||||||||||||||
(Some(ImplPolarity::Positive), Some(ImplPolarity::Positive)) | ||||||||||||||||||||||||||
| (Some(ImplPolarity::Negative), Some(ImplPolarity::Negative)) => Ordering::Equal, | ||||||||||||||||||||||||||
(None, _) | (_, None) => Ordering::Equal, | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does it mean if the polarity is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure what it means, but apparently it only happens for blanket impls and auto traits: rust/src/librustdoc/clean/blanket_impl.rs Line 136 in cb56a44
rust/src/librustdoc/clean/auto_trait.rs Lines 89 to 90 in cb56a44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this violates the transitivity requirement of Given
You might want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then use the same logic used in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be fixed on its own after #80825 - try rebasing. |
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, cache: &Cache) { | ||||||||||||||||||||||||||
let bounds = bounds(&t.bounds, false); | ||||||||||||||||||||||||||
let types = t.items.iter().filter(|m| m.is_associated_type()).collect::<Vec<_>>(); | ||||||||||||||||||||||||||
|
@@ -2718,6 +2730,10 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, | |||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let mut implementors = implementors.clone(); | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's kinda useless to sort There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I wanted to sort foreign types too. Why is it bad to sort There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that synthetic and concrete are later re-sorted by rust/src/librustdoc/html/render/print_item.rs Lines 656 to 667 in 6df26f8
You need a sort function that compares first by polarity, and then by compare_impl. You should sort implementors by that, and then remove the subsequent .sort_by calls on |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
implementors.sort_by(compare_impl_polarity); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let (local, foreign) = implementors.iter().partition::<Vec<_>, _>(|i| { | ||||||||||||||||||||||||||
i.inner_impl().for_.def_id().map_or(true, |d| cache.paths.contains_key(&d)) | ||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You only take into account the polarity and not the name for the sorting, which isn't great... In case both impls have the same polarity, please return the comparison of the traits' name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that stable sorts are composable. If you first sort by property A and then by property B then the ordering for A will effectively serve as tie-breakers where B-comparisons return Ordering::Equal.