Skip to content

Commit 48f5929

Browse files
authored
Rollup merge of rust-lang#141658 - lolbinarycat:rustdoc-search-stability-rank-138067, r=GuillaumeGomez
rustdoc search: prefer stable items in search results fixes rust-lang#138067 this does add a new field to the search index, but since we're only listing unstable items instead of adding a boolean flag to every item, it should only increase the search index size of sysroot crates, since those are the only ones using the `staged_api` feature, at least as far as the rust project is concerned.
2 parents 4c7749e + fdbc8d0 commit 48f5929

File tree

10 files changed

+65
-4
lines changed

10 files changed

+65
-4
lines changed

src/librustdoc/formats/cache.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
602602
search_type,
603603
aliases,
604604
deprecation,
605+
is_unstable: item.stability(tcx).map(|x| x.is_unstable()).unwrap_or(false),
605606
};
606607
cache.search_index.push(index_item);
607608
}

src/librustdoc/html/render/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub(crate) struct IndexItem {
139139
pub(crate) search_type: Option<IndexItemFunctionType>,
140140
pub(crate) aliases: Box<[Symbol]>,
141141
pub(crate) deprecation: Option<Deprecation>,
142+
pub(crate) is_unstable: bool,
142143
}
143144

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

src/librustdoc/html/render/search_index.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub(crate) fn build_index(
9393
),
9494
aliases: item.attrs.get_doc_aliases(),
9595
deprecation: item.deprecation(tcx),
96+
is_unstable: item.stability(tcx).is_some_and(|x| x.is_unstable()),
9697
});
9798
}
9899
}
@@ -655,6 +656,7 @@ pub(crate) fn build_index(
655656
let mut parents_backref_queue = VecDeque::new();
656657
let mut functions = String::with_capacity(self.items.len());
657658
let mut deprecated = Vec::with_capacity(self.items.len());
659+
let mut unstable = Vec::with_capacity(self.items.len());
658660

659661
let mut type_backref_queue = VecDeque::new();
660662

@@ -711,6 +713,9 @@ pub(crate) fn build_index(
711713
// bitmasks always use 1-indexing for items, with 0 as the crate itself
712714
deprecated.push(u32::try_from(index + 1).unwrap());
713715
}
716+
if item.is_unstable {
717+
unstable.push(u32::try_from(index + 1).unwrap());
718+
}
714719
}
715720

716721
for (index, path) in &revert_extra_paths {
@@ -749,6 +754,7 @@ pub(crate) fn build_index(
749754
crate_data.serialize_field("r", &re_exports)?;
750755
crate_data.serialize_field("b", &self.associated_item_disambiguators)?;
751756
crate_data.serialize_field("c", &bitmap_to_string(&deprecated))?;
757+
crate_data.serialize_field("u", &bitmap_to_string(&unstable))?;
752758
crate_data.serialize_field("e", &bitmap_to_string(&self.empty_desc))?;
753759
crate_data.serialize_field("P", &param_names)?;
754760
if has_aliases {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ declare namespace rustdoc {
449449
* of `p`) but is used for modules items like free functions.
450450
*
451451
* `c` is an array of item indices that are deprecated.
452+
*
453+
* `u` is an array of item indices that are unstable.
452454
*/
453455
type RawSearchIndexCrate = {
454456
doc: string,
@@ -463,6 +465,7 @@ declare namespace rustdoc {
463465
p: Array<[number, string] | [number, string, number] | [number, string, number, number] | [number, string, number, number, string]>,
464466
b: Array<[number, String]>,
465467
c: string,
468+
u: string,
466469
r: Array<[number, number]>,
467470
P: Array<[number, string]>,
468471
};

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,11 @@ class DocSearch {
14641464
* @type {Map<String, RoaringBitmap>}
14651465
*/
14661466
this.searchIndexEmptyDesc = new Map();
1467+
/**
1468+
* @type {Map<String, RoaringBitmap>}
1469+
*/
1470+
this.searchIndexUnstable = new Map();
1471+
14671472
/**
14681473
* @type {Uint32Array}
14691474
*/
@@ -2052,9 +2057,10 @@ class DocSearch {
20522057
};
20532058
const descShardList = [descShard];
20542059

2055-
// Deprecated items and items with no description
2060+
// Deprecated and unstable items and items with no description
20562061
this.searchIndexDeprecated.set(crate, new RoaringBitmap(crateCorpus.c));
20572062
this.searchIndexEmptyDesc.set(crate, new RoaringBitmap(crateCorpus.e));
2063+
this.searchIndexUnstable.set(crate, new RoaringBitmap(crateCorpus.u));
20582064
let descIndex = 0;
20592065

20602066
/**
@@ -3326,6 +3332,25 @@ class DocSearch {
33263332
return a - b;
33273333
}
33283334

3335+
// sort unstable items later
3336+
// FIXME: there is some doubt if this is the most effecient way to implement this.
3337+
// alternative options include:
3338+
// * put is_unstable on each item when the index is built.
3339+
// increases memory usage but avoids a hashmap lookup.
3340+
// * put is_unstable on each item before sorting.
3341+
// better worst case performance but worse average case performance.
3342+
a = Number(
3343+
// @ts-expect-error
3344+
this.searchIndexUnstable.get(aaa.item.crate).contains(aaa.item.bitIndex),
3345+
);
3346+
b = Number(
3347+
// @ts-expect-error
3348+
this.searchIndexUnstable.get(bbb.item.crate).contains(bbb.item.bitIndex),
3349+
);
3350+
if (a !== b) {
3351+
return a - b;
3352+
}
3353+
33293354
// sort by crate (current crate comes first)
33303355
a = Number(aaa.item.crate !== preferredCrate);
33313356
b = Number(bbb.item.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+
];

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)