@@ -16,7 +16,6 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
16
16
use rustc_hir as hir;
17
17
use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
18
18
use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
19
- use rustc_hir:: PredicateOrigin ;
20
19
use rustc_infer:: infer:: region_constraints:: { Constraint , RegionConstraintData } ;
21
20
use rustc_middle:: middle:: resolve_lifetime as rl;
22
21
use rustc_middle:: ty:: fold:: TypeFolder ;
@@ -33,7 +32,7 @@ use std::collections::hash_map::Entry;
33
32
use std:: collections:: BTreeMap ;
34
33
use std:: default:: Default ;
35
34
use std:: hash:: Hash ;
36
- use std:: { mem , vec} ;
35
+ use std:: vec;
37
36
38
37
use crate :: core:: { self , DocContext , ImplTraitParam } ;
39
38
use crate :: formats:: item_type:: ItemType ;
@@ -167,10 +166,9 @@ fn clean_poly_trait_ref_with_bindings<'tcx>(
167
166
. collect_referenced_late_bound_regions ( & poly_trait_ref)
168
167
. into_iter ( )
169
168
. filter_map ( |br| match br {
170
- ty:: BrNamed ( _, name) => Some ( GenericParamDef {
171
- name,
172
- kind : GenericParamDefKind :: Lifetime { outlives : vec ! [ ] } ,
173
- } ) ,
169
+ ty:: BrNamed ( _, name) => {
170
+ Some ( GenericParamDef { name, kind : GenericParamDefKind :: Lifetime } )
171
+ }
174
172
_ => None ,
175
173
} )
176
174
. collect ( ) ;
@@ -239,12 +237,9 @@ impl<'tcx> Clean<'tcx, Option<Lifetime>> for ty::Region<'tcx> {
239
237
}
240
238
}
241
239
242
- impl < ' tcx > Clean < ' tcx , Option < WherePredicate > > for hir:: WherePredicate < ' tcx > {
243
- fn clean ( & self , cx : & mut DocContext < ' tcx > ) -> Option < WherePredicate > {
244
- if !self . in_where_clause ( ) {
245
- return None ;
246
- }
247
- Some ( match * self {
240
+ impl < ' tcx > Clean < ' tcx , WherePredicate > for hir:: WherePredicate < ' tcx > {
241
+ fn clean ( & self , cx : & mut DocContext < ' tcx > ) -> WherePredicate {
242
+ match * self {
248
243
hir:: WherePredicate :: BoundPredicate ( ref wbp) => {
249
244
let bound_params = wbp
250
245
. bound_generic_params
@@ -275,7 +270,7 @@ impl<'tcx> Clean<'tcx, Option<WherePredicate>> for hir::WherePredicate<'tcx> {
275
270
lhs : wrp. lhs_ty . clean ( cx) ,
276
271
rhs : wrp. rhs_ty . clean ( cx) . into ( ) ,
277
272
} ,
278
- } )
273
+ }
279
274
}
280
275
}
281
276
@@ -434,9 +429,7 @@ fn projection_to_path_segment<'tcx>(
434
429
impl < ' tcx > Clean < ' tcx , GenericParamDef > for ty:: GenericParamDef {
435
430
fn clean ( & self , cx : & mut DocContext < ' tcx > ) -> GenericParamDef {
436
431
let ( name, kind) = match self . kind {
437
- ty:: GenericParamDefKind :: Lifetime => {
438
- ( self . name , GenericParamDefKind :: Lifetime { outlives : vec ! [ ] } )
439
- }
432
+ ty:: GenericParamDefKind :: Lifetime => ( self . name , GenericParamDefKind :: Lifetime ) ,
440
433
ty:: GenericParamDefKind :: Type { has_default, synthetic, .. } => {
441
434
let default = if has_default {
442
435
Some ( clean_ty ( cx. tcx . type_of ( self . def_id ) , cx, Some ( self . def_id ) ) )
@@ -447,7 +440,6 @@ impl<'tcx> Clean<'tcx, GenericParamDef> for ty::GenericParamDef {
447
440
self . name ,
448
441
GenericParamDefKind :: Type {
449
442
did : self . def_id ,
450
- bounds : vec ! [ ] , // These are filled in from the where-clauses.
451
443
default : default. map ( Box :: new) ,
452
444
synthetic,
453
445
} ,
@@ -472,143 +464,61 @@ impl<'tcx> Clean<'tcx, GenericParamDef> for ty::GenericParamDef {
472
464
473
465
fn clean_generic_param < ' tcx > (
474
466
cx : & mut DocContext < ' tcx > ,
475
- generics : Option < & hir:: Generics < ' tcx > > ,
476
467
param : & hir:: GenericParam < ' tcx > ,
477
468
) -> GenericParamDef {
478
469
let did = cx. tcx . hir ( ) . local_def_id ( param. hir_id ) ;
479
- let ( name, kind) = match param. kind {
480
- hir:: GenericParamKind :: Lifetime { .. } => {
481
- let outlives = if let Some ( generics) = generics {
482
- generics
483
- . outlives_for_param ( did)
484
- . filter ( |bp| !bp. in_where_clause )
485
- . flat_map ( |bp| bp. bounds )
486
- . map ( |bound| match bound {
487
- hir:: GenericBound :: Outlives ( lt) => lt. clean ( cx) ,
488
- _ => panic ! ( ) ,
489
- } )
490
- . collect ( )
491
- } else {
492
- Vec :: new ( )
493
- } ;
494
- ( param. name . ident ( ) . name , GenericParamDefKind :: Lifetime { outlives } )
495
- }
496
- hir:: GenericParamKind :: Type { ref default, synthetic } => {
497
- let bounds = if let Some ( generics) = generics {
498
- generics
499
- . bounds_for_param ( did)
500
- . filter ( |bp| bp. origin != PredicateOrigin :: WhereClause )
501
- . flat_map ( |bp| bp. bounds )
502
- . filter_map ( |x| x. clean ( cx) )
503
- . collect ( )
504
- } else {
505
- Vec :: new ( )
506
- } ;
507
- (
508
- param. name . ident ( ) . name ,
509
- GenericParamDefKind :: Type {
510
- did : did. to_def_id ( ) ,
511
- bounds,
512
- default : default. map ( |t| t. clean ( cx) ) . map ( Box :: new) ,
513
- synthetic,
514
- } ,
515
- )
516
- }
517
- hir:: GenericParamKind :: Const { ty, default } => (
518
- param. name . ident ( ) . name ,
519
- GenericParamDefKind :: Const {
520
- did : did. to_def_id ( ) ,
521
- ty : Box :: new ( ty. clean ( cx) ) ,
522
- default : default. map ( |ct| {
523
- let def_id = cx. tcx . hir ( ) . local_def_id ( ct. hir_id ) ;
524
- Box :: new ( ty:: Const :: from_anon_const ( cx. tcx , def_id) . to_string ( ) )
525
- } ) ,
526
- } ,
527
- ) ,
470
+ let name = param. name . ident ( ) . name ;
471
+ let kind = match param. kind {
472
+ hir:: GenericParamKind :: Lifetime { .. } => GenericParamDefKind :: Lifetime ,
473
+ hir:: GenericParamKind :: Type { ref default, synthetic } => GenericParamDefKind :: Type {
474
+ did : cx. tcx . hir ( ) . local_def_id ( param. hir_id ) . to_def_id ( ) ,
475
+ default : default. map ( |t| t. clean ( cx) ) . map ( Box :: new) ,
476
+ synthetic,
477
+ } ,
478
+ hir:: GenericParamKind :: Const { ty, default } => GenericParamDefKind :: Const {
479
+ did : did. to_def_id ( ) ,
480
+ ty : Box :: new ( ty. clean ( cx) ) ,
481
+ default : default. map ( |ct| {
482
+ let def_id = cx. tcx . hir ( ) . local_def_id ( ct. hir_id ) ;
483
+ Box :: new ( ty:: Const :: from_anon_const ( cx. tcx , def_id) . to_string ( ) )
484
+ } ) ,
485
+ } ,
528
486
} ;
529
487
530
488
GenericParamDef { name, kind }
531
489
}
532
490
533
491
impl < ' tcx > Clean < ' tcx , Generics > for hir:: Generics < ' tcx > {
534
492
fn clean ( & self , cx : & mut DocContext < ' tcx > ) -> Generics {
535
- // Synthetic type-parameters are inserted after normal ones.
536
- // In order for normal parameters to be able to refer to synthetic ones,
537
- // scans them first.
538
- fn is_impl_trait ( param : & hir:: GenericParam < ' _ > ) -> bool {
539
- match param. kind {
540
- hir:: GenericParamKind :: Type { synthetic, .. } => synthetic,
541
- _ => false ,
542
- }
543
- }
544
- /// This can happen for `async fn`, e.g. `async fn f<'_>(&'_ self)`.
545
- ///
546
- /// See [`lifetime_to_generic_param`] in [`rustc_ast_lowering`] for more information.
547
- ///
548
- /// [`lifetime_to_generic_param`]: rustc_ast_lowering::LoweringContext::lifetime_to_generic_param
549
- fn is_elided_lifetime ( param : & hir:: GenericParam < ' _ > ) -> bool {
550
- matches ! (
551
- param. kind,
552
- hir:: GenericParamKind :: Lifetime { kind: hir:: LifetimeParamKind :: Elided }
553
- )
554
- }
555
-
556
- let impl_trait_params = self
557
- . params
558
- . iter ( )
559
- . filter ( |param| is_impl_trait ( param) )
560
- . map ( |param| {
561
- let param = clean_generic_param ( cx, Some ( self ) , param) ;
562
- match param. kind {
563
- GenericParamDefKind :: Lifetime { .. } => unreachable ! ( ) ,
564
- GenericParamDefKind :: Type { did, ref bounds, .. } => {
565
- cx. impl_trait_bounds . insert ( did. into ( ) , bounds. clone ( ) ) ;
493
+ let mut impl_trait = FxHashSet :: default ( ) ;
494
+ for ( i, p) in self . predicates . iter ( ) . enumerate ( ) {
495
+ if let hir:: WherePredicate :: BoundPredicate ( bp) = p {
496
+ if let Some ( ( def_id, ident) ) = bp. bounded_ty . as_generic_param ( ) {
497
+ // Do not include predicates on `impl Trait` desugaring.
498
+ if ident. as_str ( ) . starts_with ( "impl " ) {
499
+ let bounds = bp. bounds . iter ( ) . filter_map ( |b| b. clean ( cx) ) . collect ( ) ;
500
+ cx. impl_trait_bounds . insert ( def_id. into ( ) , bounds) ;
501
+ impl_trait. insert ( i) ;
566
502
}
567
- GenericParamDefKind :: Const { .. } => unreachable ! ( ) ,
568
503
}
569
- param
570
- } )
571
- . collect :: < Vec < _ > > ( ) ;
504
+ }
505
+ }
572
506
573
507
let mut params = Vec :: with_capacity ( self . params . len ( ) ) ;
574
- for p in self . params . iter ( ) . filter ( |p| !is_impl_trait ( p) && !is_elided_lifetime ( p) ) {
575
- let p = clean_generic_param ( cx, Some ( self ) , p) ;
576
- params. push ( p) ;
508
+ for p in self . params {
509
+ if !p. is_elided_lifetime ( ) {
510
+ params. push ( clean_generic_param ( cx, p) ) ;
511
+ }
577
512
}
578
- params. extend ( impl_trait_params) ;
579
-
580
- let mut generics = Generics {
581
- params,
582
- where_predicates : self . predicates . iter ( ) . filter_map ( |x| x. clean ( cx) ) . collect ( ) ,
583
- } ;
584
513
585
- // Some duplicates are generated for ?Sized bounds between type params and where
586
- // predicates. The point in here is to move the bounds definitions from type params
587
- // to where predicates when such cases occur.
588
- for where_pred in & mut generics. where_predicates {
589
- match * where_pred {
590
- WherePredicate :: BoundPredicate {
591
- ty : Generic ( ref name) , ref mut bounds, ..
592
- } => {
593
- if bounds. is_empty ( ) {
594
- for param in & mut generics. params {
595
- match param. kind {
596
- GenericParamDefKind :: Lifetime { .. } => { }
597
- GenericParamDefKind :: Type { bounds : ref mut ty_bounds, .. } => {
598
- if & param. name == name {
599
- mem:: swap ( bounds, ty_bounds) ;
600
- break ;
601
- }
602
- }
603
- GenericParamDefKind :: Const { .. } => { }
604
- }
605
- }
606
- }
607
- }
608
- _ => continue ,
514
+ let mut where_predicates = Vec :: with_capacity ( self . predicates . len ( ) ) ;
515
+ for ( i, p) in self . predicates . iter ( ) . enumerate ( ) {
516
+ if impl_trait. contains ( & i) {
517
+ continue ;
609
518
}
519
+ where_predicates. push ( p. clean ( cx) ) ;
610
520
}
611
- generics
521
+ Generics { params , where_predicates }
612
522
}
613
523
}
614
524
@@ -991,7 +901,7 @@ impl<'tcx> Clean<'tcx, PolyTrait> for hir::PolyTraitRef<'tcx> {
991
901
generic_params : self
992
902
. bound_generic_params
993
903
. iter ( )
994
- . map ( |x| clean_generic_param ( cx, None , x) )
904
+ . map ( |x| clean_generic_param ( cx, x) )
995
905
. collect ( ) ,
996
906
}
997
907
}
@@ -1866,7 +1776,7 @@ impl<'tcx> Clean<'tcx, BareFunctionDecl> for hir::BareFnTy<'tcx> {
1866
1776
let ( generic_params, decl) = enter_impl_trait ( cx, |cx| {
1867
1777
// NOTE: generics must be cleaned before args
1868
1778
let generic_params =
1869
- self . generic_params . iter ( ) . map ( |x| clean_generic_param ( cx, None , x) ) . collect ( ) ;
1779
+ self . generic_params . iter ( ) . map ( |x| clean_generic_param ( cx, x) ) . collect ( ) ;
1870
1780
let args = clean_args_from_types_and_names ( cx, self . decl . inputs , self . param_names ) ;
1871
1781
let decl = clean_fn_decl_with_args ( cx, self . decl , args) ;
1872
1782
( generic_params, decl)
0 commit comments