Skip to content

Commit ca33d5e

Browse files
committed
rustdoc-search: store the real name of type parameters
In order to show the type signature, this has to be stored somewhere.
1 parent c514471 commit ca33d5e

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ pub(crate) struct IndexItemFunctionType {
205205
inputs: Vec<RenderType>,
206206
output: Vec<RenderType>,
207207
where_clause: Vec<Vec<RenderType>>,
208+
param_names: Vec<Symbol>,
208209
}
209210

210211
impl IndexItemFunctionType {

src/librustdoc/html/render/search_index.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use encode::{bitmap_to_string, write_vlqhex_to_string};
4949
pub(crate) struct SerializedSearchIndex {
5050
pub(crate) index: String,
5151
pub(crate) desc: Vec<(usize, String)>,
52+
pub(crate) param_names: String,
5253
}
5354

5455
const DESC_INDEX_SHARD_LEN: usize = 128 * 1024;
@@ -681,6 +682,23 @@ pub(crate) fn build_index<'tcx>(
681682
desc.iter().map(|(len, _)| *len).sum::<usize>() + empty_desc.len()
682683
);
683684

685+
let param_names = {
686+
let result: Vec<Vec<&str>> = crate_items
687+
.iter()
688+
.map(|item| match &item.search_type {
689+
Some(ty) => ty.param_names.iter().map(|sym| sym.as_str()).collect(),
690+
None => Vec::new(),
691+
})
692+
.collect();
693+
serde_json::to_string(&result)
694+
.expect("failed serde conversion")
695+
// All these `replace` calls are because we have to go through JS string for JSON content.
696+
.replace('\\', r"\\")
697+
.replace('\'', r"\'")
698+
// We need to escape double quotes for the JSON.
699+
.replace("\\\"", "\\\\\"")
700+
};
701+
684702
// The index, which is actually used to search, is JSON
685703
// It uses `JSON.parse(..)` to actually load, since JSON
686704
// parses faster than the full JavaScript syntax.
@@ -702,7 +720,7 @@ pub(crate) fn build_index<'tcx>(
702720
// We need to escape double quotes for the JSON.
703721
.replace("\\\"", "\\\\\"")
704722
);
705-
SerializedSearchIndex { index, desc }
723+
SerializedSearchIndex { index, desc, param_names }
706724
}
707725

708726
pub(crate) fn get_function_type_for_search<'tcx>(
@@ -738,7 +756,7 @@ pub(crate) fn get_function_type_for_search<'tcx>(
738756
None
739757
}
740758
});
741-
let (mut inputs, mut output, where_clause) = match *item.kind {
759+
let (mut inputs, mut output, param_names, where_clause) = match *item.kind {
742760
clean::FunctionItem(ref f) | clean::MethodItem(ref f, _) | clean::TyMethodItem(ref f) => {
743761
get_fn_inputs_and_outputs(f, tcx, impl_or_trait_generics, cache)
744762
}
@@ -748,7 +766,7 @@ pub(crate) fn get_function_type_for_search<'tcx>(
748766
inputs.retain(|a| a.id.is_some() || a.generics.is_some());
749767
output.retain(|a| a.id.is_some() || a.generics.is_some());
750768

751-
Some(IndexItemFunctionType { inputs, output, where_clause })
769+
Some(IndexItemFunctionType { inputs, output, where_clause, param_names })
752770
}
753771

754772
fn get_index_type(
@@ -1250,7 +1268,7 @@ fn get_fn_inputs_and_outputs<'tcx>(
12501268
tcx: TyCtxt<'tcx>,
12511269
impl_or_trait_generics: Option<&(clean::Type, clean::Generics)>,
12521270
cache: &Cache,
1253-
) -> (Vec<RenderType>, Vec<RenderType>, Vec<Vec<RenderType>>) {
1271+
) -> (Vec<RenderType>, Vec<RenderType>, Vec<Symbol>, Vec<Vec<RenderType>>) {
12541272
let decl = &func.decl;
12551273

12561274
let mut rgen: FxHashMap<SimplifiedParam, (isize, Vec<RenderType>)> = Default::default();
@@ -1296,7 +1314,21 @@ fn get_fn_inputs_and_outputs<'tcx>(
12961314
let mut ret_types = Vec::new();
12971315
simplify_fn_type(self_, generics, &decl.output, tcx, 0, &mut ret_types, &mut rgen, true, cache);
12981316

1299-
let mut simplified_params = rgen.into_values().collect::<Vec<_>>();
1300-
simplified_params.sort_by_key(|(idx, _)| -idx);
1301-
(arg_types, ret_types, simplified_params.into_iter().map(|(_idx, traits)| traits).collect())
1317+
let mut simplified_params = rgen.into_iter().collect::<Vec<_>>();
1318+
simplified_params.sort_by_key(|(_, (idx, _))| -idx);
1319+
(
1320+
arg_types,
1321+
ret_types,
1322+
simplified_params
1323+
.iter()
1324+
.map(|(name, (_idx, _traits))| match name {
1325+
SimplifiedParam::Symbol(name) => *name,
1326+
SimplifiedParam::Anonymous(_) => kw::Empty,
1327+
SimplifiedParam::AssociatedType(def_id, name) => {
1328+
Symbol::intern(&format!("{}::{}", tcx.item_name(*def_id), name))
1329+
}
1330+
})
1331+
.collect(),
1332+
simplified_params.into_iter().map(|(_name, (_idx, traits))| traits).collect(),
1333+
)
13021334
}

src/librustdoc/html/render/write_shared.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,23 @@ else if (window.initSearch) window.initSearch(searchIndex);
361361
&path
362362
);
363363
}
364+
let output_filename = static_files::suffix_path(
365+
&format!("{kratename}-param-names.js"),
366+
&cx.shared.resource_suffix,
367+
);
368+
let path = search_desc_dir.join(output_filename);
369+
try_err!(
370+
std::fs::write(
371+
&path,
372+
&format!(
373+
r##"searchState.loadedParamNames({kratename}, JSON.parse('{data}'))"##,
374+
kratename = serde_json::to_string(&kratename).unwrap(),
375+
data = search_index.param_names,
376+
)
377+
.into_bytes()
378+
),
379+
&path
380+
);
364381

365382
write_invocation_specific("crates.js", &|| {
366383
let krates = krates.iter().map(|k| format!("\"{k}\"")).join(",");

0 commit comments

Comments
 (0)