Skip to content

Commit 0e41194

Browse files
authored
Rollup merge of #92340 - camelid:search-index-cleanup, r=GuillaumeGomez
rustdoc: Start cleaning up search index generation I'm trying to simplify and clean up the code, partly to make #90779 easier. r? `@GuillaumeGomez`
2 parents f044c6c + 908a9d4 commit 0e41194

File tree

8 files changed

+65
-50
lines changed

8 files changed

+65
-50
lines changed

src/librustdoc/clean/types.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use crate::clean::Clean;
3939
use crate::core::DocContext;
4040
use crate::formats::cache::Cache;
4141
use crate::formats::item_type::ItemType;
42-
use crate::html::render::cache::ExternalLocation;
4342
use crate::html::render::Context;
4443
use crate::passes::collect_intra_doc_links::UrlFragment;
4544

@@ -339,6 +338,16 @@ impl ExternalCrate {
339338
}
340339
}
341340

341+
/// Indicates where an external crate can be found.
342+
crate enum ExternalLocation {
343+
/// Remote URL root of the external crate
344+
Remote(String),
345+
/// This external crate can be found in the local doc/ folder
346+
Local,
347+
/// The external crate could not be found.
348+
Unknown,
349+
}
350+
342351
/// Anything with a source location and set of attributes and, optionally, a
343352
/// name. That is, anything that can be documented. This doesn't correspond
344353
/// directly to the AST's concept of an item; it's a strict superset.

src/librustdoc/formats/cache.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use rustc_middle::middle::privacy::AccessLevels;
66
use rustc_middle::ty::TyCtxt;
77
use rustc_span::symbol::sym;
88

9-
use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
9+
use crate::clean::{self, types::ExternalLocation, ExternalCrate, ItemId, PrimitiveType};
1010
use crate::core::DocContext;
1111
use crate::fold::DocFolder;
1212
use crate::formats::item_type::ItemType;
1313
use crate::formats::Impl;
1414
use crate::html::markdown::short_markdown_summary;
15-
use crate::html::render::cache::{get_index_search_type, ExternalLocation};
15+
use crate::html::render::search_index::get_function_type_for_search;
1616
use crate::html::render::IndexItem;
1717

1818
/// This cache is used to store information about the [`clean::Crate`] being
@@ -303,7 +303,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
303303
desc,
304304
parent,
305305
parent_idx: None,
306-
search_type: get_index_search_type(&item, self.tcx, self.cache),
306+
search_type: get_function_type_for_search(&item, self.tcx),
307307
aliases: item.attrs.get_doc_aliases(),
308308
});
309309
}

src/librustdoc/html/format.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ use rustc_middle::ty::TyCtxt;
2121
use rustc_span::def_id::CRATE_DEF_INDEX;
2222
use rustc_target::spec::abi::Abi;
2323

24-
use crate::clean::{self, utils::find_nearest_parent_module, ExternalCrate, ItemId, PrimitiveType};
24+
use crate::clean::{
25+
self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId,
26+
PrimitiveType,
27+
};
2528
use crate::formats::item_type::ItemType;
2629
use crate::html::escape::Escape;
27-
use crate::html::render::cache::ExternalLocation;
2830
use crate::html::render::Context;
2931

3032
use super::url_parts_builder::UrlPartsBuilder;

src/librustdoc/html/render/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ use rustc_span::edition::Edition;
1313
use rustc_span::source_map::FileName;
1414
use rustc_span::symbol::sym;
1515

16-
use super::cache::{build_index, ExternalLocation};
1716
use super::print_item::{full_path, item_path, print_item};
17+
use super::search_index::build_index;
1818
use super::templates;
1919
use super::write_shared::write_shared;
2020
use super::{
2121
collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath,
2222
BASIC_KEYWORDS,
2323
};
2424

25-
use crate::clean::{self, ExternalCrate};
25+
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
2626
use crate::config::RenderOptions;
2727
use crate::docfs::{DocFS, PathError};
2828
use crate::error::Error;

src/librustdoc/html/render/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//! These threads are not parallelized (they haven't been a bottleneck yet), and
2424
//! both occur before the crate is rendered.
2525
26-
crate mod cache;
26+
crate mod search_index;
2727

2828
#[cfg(test)]
2929
mod tests;

src/librustdoc/html/render/cache.rs renamed to src/librustdoc/html/render/search_index.rs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,12 @@ use rustc_span::symbol::Symbol;
77
use serde::ser::{Serialize, SerializeStruct, Serializer};
88

99
use crate::clean;
10-
use crate::clean::types::{FnDecl, FnRetTy, GenericBound, Generics, Type, WherePredicate};
10+
use crate::clean::types::{FnRetTy, Function, GenericBound, Generics, Type, WherePredicate};
1111
use crate::formats::cache::Cache;
1212
use crate::formats::item_type::ItemType;
1313
use crate::html::markdown::short_markdown_summary;
1414
use crate::html::render::{IndexItem, IndexItemFunctionType, RenderType, TypeWithKind};
1515

16-
/// Indicates where an external crate can be found.
17-
crate enum ExternalLocation {
18-
/// Remote URL root of the external crate
19-
Remote(String),
20-
/// This external crate can be found in the local doc/ folder
21-
Local,
22-
/// The external crate could not be found.
23-
Unknown,
24-
}
25-
2616
/// Builds the search index from the collected metadata
2717
crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<'tcx>) -> String {
2818
let mut defid_to_pathid = FxHashMap::default();
@@ -42,7 +32,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
4232
desc,
4333
parent: Some(did),
4434
parent_idx: None,
45-
search_type: get_index_search_type(item, tcx, cache),
35+
search_type: get_function_type_for_search(item, tcx),
4636
aliases: item.attrs.get_doc_aliases(),
4737
});
4838
}
@@ -191,15 +181,14 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
191181
)
192182
}
193183

194-
crate fn get_index_search_type<'tcx>(
184+
crate fn get_function_type_for_search<'tcx>(
195185
item: &clean::Item,
196186
tcx: TyCtxt<'tcx>,
197-
cache: &Cache,
198187
) -> Option<IndexItemFunctionType> {
199188
let (mut inputs, mut output) = match *item.kind {
200-
clean::FunctionItem(ref f) => get_all_types(&f.generics, &f.decl, tcx, cache),
201-
clean::MethodItem(ref m, _) => get_all_types(&m.generics, &m.decl, tcx, cache),
202-
clean::TyMethodItem(ref m) => get_all_types(&m.generics, &m.decl, tcx, cache),
189+
clean::FunctionItem(ref f) => get_fn_inputs_and_outputs(f, tcx),
190+
clean::MethodItem(ref m, _) => get_fn_inputs_and_outputs(m, tcx),
191+
clean::TyMethodItem(ref m) => get_fn_inputs_and_outputs(m, tcx),
203192
_ => return None,
204193
};
205194

@@ -211,12 +200,12 @@ crate fn get_index_search_type<'tcx>(
211200

212201
fn get_index_type(clean_type: &clean::Type, generics: Vec<TypeWithKind>) -> RenderType {
213202
RenderType {
214-
name: get_index_type_name(clean_type, true).map(|s| s.as_str().to_ascii_lowercase()),
203+
name: get_index_type_name(clean_type).map(|s| s.as_str().to_ascii_lowercase()),
215204
generics: if generics.is_empty() { None } else { Some(generics) },
216205
}
217206
}
218207

219-
fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option<Symbol> {
208+
fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
220209
match *clean_type {
221210
clean::Type::Path { ref path, .. } => {
222211
let path_segment = path.segments.last().unwrap();
@@ -226,11 +215,10 @@ fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option
226215
let path = &bounds[0].trait_;
227216
Some(path.segments.last().unwrap().name)
228217
}
229-
clean::Generic(s) if accept_generic => Some(s),
218+
clean::Generic(s) => Some(s),
230219
clean::Primitive(ref p) => Some(p.as_sym()),
231-
clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_, accept_generic),
232-
clean::Generic(_)
233-
| clean::BareFunction(_)
220+
clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_),
221+
clean::BareFunction(_)
234222
| clean::Tuple(_)
235223
| clean::Slice(_)
236224
| clean::Array(_, _)
@@ -248,20 +236,19 @@ fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option
248236
///
249237
/// Important note: It goes through generics recursively. So if you have
250238
/// `T: Option<Result<(), ()>>`, it'll go into `Option` and then into `Result`.
251-
crate fn get_real_types<'tcx>(
239+
#[instrument(level = "trace", skip(tcx, res))]
240+
fn add_generics_and_bounds_as_types<'tcx>(
252241
generics: &Generics,
253242
arg: &Type,
254243
tcx: TyCtxt<'tcx>,
255244
recurse: usize,
256245
res: &mut Vec<TypeWithKind>,
257-
cache: &Cache,
258246
) {
259247
fn insert_ty(
260248
res: &mut Vec<TypeWithKind>,
261249
tcx: TyCtxt<'_>,
262250
ty: Type,
263251
mut generics: Vec<TypeWithKind>,
264-
_cache: &Cache,
265252
) {
266253
let is_full_generic = ty.is_full_generic();
267254

@@ -330,6 +317,7 @@ crate fn get_real_types<'tcx>(
330317

331318
if recurse >= 10 {
332319
// FIXME: remove this whole recurse thing when the recursion bug is fixed
320+
// See #59502 for the original issue.
333321
return;
334322
}
335323

@@ -350,32 +338,37 @@ crate fn get_real_types<'tcx>(
350338
for param_def in poly_trait.generic_params.iter() {
351339
match &param_def.kind {
352340
clean::GenericParamDefKind::Type { default: Some(ty), .. } => {
353-
get_real_types(
341+
add_generics_and_bounds_as_types(
354342
generics,
355343
ty,
356344
tcx,
357345
recurse + 1,
358346
&mut ty_generics,
359-
cache,
360347
)
361348
}
362349
_ => {}
363350
}
364351
}
365352
}
366353
}
367-
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
354+
insert_ty(res, tcx, arg.clone(), ty_generics);
368355
}
369356
// Otherwise we check if the trait bounds are "inlined" like `T: Option<u32>`...
370357
if let Some(bound) = generics.params.iter().find(|g| g.is_type() && g.name == arg_s) {
371358
let mut ty_generics = Vec::new();
372359
for bound in bound.get_bounds().unwrap_or(&[]) {
373360
if let Some(path) = bound.get_trait_path() {
374361
let ty = Type::Path { path };
375-
get_real_types(generics, &ty, tcx, recurse + 1, &mut ty_generics, cache);
362+
add_generics_and_bounds_as_types(
363+
generics,
364+
&ty,
365+
tcx,
366+
recurse + 1,
367+
&mut ty_generics,
368+
);
376369
}
377370
}
378-
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
371+
insert_ty(res, tcx, arg.clone(), ty_generics);
379372
}
380373
} else {
381374
// This is not a type parameter. So for example if we have `T, U: Option<T>`, and we're
@@ -386,30 +379,31 @@ crate fn get_real_types<'tcx>(
386379
let mut ty_generics = Vec::new();
387380
if let Some(arg_generics) = arg.generics() {
388381
for gen in arg_generics.iter() {
389-
get_real_types(generics, gen, tcx, recurse + 1, &mut ty_generics, cache);
382+
add_generics_and_bounds_as_types(generics, gen, tcx, recurse + 1, &mut ty_generics);
390383
}
391384
}
392-
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
385+
insert_ty(res, tcx, arg.clone(), ty_generics);
393386
}
394387
}
395388

396389
/// Return the full list of types when bounds have been resolved.
397390
///
398391
/// i.e. `fn foo<A: Display, B: Option<A>>(x: u32, y: B)` will return
399392
/// `[u32, Display, Option]`.
400-
crate fn get_all_types<'tcx>(
401-
generics: &Generics,
402-
decl: &FnDecl,
393+
fn get_fn_inputs_and_outputs<'tcx>(
394+
func: &Function,
403395
tcx: TyCtxt<'tcx>,
404-
cache: &Cache,
405396
) -> (Vec<TypeWithKind>, Vec<TypeWithKind>) {
397+
let decl = &func.decl;
398+
let generics = &func.generics;
399+
406400
let mut all_types = Vec::new();
407401
for arg in decl.inputs.values.iter() {
408402
if arg.type_.is_self_type() {
409403
continue;
410404
}
411405
let mut args = Vec::new();
412-
get_real_types(generics, &arg.type_, tcx, 0, &mut args, cache);
406+
add_generics_and_bounds_as_types(generics, &arg.type_, tcx, 0, &mut args);
413407
if !args.is_empty() {
414408
all_types.extend(args);
415409
} else {
@@ -423,7 +417,7 @@ crate fn get_all_types<'tcx>(
423417
let mut ret_types = Vec::new();
424418
match decl.output {
425419
FnRetTy::Return(ref return_type) => {
426-
get_real_types(generics, return_type, tcx, 0, &mut ret_types, cache);
420+
add_generics_and_bounds_as_types(generics, return_type, tcx, 0, &mut ret_types);
427421
if ret_types.is_empty() {
428422
if let Some(kind) =
429423
return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into())

src/librustdoc/json/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ use rustc_session::Session;
1919
use rustdoc_json_types as types;
2020

2121
use crate::clean;
22-
use crate::clean::ExternalCrate;
22+
use crate::clean::types::{ExternalCrate, ExternalLocation};
2323
use crate::config::RenderOptions;
2424
use crate::error::Error;
2525
use crate::formats::cache::Cache;
2626
use crate::formats::FormatRenderer;
27-
use crate::html::render::cache::ExternalLocation;
2827
use crate::json::conversions::{from_item_id, IntoWithTcx};
2928

3029
#[derive(Clone)]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// check-pass
2+
3+
// Minimization of issue #59502
4+
5+
trait MyTrait<T> {
6+
type Output;
7+
}
8+
9+
pub fn pow<T: MyTrait<T, Output = T>>(arg: T) -> T {
10+
arg
11+
}

0 commit comments

Comments
 (0)