diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 56f10b03d62de..dd1fecea6872f 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -594,6 +594,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It ); let aliases = item.attrs.get_doc_aliases(); let deprecation = item.deprecation(tcx); + let stability = item.stability(tcx); let index_item = IndexItem { ty: item.type_(), defid: Some(defid), @@ -609,6 +610,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It search_type, aliases, deprecation, + stability, }; cache.search_index.push(index_item); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 9c8e599104be4..2601208f4f841 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -142,6 +142,7 @@ pub(crate) struct IndexItem { pub(crate) search_type: Option, pub(crate) aliases: Box<[Symbol]>, pub(crate) deprecation: Option, + pub(crate) stability: Option, } /// A type used for the search index. diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 3514c517d9134..2930ca44327e7 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -611,6 +611,7 @@ impl SerializedSearchIndex { parent, trait_parent, deprecated, + unstable, associated_item_disambiguator, }| EntryData { krate: *map.get(krate).unwrap(), @@ -621,6 +622,7 @@ impl SerializedSearchIndex { parent: parent.and_then(|path_id| map.get(&path_id).copied()), trait_parent: trait_parent.and_then(|path_id| map.get(&path_id).copied()), deprecated: *deprecated, + unstable: *unstable, associated_item_disambiguator: associated_item_disambiguator.clone(), }, ), @@ -889,6 +891,7 @@ struct EntryData { parent: Option, trait_parent: Option, deprecated: bool, + unstable: bool, associated_item_disambiguator: Option, } @@ -905,6 +908,7 @@ impl Serialize for EntryData { seq.serialize_element(&self.parent.map(|id| id + 1).unwrap_or(0))?; seq.serialize_element(&self.trait_parent.map(|id| id + 1).unwrap_or(0))?; seq.serialize_element(&if self.deprecated { 1 } else { 0 })?; + seq.serialize_element(&if self.unstable { 1 } else { 0 })?; if let Some(disambig) = &self.associated_item_disambiguator { seq.serialize_element(&disambig)?; } @@ -939,6 +943,7 @@ impl<'de> Deserialize<'de> for EntryData { v.next_element()?.ok_or_else(|| A::Error::missing_field("trait_parent"))?; let deprecated: u32 = v.next_element()?.unwrap_or(0); + let unstable: u32 = v.next_element()?.unwrap_or(0); let associated_item_disambiguator: Option = v.next_element()?; Ok(EntryData { krate, @@ -949,6 +954,7 @@ impl<'de> Deserialize<'de> for EntryData { parent: Option::::from(parent).map(|path| path as usize), trait_parent: Option::::from(trait_parent).map(|path| path as usize), deprecated: deprecated != 0, + unstable: unstable != 0, associated_item_disambiguator, }) } @@ -1275,6 +1281,7 @@ pub(crate) fn build_index( ), aliases: item.attrs.get_doc_aliases(), deprecation: item.deprecation(tcx), + stability: item.stability(tcx), }); } } @@ -1370,6 +1377,7 @@ pub(crate) fn build_index( parent: None, trait_parent: None, deprecated: false, + unstable: false, associated_item_disambiguator: None, }), crate_doc, @@ -1508,6 +1516,7 @@ pub(crate) fn build_index( module_path, exact_module_path, deprecated: item.deprecation.is_some(), + unstable: item.stability.is_some_and(|x| x.is_unstable()), associated_item_disambiguator: if let Some(impl_id) = item.impl_id && let Some(parent_idx) = item.parent_idx && associated_item_duplicates diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index e206d6633e630..f92d81d6161a8 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -148,7 +148,7 @@ declare namespace rustdoc { /** * A single parsed "atom" in a search query. For example, - * + * * std::fmt::Formatter, Write -> Result<()> * ┏━━━━━━━━━━━━━━━━━━ ┌──── ┏━━━━━┅┅┅┅┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐ * ┃ │ ┗ QueryElement { ┊ @@ -243,6 +243,7 @@ declare namespace rustdoc { parent: number?, traitParent: number?, deprecated: boolean, + unstable: boolean, associatedItemDisambiguator: string?, } @@ -292,6 +293,7 @@ declare namespace rustdoc { path: PathData?, functionData: FunctionData?, deprecated: boolean, + unstable: boolean, parent: RowParent, traitParent: RowParent, } diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 2b381d64547b5..017cd1bb9b9fe 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -1633,6 +1633,7 @@ class DocSearch { * parent, * trait_parent, * deprecated, + * unstable, * associated_item_disambiguator * @type {rustdoc.ArrayWithOptionals<[ * number, @@ -1642,6 +1643,7 @@ class DocSearch { * number, * number, * number, + * number, * ], [string]>} */ const raw = JSON.parse(encoded); @@ -1653,7 +1655,8 @@ class DocSearch { parent: raw[4] === 0 ? null : raw[4] - 1, traitParent: raw[5] === 0 ? null : raw[5] - 1, deprecated: raw[6] === 1 ? true : false, - associatedItemDisambiguator: raw.length === 7 ? null : raw[7], + unstable: raw[7] === 1 ? true : false, + associatedItemDisambiguator: raw.length === 8 ? null : raw[8], }; } @@ -1946,6 +1949,7 @@ class DocSearch { path, functionData, deprecated: entry ? entry.deprecated : false, + unstable: entry ? entry.unstable : false, parent, traitParent, }; @@ -2867,6 +2871,13 @@ class DocSearch { return a - b; } + // sort unstable items later + a = Number(aai.unstable); + b = Number(bbi.unstable); + if (a !== b) { + return a - b; + } + // sort by crate (current crate comes first) a = Number(aai.crate !== preferredCrate); b = Number(bbi.crate !== preferredCrate); diff --git a/tests/rustdoc-js/sort-stability.js b/tests/rustdoc-js/sort-stability.js new file mode 100644 index 0000000000000..8c095619a0866 --- /dev/null +++ b/tests/rustdoc-js/sort-stability.js @@ -0,0 +1,9 @@ +const EXPECTED = [ + { + 'query': 'foo', + 'others': [ + {"path": "sort_stability::old", "name": "foo"}, + {"path": "sort_stability::new", "name": "foo"}, + ], + }, +]; diff --git a/tests/rustdoc-js/sort-stability.rs b/tests/rustdoc-js/sort-stability.rs new file mode 100644 index 0000000000000..68662bb3aab6c --- /dev/null +++ b/tests/rustdoc-js/sort-stability.rs @@ -0,0 +1,16 @@ +#![feature(staged_api)] +#![stable(feature = "foo_lib", since = "1.0.0")] + +#[stable(feature = "old_foo", since = "1.0.1")] +pub mod old { + /// Old, stable foo + #[stable(feature = "old_foo", since = "1.0.1")] + pub fn foo() {} +} + +#[unstable(feature = "new_foo", issue = "none")] +pub mod new { + /// New, unstable foo + #[unstable(feature = "new_foo", issue = "none")] + pub fn foo() {} +}