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: Sort negative impls to the top #79453

Closed
wants to merge 1 commit into from

Conversation

camelid
Copy link
Member

@camelid camelid commented Nov 26, 2020

Fixes #51129.

@camelid camelid added A-traits Area: Trait system T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Nov 26, 2020
@rust-highfive
Copy link
Collaborator

r? @GuillaumeGomez

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 26, 2020
(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,
Copy link
Member Author

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?

Copy link
Member

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:


AutoTraitResult::PositiveImpl(new_generics) => {
polarity = None;

Copy link
Member

@the8472 the8472 Nov 27, 2020

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.

Copy link
Member

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.

Copy link
Member

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.

@camelid
Copy link
Member Author

camelid commented Nov 27, 2020

Unfortunately it doesn't look this works for some reason...

@camelid
Copy link
Member Author

camelid commented Nov 27, 2020

The impls still aren't sorted :/

@@ -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 {
Copy link
Member

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.

Copy link
Member

@the8472 the8472 Nov 27, 2020

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();
Copy link
Member

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).

Copy link
Member Author

@camelid camelid Nov 27, 2020

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?

Copy link
Contributor

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

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.

@bors
Copy link
Contributor

bors commented Dec 16, 2020

☔ 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:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@camelid camelid added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Dec 16, 2020
@camelid
Copy link
Member Author

camelid commented Mar 22, 2021

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?

@jsha
Copy link
Contributor

jsha commented Apr 21, 2021

I think what @jyn514 is saying is: If you merge the latest master, you'll need to update compare_impl_polarity to take advantage of the fact that polarity is now just a boolean, not a three-valued item. The natural, intuitive implementation there (sort true above false) will solve the transitivity of equality problem raised above.

I also commented at https://github.com/rust-lang/rust/pull/79453/files/1bfb64bb7cfb2b4b79c7f12417b353fe09119b28#r617221763 discussing why sorting on implementors wasn't taking effect (according to my reading of the code, at least; I haven't run it).

@jyn514 jyn514 removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 21, 2021
@jsha
Copy link
Contributor

jsha commented Jun 26, 2021

I think this is a candidate for closing. As discussed in #85398 and #51129, we need a more significant refactor in how impls are rendered before we can do this correctly.

@jyn514 jyn514 closed this Jun 26, 2021
@camelid camelid deleted the rustdoc-neg-impl branch June 29, 2021 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
7 participants