diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 147a8d33765af..e3b20632db01f 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -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, + } +} + 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::>(); @@ -2718,6 +2730,10 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait, } } + let mut implementors = implementors.clone(); + + implementors.sort_by(compare_impl_polarity); + let (local, foreign) = implementors.iter().partition::, _>(|i| { i.inner_impl().for_.def_id().map_or(true, |d| cache.paths.contains_key(&d)) });