-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
(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 comment
The reason will be displayed to describe this comment to others. Learn more.
What does it mean if the polarity is None
? Is this the correct way to handle that case?
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.
$ rg polarity.*None src/librustdoc/
src/librustdoc/clean/blanket_impl.rs
136: polarity: None,
src/librustdoc/clean/auto_trait.rs
90: polarity = None;
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
polarity: None, |
rust/src/librustdoc/clean/auto_trait.rs
Lines 89 to 90 in cb56a44
AutoTraitResult::PositiveImpl(new_generics) => { | |
polarity = None; |
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.
I think this violates the transitivity requirement of sort_by
.
Given a = Positive; b = None; c = Negative
then
- a == b
- b == c
- a != c
You might want to use sort_by_key
and assign an artificial ranking instead.
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.
Then use the same logic used in compare_impl
.
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.
I think this should be fixed on its own after #80825 - try rebasing.
7b81e71
to
ea211bf
Compare
Unfortunately it doesn't look this works for some reason... |
The impls still aren't sorted :/ |
ea211bf
to
1bfb64b
Compare
@@ -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 { |
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.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
It's kinda useless to sort implementors
since it's not used for rendering as is. Look at synthetic
and concrete
(and update the compare_impl
function too like I said above).
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.
But I wanted to sort foreign types too. Why is it bad to sort implementors
? Aren’t synthetic and concrete created it out of it?
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.
The problem is that synthetic and concrete are later re-sorted by compare_impl
(lines 2744 and 2745 in this diff, or
rust/src/librustdoc/html/render/print_item.rs
Lines 656 to 667 in 6df26f8
let (local, foreign) = implementors.iter().partition::<Vec<_>, _>(|i| { | |
i.inner_impl() | |
.for_ | |
.def_id_full(cx.cache()) | |
.map_or(true, |d| cx.cache.paths.contains_key(&d)) | |
}); | |
let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) = | |
local.iter().partition(|i| i.inner_impl().synthetic); | |
synthetic.sort_by(|a, b| compare_impl(a, b, cx)); | |
concrete.sort_by(|a, b| compare_impl(a, b, cx)); |
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 synthetic
and concrete
so you don't undo the sorting.
☔ The latest upstream changes (presumably #80041) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
I haven't looked at this in a while. There are several threads of review comments and I feel a bit confused about what the overall approach should be. So, what is the "right" way to sort the impls? |
I think what @jyn514 is saying is: If you merge the latest master, you'll need to update I also commented at https://github.com/rust-lang/rust/pull/79453/files/1bfb64bb7cfb2b4b79c7f12417b353fe09119b28#r617221763 discussing why sorting on |
Fixes #51129.