Skip to content

Commit 668af26

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 80eb5a8 commit 668af26

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
@@ -200,6 +200,7 @@ pub(crate) struct IndexItemFunctionType {
200200
inputs: Vec<RenderType>,
201201
output: Vec<RenderType>,
202202
where_clause: Vec<Vec<RenderType>>,
203+
param_names: Vec<Symbol>,
203204
}
204205

205206
impl IndexItemFunctionType {

src/librustdoc/html/render/search_index.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, Re
4848
pub(crate) struct SerializedSearchIndex {
4949
pub(crate) index: String,
5050
pub(crate) desc: Vec<(usize, String)>,
51+
pub(crate) param_names: String,
5152
}
5253

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

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

707725
pub(crate) fn get_function_type_for_search<'tcx>(
@@ -737,7 +755,7 @@ pub(crate) fn get_function_type_for_search<'tcx>(
737755
None
738756
}
739757
});
740-
let (mut inputs, mut output, where_clause) = match *item.kind {
758+
let (mut inputs, mut output, param_names, where_clause) = match *item.kind {
741759
clean::FunctionItem(ref f) | clean::MethodItem(ref f, _) | clean::TyMethodItem(ref f) => {
742760
get_fn_inputs_and_outputs(f, tcx, impl_or_trait_generics, cache)
743761
}
@@ -747,7 +765,7 @@ pub(crate) fn get_function_type_for_search<'tcx>(
747765
inputs.retain(|a| a.id.is_some() || a.generics.is_some());
748766
output.retain(|a| a.id.is_some() || a.generics.is_some());
749767

750-
Some(IndexItemFunctionType { inputs, output, where_clause })
768+
Some(IndexItemFunctionType { inputs, output, where_clause, param_names })
751769
}
752770

753771
fn get_index_type(
@@ -1262,7 +1280,7 @@ fn get_fn_inputs_and_outputs<'tcx>(
12621280
tcx: TyCtxt<'tcx>,
12631281
impl_or_trait_generics: Option<&(clean::Type, clean::Generics)>,
12641282
cache: &Cache,
1265-
) -> (Vec<RenderType>, Vec<RenderType>, Vec<Vec<RenderType>>) {
1283+
) -> (Vec<RenderType>, Vec<RenderType>, Vec<Symbol>, Vec<Vec<RenderType>>) {
12661284
let decl = &func.decl;
12671285

12681286
let mut rgen: FxHashMap<SimplifiedParam, (isize, Vec<RenderType>)> = Default::default();
@@ -1308,7 +1326,21 @@ fn get_fn_inputs_and_outputs<'tcx>(
13081326
let mut ret_types = Vec::new();
13091327
simplify_fn_type(self_, generics, &decl.output, tcx, 0, &mut ret_types, &mut rgen, true, cache);
13101328

1311-
let mut simplified_params = rgen.into_values().collect::<Vec<_>>();
1312-
simplified_params.sort_by_key(|(idx, _)| -idx);
1313-
(arg_types, ret_types, simplified_params.into_iter().map(|(_idx, traits)| traits).collect())
1329+
let mut simplified_params = rgen.into_iter().collect::<Vec<_>>();
1330+
simplified_params.sort_by_key(|(_, (idx, _))| -idx);
1331+
(
1332+
arg_types,
1333+
ret_types,
1334+
simplified_params
1335+
.iter()
1336+
.map(|(name, (_idx, _traits))| match name {
1337+
SimplifiedParam::Symbol(name) => *name,
1338+
SimplifiedParam::Anonymous(_) => kw::Empty,
1339+
SimplifiedParam::AssociatedType(def_id, name) => {
1340+
Symbol::intern(&format!("{}::{}", tcx.item_name(*def_id), name))
1341+
}
1342+
})
1343+
.collect(),
1344+
simplified_params.into_iter().map(|(_name, (_idx, traits))| traits).collect(),
1345+
)
13141346
}

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)