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

Sort negative impls before positive ones. #85398

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
18 changes: 12 additions & 6 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,18 +664,18 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
}
}

let (local, foreign) = implementors.iter().partition::<Vec<_>, _>(|i| {
let (mut local, mut 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);
local.sort_by(|a, b| compare_impl(a, b, cx));
foreign.sort_by(|a, b| compare_impl(a, b, cx));

synthetic.sort_by(|a, b| compare_impl(a, b, cx));
concrete.sort_by(|a, b| compare_impl(a, b, cx));
let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) =
local.iter().partition(|i| i.inner_impl().synthetic);

if !foreign.is_empty() {
write_small_section_header(w, "foreign-impls", "Implementations on Foreign Types", "");
Expand Down Expand Up @@ -1284,10 +1284,16 @@ fn render_stability_since(
)
}

fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl, cx: &Context<'_>) -> Ordering {
fn compare_impl<'a, 'b>(lhs: &'a &Impl, rhs: &'b &Impl, cx: &Context<'_>) -> Ordering {
let lhss = format!("{}", lhs.inner_impl().print(false, cx));
let rhss = format!("{}", rhs.inner_impl().print(false, cx));

if lhs.inner_impl().negative_polarity && !rhs.inner_impl().negative_polarity {
return Ordering::Less;
} else if rhs.inner_impl().negative_polarity && !lhs.inner_impl().negative_polarity {
return Ordering::Greater;
}
Comment on lines +1291 to +1295
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this to use a pattern match instead? I think it would be easier to understand the logic and reduce bugs. Something like this:

match (lhs.inner_impl().negative_polarity, rhs.inner_impl().negative_polarity) {
    (true, false) => return Ordering::Less,
    // ...
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case where the polarities were the same would fall through via => {}.


// lhs and rhs are formatted as HTML, which may be unnecessary
compare_names(&lhss, &rhss)
}
Expand Down