Skip to content

Commit f522f26

Browse files
authored
Rollup merge of #90795 - GuillaumeGomez:more-search-index-comments, r=notriddle
Add more comments to explain the code to generate the search index Fixes #90766. I tried to put comments when the code wasn't easy to understand at first sight and added more documentation on the recursive function. Please tell me if I misused the terminology or if comments can be improved or added into other places. r? `@notriddle`
2 parents 640f365 + 9c05335 commit f522f26

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/librustdoc/html/render/cache.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,10 @@ fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option
244244
/// The point of this function is to replace bounds with types.
245245
///
246246
/// i.e. `[T, U]` when you have the following bounds: `T: Display, U: Option<T>` will return
247-
/// `[Display, Option]` (we just returns the list of the types, we don't care about the
248-
/// wrapped types in here).
247+
/// `[Display, Option]`. If a type parameter has no trait bound, it is discarded.
248+
///
249+
/// Important note: It goes through generics recursively. So if you have
250+
/// `T: Option<Result<(), ()>>`, it'll go into `Option` and then into `Result`.
249251
crate fn get_real_types<'tcx>(
250252
generics: &Generics,
251253
arg: &Type,
@@ -329,7 +331,10 @@ crate fn get_real_types<'tcx>(
329331
return;
330332
}
331333

334+
// If this argument is a type parameter and not a trait bound or a type, we need to look
335+
// for its bounds.
332336
if let Type::Generic(arg_s) = *arg {
337+
// First we check if the bounds are in a `where` predicate...
333338
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
334339
WherePredicate::BoundPredicate { ty, .. } => {
335340
ty.def_id_no_primitives() == arg.def_id_no_primitives()
@@ -352,6 +357,7 @@ crate fn get_real_types<'tcx>(
352357
}
353358
insert_ty(res, tcx, arg.clone(), ty_generics);
354359
}
360+
// Otherwise we check if the trait bounds are "inlined" like `T: Option<u32>`...
355361
if let Some(bound) = generics.params.iter().find(|g| g.is_type() && g.name == arg_s) {
356362
let mut ty_generics = Vec::new();
357363
for bound in bound.get_bounds().unwrap_or(&[]) {
@@ -363,6 +369,11 @@ crate fn get_real_types<'tcx>(
363369
insert_ty(res, tcx, arg.clone(), ty_generics);
364370
}
365371
} else {
372+
// This is not a type parameter. So for example if we have `T, U: Option<T>`, and we're
373+
// looking at `Option`, we enter this "else" condition, otherwise if it's `T`, we don't.
374+
//
375+
// So in here, we can add it directly and look for its own type parameters (so for `Option`,
376+
// we will look for them but not for `T`).
366377
let mut ty_generics = Vec::new();
367378
if let Some(arg_generics) = arg.generics() {
368379
for gen in arg_generics.iter() {

0 commit comments

Comments
 (0)