From c2223303bd5f46486f613140c49afde549c9b461 Mon Sep 17 00:00:00 2001 From: binarycat Date: Wed, 16 Oct 2024 16:19:51 -0500 Subject: [PATCH 1/4] rustdoc search: constants are treated as nullary functions --- src/librustdoc/html/render/search_index.rs | 12 ++++++++++++ tests/rustdoc-js-std/const-is-nullary-func.js | 6 ++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/rustdoc-js-std/const-is-nullary-func.js diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index c958458b662c2..f7989291d589f 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -762,6 +762,7 @@ pub(crate) fn get_function_type_for_search<'tcx>( clean::FunctionItem(ref f) | clean::MethodItem(ref f, _) | clean::TyMethodItem(ref f) => { get_fn_inputs_and_outputs(f, tcx, impl_or_trait_generics, cache) } + clean::ConstantItem(ref c) => make_nullary_fn(&c.type_), _ => return None, }; @@ -1273,6 +1274,17 @@ fn simplify_fn_constraint<'a, 'tcx>( res.push((ty_constrained_assoc, ty_constraints)); } +/// Create a fake nullary function. +/// +/// Used to allow type-based search on constants and statics. +fn make_nullary_fn( + clean_type: &clean::Type, +) -> (Vec, Vec, Vec>) { + let mut rgen: FxIndexMap)> = Default::default(); + let output = get_index_type(clean_type, vec![], &mut rgen); + (vec![], vec![output], vec![]) +} + /// Return the full list of types when bounds have been resolved. /// /// i.e. `fn foo>(x: u32, y: B)` will return diff --git a/tests/rustdoc-js-std/const-is-nullary-func.js b/tests/rustdoc-js-std/const-is-nullary-func.js new file mode 100644 index 0000000000000..c0662206cafa8 --- /dev/null +++ b/tests/rustdoc-js-std/const-is-nullary-func.js @@ -0,0 +1,6 @@ +const EXPECTED = { + 'query': '-> char', + 'others': [ + { 'path': 'std::char', 'name': 'MAX' }, + ], +} From 0b4b586c2a1e755dd6f0928d2fb0c5479f02c9cf Mon Sep 17 00:00:00 2001 From: binarycat Date: Wed, 16 Oct 2024 17:44:41 -0500 Subject: [PATCH 2/4] rustdoc: fields are treated as unary functions statics are treated as nullary functions --- src/librustdoc/html/render/search_index.rs | 15 +++++++++++++++ tests/rustdoc-js-std/const-is-nullary-func.js | 8 ++++---- tests/rustdoc-js-std/field-is-unary-func.js | 7 +++++++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 tests/rustdoc-js-std/field-is-unary-func.js diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index f7989291d589f..88b517ddc0958 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -763,6 +763,21 @@ pub(crate) fn get_function_type_for_search<'tcx>( get_fn_inputs_and_outputs(f, tcx, impl_or_trait_generics, cache) } clean::ConstantItem(ref c) => make_nullary_fn(&c.type_), + clean::StaticItem(ref s) => make_nullary_fn(&s.type_), + clean::StructFieldItem(ref t) => { + let Some(parent) = parent else { + return None; + }; + let mut rgen: FxIndexMap)> = + Default::default(); + let output = get_index_type(t, vec![], &mut rgen); + let input = RenderType { + id: Some(RenderTypeId::DefId(parent)), + generics: None, + bindings: None, + }; + (vec![input], vec![output], vec![]) + } _ => return None, }; diff --git a/tests/rustdoc-js-std/const-is-nullary-func.js b/tests/rustdoc-js-std/const-is-nullary-func.js index c0662206cafa8..67dca4e9ee1e5 100644 --- a/tests/rustdoc-js-std/const-is-nullary-func.js +++ b/tests/rustdoc-js-std/const-is-nullary-func.js @@ -1,6 +1,6 @@ const EXPECTED = { - 'query': '-> char', - 'others': [ - { 'path': 'std::char', 'name': 'MAX' }, - ], + 'query': '-> char', + 'others': [ + { 'path': 'std::char', 'name': 'MAX' }, + ], } diff --git a/tests/rustdoc-js-std/field-is-unary-func.js b/tests/rustdoc-js-std/field-is-unary-func.js new file mode 100644 index 0000000000000..bda19eee0ec6d --- /dev/null +++ b/tests/rustdoc-js-std/field-is-unary-func.js @@ -0,0 +1,7 @@ +const EXPECTED = { + // one of the only non-generic structs with public fields + 'query': 'CpuidResult -> u32' + 'others': [ + { 'path': 'core::arch::x86::CpuidResult', 'name': 'eax' }, + ], +} From 272fcc6fed1931484e5a04a3aaf2c326549ef9d0 Mon Sep 17 00:00:00 2001 From: binarycat Date: Wed, 16 Oct 2024 19:05:43 -0500 Subject: [PATCH 3/4] adjust count in ui test --- tests/rustdoc-gui/search-tab.goml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rustdoc-gui/search-tab.goml b/tests/rustdoc-gui/search-tab.goml index 4329726398c10..9ddaeb5f80cc7 100644 --- a/tests/rustdoc-gui/search-tab.goml +++ b/tests/rustdoc-gui/search-tab.goml @@ -79,7 +79,7 @@ set-window-size: (851, 600) // Check the size and count in tabs assert-text: ("#search-tabs > button:nth-child(1) > .count", " (25) ") -assert-text: ("#search-tabs > button:nth-child(2) > .count", " (6)  ") +assert-text: ("#search-tabs > button:nth-child(2) > .count", " (7)  ") assert-text: ("#search-tabs > button:nth-child(3) > .count", " (0)  ") store-property: ("#search-tabs > button:nth-child(1)", {"offsetWidth": buttonWidth}) assert-property: ("#search-tabs > button:nth-child(2)", {"offsetWidth": |buttonWidth|}) From b3e0da7f1fd22cbf39325257e580857142a4f99b Mon Sep 17 00:00:00 2001 From: binarycat Date: Wed, 16 Oct 2024 20:08:55 -0500 Subject: [PATCH 4/4] fix typo in search test --- tests/rustdoc-js-std/field-is-unary-func.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rustdoc-js-std/field-is-unary-func.js b/tests/rustdoc-js-std/field-is-unary-func.js index bda19eee0ec6d..09ce8a0dde0a4 100644 --- a/tests/rustdoc-js-std/field-is-unary-func.js +++ b/tests/rustdoc-js-std/field-is-unary-func.js @@ -1,6 +1,6 @@ const EXPECTED = { // one of the only non-generic structs with public fields - 'query': 'CpuidResult -> u32' + 'query': 'CpuidResult -> u32', 'others': [ { 'path': 'core::arch::x86::CpuidResult', 'name': 'eax' }, ],