Skip to content

Commit 9c05335

Browse files
Add more comments to explain the code to generate the search index
1 parent 1d34cb4 commit 9c05335

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
@@ -241,8 +241,10 @@ fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option
241241
/// The point of this function is to replace bounds with types.
242242
///
243243
/// i.e. `[T, U]` when you have the following bounds: `T: Display, U: Option<T>` will return
244-
/// `[Display, Option]` (we just returns the list of the types, we don't care about the
245-
/// wrapped types in here).
244+
/// `[Display, Option]`. If a type parameter has no trait bound, it is discarded.
245+
///
246+
/// Important note: It goes through generics recursively. So if you have
247+
/// `T: Option<Result<(), ()>>`, it'll go into `Option` and then into `Result`.
246248
crate fn get_real_types<'tcx>(
247249
generics: &Generics,
248250
arg: &Type,
@@ -326,7 +328,10 @@ crate fn get_real_types<'tcx>(
326328
return;
327329
}
328330

331+
// If this argument is a type parameter and not a trait bound or a type, we need to look
332+
// for its bounds.
329333
if let Type::Generic(arg_s) = *arg {
334+
// First we check if the bounds are in a `where` predicate...
330335
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
331336
WherePredicate::BoundPredicate { ty, .. } => {
332337
ty.def_id_no_primitives() == arg.def_id_no_primitives()
@@ -349,6 +354,7 @@ crate fn get_real_types<'tcx>(
349354
}
350355
insert_ty(res, tcx, arg.clone(), ty_generics);
351356
}
357+
// Otherwise we check if the trait bounds are "inlined" like `T: Option<u32>`...
352358
if let Some(bound) = generics.params.iter().find(|g| g.is_type() && g.name == arg_s) {
353359
let mut ty_generics = Vec::new();
354360
for bound in bound.get_bounds().unwrap_or(&[]) {
@@ -360,6 +366,11 @@ crate fn get_real_types<'tcx>(
360366
insert_ty(res, tcx, arg.clone(), ty_generics);
361367
}
362368
} else {
369+
// This is not a type parameter. So for example if we have `T, U: Option<T>`, and we're
370+
// looking at `Option`, we enter this "else" condition, otherwise if it's `T`, we don't.
371+
//
372+
// So in here, we can add it directly and look for its own type parameters (so for `Option`,
373+
// we will look for them but not for `T`).
363374
let mut ty_generics = Vec::new();
364375
if let Some(arg_generics) = arg.generics() {
365376
for gen in arg_generics.iter() {

0 commit comments

Comments
 (0)