Skip to content

Commit 22ac5f7

Browse files
committed
rustdoc: sort stable items first
1 parent a234ae6 commit 22ac5f7

8 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/librustdoc/clean/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ impl Item {
431431
self.deprecation(tcx).is_some_and(|deprecation| deprecation.is_in_effect())
432432
}
433433

434+
pub(crate) fn is_unstable(&self) -> bool {
435+
self.stability.is_some_and(|x| x.is_unstable())
436+
}
437+
434438
pub(crate) fn inner_docs(&self, tcx: TyCtxt<'_>) -> bool {
435439
self.item_id.as_def_id().map(|did| inner_docs(tcx.get_all_attrs(did))).unwrap_or(false)
436440
}

src/librustdoc/formats/cache.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
594594
);
595595
let aliases = item.attrs.get_doc_aliases();
596596
let is_deprecated = item.is_deprecated(tcx);
597+
let is_unstable = item.is_unstable();
597598
let index_item = IndexItem {
598599
ty: item.type_(),
599600
defid: Some(defid),
@@ -609,6 +610,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
609610
search_type,
610611
aliases,
611612
is_deprecated,
613+
is_unstable,
612614
};
613615

614616
cache.search_index.push(index_item);

src/librustdoc/html/render/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub(crate) struct IndexItem {
142142
pub(crate) search_type: Option<IndexItemFunctionType>,
143143
pub(crate) aliases: Box<[Symbol]>,
144144
pub(crate) is_deprecated: bool,
145+
pub(crate) is_unstable: bool,
145146
}
146147

147148
/// A type used for the search index.

src/librustdoc/html/render/search_index.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ impl SerializedSearchIndex {
616616
parent,
617617
trait_parent,
618618
deprecated,
619+
unstable,
619620
associated_item_disambiguator,
620621
}| EntryData {
621622
krate: *map.get(krate).unwrap(),
@@ -626,6 +627,7 @@ impl SerializedSearchIndex {
626627
parent: parent.and_then(|path_id| map.get(&path_id).copied()),
627628
trait_parent: trait_parent.and_then(|path_id| map.get(&path_id).copied()),
628629
deprecated: *deprecated,
630+
unstable: *unstable,
629631
associated_item_disambiguator: associated_item_disambiguator.clone(),
630632
},
631633
),
@@ -896,6 +898,7 @@ struct EntryData {
896898
parent: Option<usize>,
897899
trait_parent: Option<usize>,
898900
deprecated: bool,
901+
unstable: bool,
899902
associated_item_disambiguator: Option<String>,
900903
}
901904

@@ -912,6 +915,7 @@ impl Serialize for EntryData {
912915
seq.serialize_element(&self.parent.map(|id| id + 1).unwrap_or(0))?;
913916
seq.serialize_element(&self.trait_parent.map(|id| id + 1).unwrap_or(0))?;
914917
seq.serialize_element(&if self.deprecated { 1 } else { 0 })?;
918+
seq.serialize_element(&if self.unstable { 1 } else { 0 })?;
915919
if let Some(disambig) = &self.associated_item_disambiguator {
916920
seq.serialize_element(&disambig)?;
917921
}
@@ -946,6 +950,7 @@ impl<'de> Deserialize<'de> for EntryData {
946950
v.next_element()?.ok_or_else(|| A::Error::missing_field("trait_parent"))?;
947951

948952
let deprecated: u32 = v.next_element()?.unwrap_or(0);
953+
let unstable: u32 = v.next_element()?.unwrap_or(0);
949954
let associated_item_disambiguator: Option<String> = v.next_element()?;
950955
Ok(EntryData {
951956
krate,
@@ -956,6 +961,7 @@ impl<'de> Deserialize<'de> for EntryData {
956961
parent: Option::<i32>::from(parent).map(|path| path as usize),
957962
trait_parent: Option::<i32>::from(trait_parent).map(|path| path as usize),
958963
deprecated: deprecated != 0,
964+
unstable: unstable != 0,
959965
associated_item_disambiguator,
960966
})
961967
}
@@ -1283,6 +1289,7 @@ pub(crate) fn build_index(
12831289
),
12841290
aliases: item.attrs.get_doc_aliases(),
12851291
is_deprecated: item.is_deprecated(tcx),
1292+
is_unstable: item.is_unstable(),
12861293
});
12871294
}
12881295
}
@@ -1382,6 +1389,7 @@ pub(crate) fn build_index(
13821389
parent: None,
13831390
trait_parent: None,
13841391
deprecated: false,
1392+
unstable: false,
13851393
associated_item_disambiguator: None,
13861394
}),
13871395
crate_doc,
@@ -1520,6 +1528,7 @@ pub(crate) fn build_index(
15201528
module_path,
15211529
exact_module_path,
15221530
deprecated: item.is_deprecated,
1531+
unstable: item.is_unstable,
15231532
associated_item_disambiguator: if let Some(impl_id) = item.impl_id
15241533
&& let Some(parent_idx) = item.parent_idx
15251534
&& associated_item_duplicates

src/librustdoc/html/static/js/rustdoc.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ declare namespace rustdoc {
243243
parent: number?,
244244
traitParent: number?,
245245
deprecated: boolean,
246+
unstable: boolean,
246247
associatedItemDisambiguator: string?,
247248
}
248249

@@ -292,6 +293,7 @@ declare namespace rustdoc {
292293
path: PathData?,
293294
functionData: FunctionData?,
294295
deprecated: boolean,
296+
unstable: boolean,
295297
parent: RowParent,
296298
traitParent: RowParent,
297299
}

src/librustdoc/html/static/js/search.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,7 @@ class DocSearch {
16331633
* parent,
16341634
* trait_parent,
16351635
* deprecated,
1636+
* unstable,
16361637
* associated_item_disambiguator
16371638
* @type {rustdoc.ArrayWithOptionals<[
16381639
* number,
@@ -1642,6 +1643,7 @@ class DocSearch {
16421643
* number,
16431644
* number,
16441645
* number,
1646+
* number,
16451647
* ], [string]>}
16461648
*/
16471649
const raw = JSON.parse(encoded);
@@ -1653,7 +1655,8 @@ class DocSearch {
16531655
parent: raw[4] === 0 ? null : raw[4] - 1,
16541656
traitParent: raw[5] === 0 ? null : raw[5] - 1,
16551657
deprecated: raw[6] === 1 ? true : false,
1656-
associatedItemDisambiguator: raw.length === 7 ? null : raw[7],
1658+
unstable: raw[7] === 1 ? true : false,
1659+
associatedItemDisambiguator: raw.length === 8 ? null : raw[8],
16571660
};
16581661
}
16591662

@@ -1946,6 +1949,7 @@ class DocSearch {
19461949
path,
19471950
functionData,
19481951
deprecated: entry ? entry.deprecated : false,
1952+
unstable: entry ? entry.unstable : false,
19491953
parent,
19501954
traitParent,
19511955
};
@@ -2858,6 +2862,13 @@ class DocSearch {
28582862
return a - b;
28592863
}
28602864

2865+
// sort unstable items later
2866+
a = Number(aai.unstable);
2867+
b = Number(bbi.unstable);
2868+
if (a !== b) {
2869+
return a - b;
2870+
}
2871+
28612872
// sort by crate (current crate comes first)
28622873
a = Number(aai.crate !== preferredCrate);
28632874
b = Number(bbi.crate !== preferredCrate);

tests/rustdoc-js/sort-stability.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const EXPECTED = [
2+
{
3+
'query': 'foo',
4+
'others': [
5+
{"path": "sort_stability::old", "name": "foo"},
6+
{"path": "sort_stability::new", "name": "foo"},
7+
],
8+
},
9+
];

tests/rustdoc-js/sort-stability.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(staged_api)]
2+
#![stable(feature = "foo_lib", since = "1.0.0")]
3+
4+
#[stable(feature = "old_foo", since = "1.0.1")]
5+
pub mod old {
6+
/// Old, stable foo
7+
#[stable(feature = "old_foo", since = "1.0.1")]
8+
pub fn foo() {}
9+
}
10+
11+
#[unstable(feature = "new_foo", issue = "none")]
12+
pub mod new {
13+
/// New, unstable foo
14+
#[unstable(feature = "new_foo", issue = "none")]
15+
pub fn foo() {}
16+
}

0 commit comments

Comments
 (0)