@@ -3,7 +3,7 @@ use std::collections::BTreeMap;
33
44use rustc_data_structures:: fx:: FxHashMap ;
55use rustc_middle:: ty:: TyCtxt ;
6- use rustc_span:: symbol:: Symbol ;
6+ use rustc_span:: symbol:: { kw , Symbol } ;
77use serde:: ser:: { Serialize , SerializeStruct , Serializer } ;
88
99use crate :: clean;
@@ -33,7 +33,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
3333 desc,
3434 parent : Some ( did) ,
3535 parent_idx : None ,
36- search_type : get_function_type_for_search ( item, tcx) ,
36+ search_type : get_function_type_for_search ( item, tcx, & cache ) ,
3737 aliases : item. attrs . get_doc_aliases ( ) ,
3838 } ) ;
3939 }
@@ -188,11 +188,12 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
188188crate fn get_function_type_for_search < ' tcx > (
189189 item : & clean:: Item ,
190190 tcx : TyCtxt < ' tcx > ,
191+ cache : & Cache ,
191192) -> Option < IndexItemFunctionType > {
192193 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 ) ,
196197 _ => return None ,
197198 } ;
198199
@@ -219,7 +220,8 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
219220 let path = & bounds[ 0 ] . trait_ ;
220221 Some ( path. segments . last ( ) . unwrap ( ) . name )
221222 }
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 ) ,
223225 clean:: Primitive ( ref p) => Some ( p. as_sym ( ) ) ,
224226 clean:: BorrowedRef { ref type_, .. } => get_index_type_name ( type_) ,
225227 clean:: BareFunction ( _)
@@ -240,24 +242,27 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
240242///
241243/// Important note: It goes through generics recursively. So if you have
242244/// `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 ) ) ]
244246fn add_generics_and_bounds_as_types < ' tcx > (
245247 generics : & Generics ,
246248 arg : & Type ,
247249 tcx : TyCtxt < ' tcx > ,
248250 recurse : usize ,
249251 res : & mut Vec < TypeWithKind > ,
252+ cache : & Cache ,
250253) {
251254 fn insert_ty (
252255 res : & mut Vec < TypeWithKind > ,
253256 tcx : TyCtxt < ' _ > ,
254257 ty : Type ,
255258 mut generics : Vec < TypeWithKind > ,
259+ cache : & Cache ,
256260 ) {
257261 let is_full_generic = ty. is_full_generic ( ) ;
262+ let generics_empty = generics. is_empty ( ) ;
258263
259264 if is_full_generic {
260- if generics . is_empty ( ) {
265+ if generics_empty {
261266 // This is a type parameter with no trait bounds (for example: `T` in
262267 // `fn f<T>(p: T)`, so not useful for the rustdoc search because we would end up
263268 // 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>(
304309 }
305310 }
306311 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 ) {
308313 return ;
309314 }
310315 if is_full_generic {
311316 // We remove the name of the full generic because we have no use for it.
312317 index_ty. name = Some ( String :: new ( ) ) ;
313318 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 ( ) ) {
315320 res. push ( TypeWithKind :: from ( ( index_ty, kind) ) ) ;
316321 } else if ty. is_primitive ( ) {
317322 // This is a primitive, let's store it as such.
@@ -330,9 +335,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
330335 if let Type :: Generic ( arg_s) = * arg {
331336 // First we check if the bounds are in a `where` predicate...
332337 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) ,
336339 _ => false ,
337340 } ) {
338341 let mut ty_generics = Vec :: new ( ) ;
@@ -348,14 +351,15 @@ fn add_generics_and_bounds_as_types<'tcx>(
348351 tcx,
349352 recurse + 1 ,
350353 & mut ty_generics,
354+ cache,
351355 )
352356 }
353357 _ => { }
354358 }
355359 }
356360 }
357361 }
358- insert_ty ( res, tcx, arg. clone ( ) , ty_generics) ;
362+ insert_ty ( res, tcx, arg. clone ( ) , ty_generics, cache ) ;
359363 }
360364 // Otherwise we check if the trait bounds are "inlined" like `T: Option<u32>`...
361365 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>(
369373 tcx,
370374 recurse + 1 ,
371375 & mut ty_generics,
376+ cache,
372377 ) ;
373378 }
374379 }
375- insert_ty ( res, tcx, arg. clone ( ) , ty_generics) ;
380+ insert_ty ( res, tcx, arg. clone ( ) , ty_generics, cache ) ;
376381 }
377382 } else {
378383 // 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>(
383388 let mut ty_generics = Vec :: new ( ) ;
384389 if let Some ( arg_generics) = arg. generics ( ) {
385390 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+ ) ;
387399 }
388400 }
389- insert_ty ( res, tcx, arg. clone ( ) , ty_generics) ;
401+ insert_ty ( res, tcx, arg. clone ( ) , ty_generics, cache ) ;
390402 }
391403}
392404
@@ -397,6 +409,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
397409fn get_fn_inputs_and_outputs < ' tcx > (
398410 func : & Function ,
399411 tcx : TyCtxt < ' tcx > ,
412+ cache : & Cache ,
400413) -> ( Vec < TypeWithKind > , Vec < TypeWithKind > ) {
401414 let decl = & func. decl ;
402415 let generics = & func. generics ;
@@ -407,12 +420,11 @@ fn get_fn_inputs_and_outputs<'tcx>(
407420 continue ;
408421 }
409422 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 ) ;
411424 if !args. is_empty ( ) {
412425 all_types. extend ( args) ;
413426 } 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 ( ) ) {
416428 all_types. push ( TypeWithKind :: from ( ( get_index_type ( & arg. type_ , vec ! [ ] ) , kind) ) ) ;
417429 }
418430 }
@@ -421,11 +433,9 @@ fn get_fn_inputs_and_outputs<'tcx>(
421433 let mut ret_types = Vec :: new ( ) ;
422434 match decl. output {
423435 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 ) ;
425437 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 ( ) ) {
429439 ret_types. push ( TypeWithKind :: from ( ( get_index_type ( return_type, vec ! [ ] ) , kind) ) ) ;
430440 }
431441 }
0 commit comments