Skip to content

Commit

Permalink
rustdoc-search: add type param names to index
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Oct 30, 2024
1 parent 298c746 commit 488d5b0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pub(crate) struct IndexItemFunctionType {
inputs: Vec<RenderType>,
output: Vec<RenderType>,
where_clause: Vec<Vec<RenderType>>,
param_names: Vec<Symbol>,
}

impl IndexItemFunctionType {
Expand Down
45 changes: 38 additions & 7 deletions src/librustdoc/html/render/search_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,25 @@ pub(crate) fn build_index<'tcx>(
full_paths.push((*index, path));
}

let param_names: Vec<(usize, String)> = {
let mut prev = Vec::new();
let mut result = Vec::new();
for (index, item) in self.items.iter().enumerate() {
if let Some(ty) = &item.search_type
&& let my =
ty.param_names.iter().map(|sym| sym.as_str()).collect::<Vec<_>>()
&& my != prev
{
result.push((index, my.join(",")));
prev = my;
}
}
result
};

let has_aliases = !self.aliases.is_empty();
let mut crate_data =
serializer.serialize_struct("CrateData", if has_aliases { 9 } else { 8 })?;
serializer.serialize_struct("CrateData", if has_aliases { 13 } else { 12 })?;
crate_data.serialize_field("t", &types)?;
crate_data.serialize_field("n", &names)?;
crate_data.serialize_field("q", &full_paths)?;
Expand All @@ -660,6 +676,7 @@ pub(crate) fn build_index<'tcx>(
crate_data.serialize_field("b", &self.associated_item_disambiguators)?;
crate_data.serialize_field("c", &bitmap_to_string(&deprecated))?;
crate_data.serialize_field("e", &bitmap_to_string(&self.empty_desc))?;
crate_data.serialize_field("P", &param_names)?;
if has_aliases {
crate_data.serialize_field("a", &self.aliases)?;
}
Expand Down Expand Up @@ -758,7 +775,7 @@ pub(crate) fn get_function_type_for_search<'tcx>(
None
}
});
let (mut inputs, mut output, where_clause) = match item.kind {
let (mut inputs, mut output, param_names, where_clause) = match item.kind {
clean::ForeignFunctionItem(ref f, _)
| clean::FunctionItem(ref f)
| clean::MethodItem(ref f, _)
Expand All @@ -771,7 +788,7 @@ pub(crate) fn get_function_type_for_search<'tcx>(
inputs.retain(|a| a.id.is_some() || a.generics.is_some());
output.retain(|a| a.id.is_some() || a.generics.is_some());

Some(IndexItemFunctionType { inputs, output, where_clause })
Some(IndexItemFunctionType { inputs, output, where_clause, param_names })
}

fn get_index_type(
Expand Down Expand Up @@ -1285,7 +1302,7 @@ fn get_fn_inputs_and_outputs<'tcx>(
tcx: TyCtxt<'tcx>,
impl_or_trait_generics: Option<&(clean::Type, clean::Generics)>,
cache: &Cache,
) -> (Vec<RenderType>, Vec<RenderType>, Vec<Vec<RenderType>>) {
) -> (Vec<RenderType>, Vec<RenderType>, Vec<Symbol>, Vec<Vec<RenderType>>) {
let decl = &func.decl;

let mut rgen: FxIndexMap<SimplifiedParam, (isize, Vec<RenderType>)> = Default::default();
Expand Down Expand Up @@ -1331,7 +1348,21 @@ fn get_fn_inputs_and_outputs<'tcx>(
let mut ret_types = Vec::new();
simplify_fn_type(self_, generics, &decl.output, tcx, 0, &mut ret_types, &mut rgen, true, cache);

let mut simplified_params = rgen.into_values().collect::<Vec<_>>();
simplified_params.sort_by_key(|(idx, _)| -idx);
(arg_types, ret_types, simplified_params.into_iter().map(|(_idx, traits)| traits).collect())
let mut simplified_params = rgen.into_iter().collect::<Vec<_>>();
simplified_params.sort_by_key(|(_, (idx, _))| -idx);
(
arg_types,
ret_types,
simplified_params
.iter()
.map(|(name, (_idx, _traits))| match name {
SimplifiedParam::Symbol(name) => *name,
SimplifiedParam::Anonymous(_) => kw::Empty,
SimplifiedParam::AssociatedType(def_id, name) => {
Symbol::intern(&format!("{}::{}", tcx.item_name(*def_id), name))
}
})
.collect(),
simplified_params.into_iter().map(|(_name, (_idx, traits))| traits).collect(),
)
}

0 comments on commit 488d5b0

Please sign in to comment.