Skip to content

Commit 846cb34

Browse files
Make impl associated constants sorted first
1 parent b7cd99d commit 846cb34

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

src/librustdoc/html/render/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1786,24 +1786,24 @@ fn render_impl(
17861786

17871787
// Impl items are grouped by kinds:
17881788
//
1789-
// 1. Types
1790-
// 2. Constants
1789+
// 1. Constants
1790+
// 2. Types
17911791
// 3. Functions
17921792
//
1793-
// This order is because you can have associated types in associated constants, and both in
1794-
// associcated functions. So with this order, when reading from top to bottom, you should always
1795-
// see all items definitions before they're actually used.
1796-
let mut assoc_consts = Vec::new();
1793+
// This order is because you can have associated constants used in associated types (like array
1794+
// length), and both in associcated functions. So with this order, when reading from top to
1795+
// bottom, you should see items definitions before they're actually used most of the time.
1796+
let mut assoc_types = Vec::new();
17971797
let mut methods = Vec::new();
17981798

17991799
if !impl_.is_negative_trait_impl() {
18001800
for trait_item in &impl_.items {
18011801
match *trait_item.kind {
18021802
clean::MethodItem(..) | clean::TyMethodItem(_) => methods.push(trait_item),
1803-
clean::TyAssocConstItem(..) | clean::AssocConstItem(_) => {
1804-
assoc_consts.push(trait_item)
1805-
}
18061803
clean::TyAssocTypeItem(..) | clean::AssocTypeItem(..) => {
1804+
assoc_types.push(trait_item)
1805+
}
1806+
clean::TyAssocConstItem(..) | clean::AssocConstItem(_) => {
18071807
// We render it directly since they're supposed to come first.
18081808
doc_impl_item(
18091809
&mut default_impl_items,
@@ -1822,12 +1822,12 @@ fn render_impl(
18221822
}
18231823
}
18241824

1825-
for assoc_const in assoc_consts {
1825+
for assoc_type in assoc_types {
18261826
doc_impl_item(
18271827
&mut default_impl_items,
18281828
&mut impl_items,
18291829
cx,
1830-
assoc_const,
1830+
assoc_type,
18311831
if trait_.is_some() { &i.impl_item } else { parent },
18321832
link,
18331833
render_mode,

src/librustdoc/html/render/sidebar.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ fn sidebar_trait<'a>(
223223
}
224224

225225
let mut blocks: Vec<LinkBlock<'_>> = [
226-
("required-associated-types", "Required Associated Types", req_assoc),
227-
("provided-associated-types", "Provided Associated Types", prov_assoc),
228226
("required-associated-consts", "Required Associated Constants", req_assoc_const),
229227
("provided-associated-consts", "Provided Associated Constants", prov_assoc_const),
228+
("required-associated-types", "Required Associated Types", req_assoc),
229+
("provided-associated-types", "Provided Associated Types", prov_assoc),
230230
("required-methods", "Required Methods", req_method),
231231
("provided-methods", "Provided Methods", prov_method),
232232
("foreign-impls", "Implementations on Foreign Types", foreign_impls),

tests/rustdoc/impl-associated-items-order.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This test ensures that impl associated items always follow this order:
22
//
3-
// 1. Types
4-
// 2. Consts
3+
// 1. Consts
4+
// 2. Types
55
// 3. Functions
66

77
#![feature(inherent_associated_types)]
@@ -15,10 +15,10 @@ impl Bar {
1515
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[3]/h4' \
1616
// 'pub fn foo()'
1717
pub fn foo() {}
18-
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[2]/h4' \
18+
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[1]/h4' \
1919
// 'pub const X: u8 = 12u8'
2020
pub const X: u8 = 12;
21-
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[1]/h4' \
21+
//@ has - '//*[@id="implementations-list"]//*[@class="impl-items"]/section[2]/h4' \
2222
// 'pub type Y = u8'
2323
pub type Y = u8;
2424
}
@@ -31,11 +31,11 @@ pub trait Foo {
3131

3232
impl Foo for Bar {
3333
//@ has - '//*[@id="trait-implementations-list"]//*[@class="impl-items"]/section[2]/h4' \
34-
// 'const W: u32 = 12u32'
35-
const W: u32 = 12;
36-
//@ has - '//*[@id="trait-implementations-list"]//*[@class="impl-items"]/section[1]/h4' \
3734
// 'type Z = u8'
3835
type Z = u8;
36+
//@ has - '//*[@id="trait-implementations-list"]//*[@class="impl-items"]/section[1]/h4' \
37+
// 'const W: u32 = 12u32'
38+
const W: u32 = 12;
3939
//@ has - '//*[@id="trait-implementations-list"]//*[@class="impl-items"]/section[3]/h4' \
4040
// 'fn yeay()'
4141
fn yeay() {}

0 commit comments

Comments
 (0)