Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat other items as functions for the purpose of type-based search #131806

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/librustdoc/html/render/search_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,22 @@ 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_),
clean::StaticItem(ref s) => make_nullary_fn(&s.type_),
clean::StructFieldItem(ref t) => {
let Some(parent) = parent else {
return None;
};
let mut rgen: FxIndexMap<SimplifiedParam, (isize, Vec<RenderType>)> =
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,
};

Expand Down Expand Up @@ -1273,6 +1289,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<RenderType>, Vec<RenderType>, Vec<Vec<RenderType>>) {
let mut rgen: FxIndexMap<SimplifiedParam, (isize, Vec<RenderType>)> = 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<A: Display, B: Option<A>>(x: u32, y: B)` will return
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-gui/search-tab.goml
Original file line number Diff line number Diff line change
Expand Up @@ -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|})
Expand Down
6 changes: 6 additions & 0 deletions tests/rustdoc-js-std/const-is-nullary-func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const EXPECTED = {
'query': '-> char',
'others': [
{ 'path': 'std::char', 'name': 'MAX' },
],
}
7 changes: 7 additions & 0 deletions tests/rustdoc-js-std/field-is-unary-func.js
Original file line number Diff line number Diff line change
@@ -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' },
],
}
Loading