@@ -3,7 +3,7 @@ use std::collections::BTreeMap;
3
3
4
4
use rustc_data_structures:: fx:: FxHashMap ;
5
5
use rustc_middle:: ty:: TyCtxt ;
6
- use rustc_span:: symbol:: Symbol ;
6
+ use rustc_span:: symbol:: { kw , Symbol } ;
7
7
use serde:: ser:: { Serialize , SerializeStruct , Serializer } ;
8
8
9
9
use crate :: clean;
@@ -33,7 +33,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
33
33
desc,
34
34
parent : Some ( did) ,
35
35
parent_idx : None ,
36
- search_type : get_function_type_for_search ( item, tcx) ,
36
+ search_type : get_function_type_for_search ( item, tcx, & cache ) ,
37
37
aliases : item. attrs . get_doc_aliases ( ) ,
38
38
} ) ;
39
39
}
@@ -188,11 +188,12 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
188
188
crate fn get_function_type_for_search < ' tcx > (
189
189
item : & clean:: Item ,
190
190
tcx : TyCtxt < ' tcx > ,
191
+ cache : & Cache ,
191
192
) -> Option < IndexItemFunctionType > {
192
193
let ( mut inputs, mut output) = match * item. kind {
193
- clean:: FunctionItem ( ref f) => get_fn_inputs_and_outputs ( f, tcx) ,
194
- clean:: MethodItem ( ref m, _) => get_fn_inputs_and_outputs ( m, tcx) ,
195
- clean:: TyMethodItem ( ref m) => get_fn_inputs_and_outputs ( m, tcx) ,
194
+ clean:: FunctionItem ( ref f) => get_fn_inputs_and_outputs ( f, tcx, cache ) ,
195
+ clean:: MethodItem ( ref m, _) => get_fn_inputs_and_outputs ( m, tcx, cache ) ,
196
+ clean:: TyMethodItem ( ref m) => get_fn_inputs_and_outputs ( m, tcx, cache ) ,
196
197
_ => return None ,
197
198
} ;
198
199
@@ -219,7 +220,8 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
219
220
let path = & bounds[ 0 ] . trait_ ;
220
221
Some ( path. segments . last ( ) . unwrap ( ) . name )
221
222
}
222
- clean:: Generic ( s) => Some ( s) ,
223
+ // We return an empty name because we don't care about the generic name itself.
224
+ clean:: Generic ( _) => Some ( kw:: Empty ) ,
223
225
clean:: Primitive ( ref p) => Some ( p. as_sym ( ) ) ,
224
226
clean:: BorrowedRef { ref type_, .. } => get_index_type_name ( type_) ,
225
227
clean:: BareFunction ( _)
@@ -240,24 +242,27 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
240
242
///
241
243
/// Important note: It goes through generics recursively. So if you have
242
244
/// `T: Option<Result<(), ()>>`, it'll go into `Option` and then into `Result`.
243
- #[ instrument( level = "trace" , skip( tcx, res) ) ]
245
+ #[ instrument( level = "trace" , skip( tcx, res, cache ) ) ]
244
246
fn add_generics_and_bounds_as_types < ' tcx > (
245
247
generics : & Generics ,
246
248
arg : & Type ,
247
249
tcx : TyCtxt < ' tcx > ,
248
250
recurse : usize ,
249
251
res : & mut Vec < TypeWithKind > ,
252
+ cache : & Cache ,
250
253
) {
251
254
fn insert_ty (
252
255
res : & mut Vec < TypeWithKind > ,
253
256
tcx : TyCtxt < ' _ > ,
254
257
ty : Type ,
255
258
mut generics : Vec < TypeWithKind > ,
259
+ cache : & Cache ,
256
260
) {
257
261
let is_full_generic = ty. is_full_generic ( ) ;
262
+ let generics_empty = generics. is_empty ( ) ;
258
263
259
264
if is_full_generic {
260
- if generics . is_empty ( ) {
265
+ if generics_empty {
261
266
// This is a type parameter with no trait bounds (for example: `T` in
262
267
// `fn f<T>(p: T)`, so not useful for the rustdoc search because we would end up
263
268
// with an empty type with an empty name. Let's just discard it.
@@ -304,14 +309,14 @@ fn add_generics_and_bounds_as_types<'tcx>(
304
309
}
305
310
}
306
311
let mut index_ty = get_index_type ( & ty, generics) ;
307
- if index_ty. name . as_ref ( ) . map ( |s| s. is_empty ( ) ) . unwrap_or ( true ) {
312
+ if index_ty. name . as_ref ( ) . map ( |s| s. is_empty ( ) && generics_empty ) . unwrap_or ( true ) {
308
313
return ;
309
314
}
310
315
if is_full_generic {
311
316
// We remove the name of the full generic because we have no use for it.
312
317
index_ty. name = Some ( String :: new ( ) ) ;
313
318
res. push ( TypeWithKind :: from ( ( index_ty, ItemType :: Generic ) ) ) ;
314
- } else if let Some ( kind) = ty. def_id_no_primitives ( ) . map ( |did| tcx. def_kind ( did) . into ( ) ) {
319
+ } else if let Some ( kind) = ty. def_id ( cache ) . map ( |did| tcx. def_kind ( did) . into ( ) ) {
315
320
res. push ( TypeWithKind :: from ( ( index_ty, kind) ) ) ;
316
321
} else if ty. is_primitive ( ) {
317
322
// This is a primitive, let's store it as such.
@@ -330,9 +335,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
330
335
if let Type :: Generic ( arg_s) = * arg {
331
336
// First we check if the bounds are in a `where` predicate...
332
337
if let Some ( where_pred) = generics. where_predicates . iter ( ) . find ( |g| match g {
333
- WherePredicate :: BoundPredicate { ty, .. } => {
334
- ty. def_id_no_primitives ( ) == arg. def_id_no_primitives ( )
335
- }
338
+ WherePredicate :: BoundPredicate { ty, .. } => ty. def_id ( cache) == arg. def_id ( cache) ,
336
339
_ => false ,
337
340
} ) {
338
341
let mut ty_generics = Vec :: new ( ) ;
@@ -348,14 +351,15 @@ fn add_generics_and_bounds_as_types<'tcx>(
348
351
tcx,
349
352
recurse + 1 ,
350
353
& mut ty_generics,
354
+ cache,
351
355
)
352
356
}
353
357
_ => { }
354
358
}
355
359
}
356
360
}
357
361
}
358
- insert_ty ( res, tcx, arg. clone ( ) , ty_generics) ;
362
+ insert_ty ( res, tcx, arg. clone ( ) , ty_generics, cache ) ;
359
363
}
360
364
// Otherwise we check if the trait bounds are "inlined" like `T: Option<u32>`...
361
365
if let Some ( bound) = generics. params . iter ( ) . find ( |g| g. is_type ( ) && g. name == arg_s) {
@@ -369,10 +373,11 @@ fn add_generics_and_bounds_as_types<'tcx>(
369
373
tcx,
370
374
recurse + 1 ,
371
375
& mut ty_generics,
376
+ cache,
372
377
) ;
373
378
}
374
379
}
375
- insert_ty ( res, tcx, arg. clone ( ) , ty_generics) ;
380
+ insert_ty ( res, tcx, arg. clone ( ) , ty_generics, cache ) ;
376
381
}
377
382
} else {
378
383
// This is not a type parameter. So for example if we have `T, U: Option<T>`, and we're
@@ -383,10 +388,17 @@ fn add_generics_and_bounds_as_types<'tcx>(
383
388
let mut ty_generics = Vec :: new ( ) ;
384
389
if let Some ( arg_generics) = arg. generics ( ) {
385
390
for gen in arg_generics. iter ( ) {
386
- add_generics_and_bounds_as_types ( generics, gen, tcx, recurse + 1 , & mut ty_generics) ;
391
+ add_generics_and_bounds_as_types (
392
+ generics,
393
+ gen,
394
+ tcx,
395
+ recurse + 1 ,
396
+ & mut ty_generics,
397
+ cache,
398
+ ) ;
387
399
}
388
400
}
389
- insert_ty ( res, tcx, arg. clone ( ) , ty_generics) ;
401
+ insert_ty ( res, tcx, arg. clone ( ) , ty_generics, cache ) ;
390
402
}
391
403
}
392
404
@@ -397,6 +409,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
397
409
fn get_fn_inputs_and_outputs < ' tcx > (
398
410
func : & Function ,
399
411
tcx : TyCtxt < ' tcx > ,
412
+ cache : & Cache ,
400
413
) -> ( Vec < TypeWithKind > , Vec < TypeWithKind > ) {
401
414
let decl = & func. decl ;
402
415
let generics = & func. generics ;
@@ -407,12 +420,11 @@ fn get_fn_inputs_and_outputs<'tcx>(
407
420
continue ;
408
421
}
409
422
let mut args = Vec :: new ( ) ;
410
- add_generics_and_bounds_as_types ( generics, & arg. type_ , tcx, 0 , & mut args) ;
423
+ add_generics_and_bounds_as_types ( generics, & arg. type_ , tcx, 0 , & mut args, cache ) ;
411
424
if !args. is_empty ( ) {
412
425
all_types. extend ( args) ;
413
426
} else {
414
- if let Some ( kind) = arg. type_ . def_id_no_primitives ( ) . map ( |did| tcx. def_kind ( did) . into ( ) )
415
- {
427
+ if let Some ( kind) = arg. type_ . def_id ( cache) . map ( |did| tcx. def_kind ( did) . into ( ) ) {
416
428
all_types. push ( TypeWithKind :: from ( ( get_index_type ( & arg. type_ , vec ! [ ] ) , kind) ) ) ;
417
429
}
418
430
}
@@ -421,11 +433,9 @@ fn get_fn_inputs_and_outputs<'tcx>(
421
433
let mut ret_types = Vec :: new ( ) ;
422
434
match decl. output {
423
435
FnRetTy :: Return ( ref return_type) => {
424
- add_generics_and_bounds_as_types ( generics, return_type, tcx, 0 , & mut ret_types) ;
436
+ add_generics_and_bounds_as_types ( generics, return_type, tcx, 0 , & mut ret_types, cache ) ;
425
437
if ret_types. is_empty ( ) {
426
- if let Some ( kind) =
427
- return_type. def_id_no_primitives ( ) . map ( |did| tcx. def_kind ( did) . into ( ) )
428
- {
438
+ if let Some ( kind) = return_type. def_id ( cache) . map ( |did| tcx. def_kind ( did) . into ( ) ) {
429
439
ret_types. push ( TypeWithKind :: from ( ( get_index_type ( return_type, vec ! [ ] ) , kind) ) ) ;
430
440
}
431
441
}
0 commit comments