Skip to content

Commit 9b4219b

Browse files
authored
Rollup merge of #149460 - lolbinarycat:rustdoc-search-sort-stable-first, r=notriddle
rustdoc: sort stable items first Finally tackling this again now that the search system refactor is done. The tests and the general approach were taken from the original PR.
2 parents 4571317 + dca86a9 commit 9b4219b

11 files changed

Lines changed: 58 additions & 4 deletions

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
@@ -1634,6 +1634,7 @@ class DocSearch {
16341634
* parent,
16351635
* trait_parent,
16361636
* deprecated,
1637+
* unstable,
16371638
* associated_item_disambiguator
16381639
* @type {rustdoc.ArrayWithOptionals<[
16391640
* number,
@@ -1643,6 +1644,7 @@ class DocSearch {
16431644
* number,
16441645
* number,
16451646
* number,
1647+
* number,
16461648
* ], [string]>}
16471649
*/
16481650
const raw = JSON.parse(encoded);
@@ -1654,7 +1656,8 @@ class DocSearch {
16541656
parent: raw[4] === 0 ? null : raw[4] - 1,
16551657
traitParent: raw[5] === 0 ? null : raw[5] - 1,
16561658
deprecated: raw[6] === 1 ? true : false,
1657-
associatedItemDisambiguator: raw.length === 7 ? null : raw[7],
1659+
unstable: raw[7] === 1 ? true : false,
1660+
associatedItemDisambiguator: raw.length === 8 ? null : raw[8],
16581661
};
16591662
}
16601663

@@ -1947,6 +1950,7 @@ class DocSearch {
19471950
path,
19481951
functionData,
19491952
deprecated: entry ? entry.deprecated : false,
1953+
unstable: entry ? entry.unstable : false,
19501954
parent,
19511955
traitParent,
19521956
};
@@ -2859,6 +2863,13 @@ class DocSearch {
28592863
return a - b;
28602864
}
28612865

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

tests/rustdoc-js-std/core-transmute.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const EXPECTED = [
33
{
44
'query': 'generic:T -> generic:U',
55
'others': [
6+
{ 'path': 'core::mem', 'name': 'transmute' },
67
{ 'path': 'core::intrinsics::simd', 'name': 'simd_as' },
78
{ 'path': 'core::intrinsics::simd', 'name': 'simd_cast' },
8-
{ 'path': 'core::mem', 'name': 'transmute' },
99
],
1010
},
1111
];

tests/rustdoc-js-std/transmute-fail.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const EXPECTED = [
66
// should-fail tag and the search query below:
77
'query': 'generic:T -> generic:T',
88
'others': [
9+
{ 'path': 'std::mem', 'name': 'transmute' },
910
{ 'path': 'std::intrinsics::simd', 'name': 'simd_as' },
1011
{ 'path': 'std::intrinsics::simd', 'name': 'simd_cast' },
11-
{ 'path': 'std::mem', 'name': 'transmute' },
1212
],
1313
},
1414
];

tests/rustdoc-js-std/transmute.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const EXPECTED = [
55
// should-fail tag and the search query below:
66
'query': 'generic:T -> generic:U',
77
'others': [
8+
{ 'path': 'std::mem', 'name': 'transmute' },
89
{ 'path': 'std::intrinsics::simd', 'name': 'simd_as' },
910
{ 'path': 'std::intrinsics::simd', 'name': 'simd_cast' },
10-
{ 'path': 'std::mem', 'name': 'transmute' },
1111
],
1212
},
1313
];

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+
];

0 commit comments

Comments
 (0)