Skip to content

Commit 0a9a15a

Browse files
committed
rustdoc: sort unstable items first
1 parent 0f6dae4 commit 0a9a15a

7 files changed

Lines changed: 52 additions & 2 deletions

File tree

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 deprecation = item.deprecation(tcx);
597+
let stability = item.stability(tcx);
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
deprecation,
613+
stability,
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) deprecation: Option<Deprecation>,
145+
pub(crate) stability: Option<rustc_hir::Stability>,
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
@@ -611,6 +611,7 @@ impl SerializedSearchIndex {
611611
parent,
612612
trait_parent,
613613
deprecated,
614+
unstable,
614615
associated_item_disambiguator,
615616
}| EntryData {
616617
krate: *map.get(krate).unwrap(),
@@ -621,6 +622,7 @@ impl SerializedSearchIndex {
621622
parent: parent.and_then(|path_id| map.get(&path_id).copied()),
622623
trait_parent: trait_parent.and_then(|path_id| map.get(&path_id).copied()),
623624
deprecated: *deprecated,
625+
unstable: *unstable,
624626
associated_item_disambiguator: associated_item_disambiguator.clone(),
625627
},
626628
),
@@ -889,6 +891,7 @@ struct EntryData {
889891
parent: Option<usize>,
890892
trait_parent: Option<usize>,
891893
deprecated: bool,
894+
unstable: bool,
892895
associated_item_disambiguator: Option<String>,
893896
}
894897

@@ -905,6 +908,7 @@ impl Serialize for EntryData {
905908
seq.serialize_element(&self.parent.map(|id| id + 1).unwrap_or(0))?;
906909
seq.serialize_element(&self.trait_parent.map(|id| id + 1).unwrap_or(0))?;
907910
seq.serialize_element(&if self.deprecated { 1 } else { 0 })?;
911+
seq.serialize_element(&if self.unstable { 1 } else { 0 })?;
908912
if let Some(disambig) = &self.associated_item_disambiguator {
909913
seq.serialize_element(&disambig)?;
910914
}
@@ -939,6 +943,7 @@ impl<'de> Deserialize<'de> for EntryData {
939943
v.next_element()?.ok_or_else(|| A::Error::missing_field("trait_parent"))?;
940944

941945
let deprecated: u32 = v.next_element()?.unwrap_or(0);
946+
let unstable: u32 = v.next_element()?.unwrap_or(0);
942947
let associated_item_disambiguator: Option<String> = v.next_element()?;
943948
Ok(EntryData {
944949
krate,
@@ -949,6 +954,7 @@ impl<'de> Deserialize<'de> for EntryData {
949954
parent: Option::<i32>::from(parent).map(|path| path as usize),
950955
trait_parent: Option::<i32>::from(trait_parent).map(|path| path as usize),
951956
deprecated: deprecated != 0,
957+
unstable: unstable != 0,
952958
associated_item_disambiguator,
953959
})
954960
}
@@ -1275,6 +1281,7 @@ pub(crate) fn build_index(
12751281
),
12761282
aliases: item.attrs.get_doc_aliases(),
12771283
deprecation: item.deprecation(tcx),
1284+
stability: item.stability(tcx),
12781285
});
12791286
}
12801287
}
@@ -1370,6 +1377,7 @@ pub(crate) fn build_index(
13701377
parent: None,
13711378
trait_parent: None,
13721379
deprecated: false,
1380+
unstable: false,
13731381
associated_item_disambiguator: None,
13741382
}),
13751383
crate_doc,
@@ -1508,6 +1516,7 @@ pub(crate) fn build_index(
15081516
module_path,
15091517
exact_module_path,
15101518
deprecated: item.deprecation.is_some(),
1519+
unstable: item.stability.is_some_and(|x| x.is_unstable()),
15111520
associated_item_disambiguator: if let Some(impl_id) = item.impl_id
15121521
&& let Some(parent_idx) = item.parent_idx
15131522
&& associated_item_duplicates

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ declare namespace rustdoc {
148148

149149
/**
150150
* A single parsed "atom" in a search query. For example,
151-
*
151+
*
152152
* std::fmt::Formatter, Write -> Result<()>
153153
* ┏━━━━━━━━━━━━━━━━━━ ┌──── ┏━━━━━┅┅┅┅┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐
154154
* ┃ │ ┗ QueryElement { ┊
@@ -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
};
@@ -2867,6 +2871,13 @@ class DocSearch {
28672871
return a - b;
28682872
}
28692873

2874+
// sort unstable items later
2875+
a = Number(aai.unstable);
2876+
b = Number(bbi.unstable);
2877+
if (a !== b) {
2878+
return a - b;
2879+
}
2880+
28702881
// sort by crate (current crate comes first)
28712882
a = Number(aai.crate !== preferredCrate);
28722883
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)