Make impl associated constants sorted first

This commit is contained in:
Guillaume Gomez 2024-08-25 22:02:57 +02:00
parent f96aff95d5
commit 1884983001
3 changed files with 20 additions and 20 deletions

View File

@ -1796,24 +1796,24 @@ fn render_impl(
// Impl items are grouped by kinds:
//
// 1. Types
// 2. Constants
// 1. Constants
// 2. Types
// 3. Functions
//
// This order is because you can have associated types in associated constants, and both in
// associcated functions. So with this order, when reading from top to bottom, you should always
// see all items definitions before they're actually used.
let mut assoc_consts = Vec::new();
// This order is because you can have associated constants used in associated types (like array
// length), and both in associcated functions. So with this order, when reading from top to
// bottom, you should see items definitions before they're actually used most of the time.
let mut assoc_types = Vec::new();
let mut methods = Vec::new();
if !impl_.is_negative_trait_impl() {
for trait_item in &impl_.items {
match *trait_item.kind {
clean::MethodItem(..) | clean::TyMethodItem(_) => methods.push(trait_item),
clean::TyAssocConstItem(..) | clean::AssocConstItem(_) => {
assoc_consts.push(trait_item)
}
clean::TyAssocTypeItem(..) | clean::AssocTypeItem(..) => {
assoc_types.push(trait_item)
}
clean::TyAssocConstItem(..) | clean::AssocConstItem(_) => {
// We render it directly since they're supposed to come first.
doc_impl_item(
&mut default_impl_items,
@ -1832,12 +1832,12 @@ fn render_impl(
}
}
for assoc_const in assoc_consts {
for assoc_type in assoc_types {
doc_impl_item(
&mut default_impl_items,
&mut impl_items,
cx,
assoc_const,
assoc_type,
if trait_.is_some() { &i.impl_item } else { parent },
link,
render_mode,

View File

@ -302,10 +302,10 @@ fn sidebar_trait<'a>(
blocks.extend(
[
("required-associated-types", "Required Associated Types", req_assoc),
("provided-associated-types", "Provided Associated Types", prov_assoc),
("required-associated-consts", "Required Associated Constants", req_assoc_const),
("provided-associated-consts", "Provided Associated Constants", prov_assoc_const),
("required-associated-types", "Required Associated Types", req_assoc),
("provided-associated-types", "Provided Associated Types", prov_assoc),
("required-methods", "Required Methods", req_method),
("provided-methods", "Provided Methods", prov_method),
("foreign-impls", "Implementations on Foreign Types", foreign_impls),

View File

@ -1,7 +1,7 @@
// This test ensures that impl associated items always follow this order:
//
// 1. Types
// 2. Consts
// 1. Consts
// 2. Types
// 3. Functions
#![feature(inherent_associated_types)]
@ -15,10 +15,10 @@ impl Bar {
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[3]/h4' \
// 'pub fn foo()'
pub fn foo() {}
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[2]/h4' \
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[1]/h4' \
// 'pub const X: u8 = 12u8'
pub const X: u8 = 12;
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[1]/h4' \
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[2]/h4' \
// 'pub type Y = u8'
pub type Y = u8;
}
@ -31,11 +31,11 @@ pub trait Foo {
impl Foo for Bar {
//@ has - '//*[@id="trait-implementations-list"]//*[@class="impl-items"]/section[2]/h4' \
// 'const W: u32 = 12u32'
const W: u32 = 12;
//@ has - '//*[@id="trait-implementations-list"]//*[@class="impl-items"]/section[1]/h4' \
// 'type Z = u8'
type Z = u8;
//@ has - '//*[@id="trait-implementations-list"]//*[@class="impl-items"]/section[1]/h4' \
// 'const W: u32 = 12u32'
const W: u32 = 12;
//@ has - '//*[@id="trait-implementations-list"]//*[@class="impl-items"]/section[3]/h4' \
// 'fn yeay()'
fn yeay() {}