@@ -47,7 +47,7 @@ use std::u32;
4747
4848use parking_lot:: ReentrantMutex ;
4949
50- use crate :: core:: { self , DocContext } ;
50+ use crate :: core:: { self , DocContext , ImplTraitParam } ;
5151use crate :: doctree;
5252use crate :: visit_ast;
5353use crate :: html:: render:: { cache, ExternalLocation } ;
@@ -1536,7 +1536,7 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
15361536 ty:: GenericParamDefKind :: Lifetime => {
15371537 ( self . name . to_string ( ) , GenericParamDefKind :: Lifetime )
15381538 }
1539- ty:: GenericParamDefKind :: Type { has_default, .. } => {
1539+ ty:: GenericParamDefKind :: Type { has_default, synthetic , .. } => {
15401540 cx. renderinfo . borrow_mut ( ) . external_param_names
15411541 . insert ( self . def_id , self . name . clean ( cx) ) ;
15421542 let default = if has_default {
@@ -1548,7 +1548,7 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
15481548 did : self . def_id ,
15491549 bounds : vec ! [ ] , // These are filled in from the where-clauses.
15501550 default,
1551- synthetic : None ,
1551+ synthetic,
15521552 } )
15531553 }
15541554 ty:: GenericParamDefKind :: Const { .. } => {
@@ -1637,7 +1637,7 @@ impl Clean<Generics> for hir::Generics {
16371637 match param. kind {
16381638 GenericParamDefKind :: Lifetime => unreachable ! ( ) ,
16391639 GenericParamDefKind :: Type { did, ref bounds, .. } => {
1640- cx. impl_trait_bounds . borrow_mut ( ) . insert ( did, bounds. clone ( ) ) ;
1640+ cx. impl_trait_bounds . borrow_mut ( ) . insert ( did. into ( ) , bounds. clone ( ) ) ;
16411641 }
16421642 GenericParamDefKind :: Const { .. } => unreachable ! ( ) ,
16431643 }
@@ -1692,25 +1692,76 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
16921692
16931693 let ( gens, preds) = * self ;
16941694
1695+ // Don't populate `cx.impl_trait_bounds` before `clean`ning `where` clauses,
1696+ // since `Clean for ty::Predicate` would consume them.
1697+ let mut impl_trait = FxHashMap :: < ImplTraitParam , Vec < _ > > :: default ( ) ;
1698+
16951699 // Bounds in the type_params and lifetimes fields are repeated in the
16961700 // predicates field (see rustc_typeck::collect::ty_generics), so remove
16971701 // them.
1698- let stripped_typarams = gens. params . iter ( ) . filter_map ( |param| match param. kind {
1699- ty:: GenericParamDefKind :: Lifetime => None ,
1700- ty:: GenericParamDefKind :: Type { .. } => {
1701- if param. name . as_symbol ( ) == kw:: SelfUpper {
1702- assert_eq ! ( param. index, 0 ) ;
1703- return None ;
1702+ let stripped_typarams = gens. params . iter ( ) . enumerate ( )
1703+ . filter_map ( |( i, param) | match param. kind {
1704+ ty:: GenericParamDefKind :: Lifetime => None ,
1705+ ty:: GenericParamDefKind :: Type { synthetic, .. } => {
1706+ if param. name . as_symbol ( ) == kw:: SelfUpper {
1707+ assert_eq ! ( param. index, 0 ) ;
1708+ return None ;
1709+ }
1710+ if synthetic == Some ( hir:: SyntheticTyParamKind :: ImplTrait ) {
1711+ impl_trait. insert ( ( i as u32 ) . into ( ) , vec ! [ ] ) ;
1712+ return None ;
1713+ }
1714+ Some ( param. clean ( cx) )
17041715 }
1705- Some ( param. clean ( cx) )
1706- }
1707- ty:: GenericParamDefKind :: Const { .. } => None ,
1708- } ) . collect :: < Vec < GenericParamDef > > ( ) ;
1716+ ty:: GenericParamDefKind :: Const { .. } => None ,
1717+ } ) . collect :: < Vec < GenericParamDef > > ( ) ;
17091718
17101719 let mut where_predicates = preds. predicates . iter ( )
1711- . flat_map ( |( p, _) | p. clean ( cx) )
1720+ . flat_map ( |( p, _) | {
1721+ let param_idx = if let Some ( trait_ref) = p. to_opt_poly_trait_ref ( ) {
1722+ if let ty:: Param ( param) = trait_ref. self_ty ( ) . sty {
1723+ Some ( param. index )
1724+ } else {
1725+ None
1726+ }
1727+ } else if let Some ( outlives) = p. to_opt_type_outlives ( ) {
1728+ if let ty:: Param ( param) = outlives. skip_binder ( ) . 0 . sty {
1729+ Some ( param. index )
1730+ } else {
1731+ None
1732+ }
1733+ } else {
1734+ None
1735+ } ;
1736+
1737+ let p = p. clean ( cx) ?;
1738+
1739+ if let Some ( b) = param_idx. and_then ( |i| impl_trait. get_mut ( & i. into ( ) ) ) {
1740+ b. extend (
1741+ p. get_bounds ( )
1742+ . into_iter ( )
1743+ . flatten ( )
1744+ . cloned ( )
1745+ . filter ( |b| !b. is_sized_bound ( cx) )
1746+ ) ;
1747+ return None ;
1748+ }
1749+
1750+ Some ( p)
1751+ } )
17121752 . collect :: < Vec < _ > > ( ) ;
17131753
1754+ // Move `TraitPredicate`s to the front.
1755+ for ( _, bounds) in impl_trait. iter_mut ( ) {
1756+ bounds. sort_by_key ( |b| if let GenericBound :: TraitBound ( ..) = b {
1757+ false
1758+ } else {
1759+ true
1760+ } ) ;
1761+ }
1762+
1763+ cx. impl_trait_bounds . borrow_mut ( ) . extend ( impl_trait) ;
1764+
17141765 // Type parameters and have a Sized bound by default unless removed with
17151766 // ?Sized. Scan through the predicates and mark any type parameter with
17161767 // a Sized bound, removing the bounds as we find them.
@@ -2789,7 +2840,7 @@ impl Clean<Type> for hir::Ty {
27892840 if let Some ( new_ty) = cx. ty_substs . borrow ( ) . get ( & did) . cloned ( ) {
27902841 return new_ty;
27912842 }
2792- if let Some ( bounds) = cx. impl_trait_bounds . borrow_mut ( ) . remove ( & did) {
2843+ if let Some ( bounds) = cx. impl_trait_bounds . borrow_mut ( ) . remove ( & did. into ( ) ) {
27932844 return ImplTrait ( bounds) ;
27942845 }
27952846 }
@@ -3079,7 +3130,13 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
30793130
30803131 ty:: Projection ( ref data) => data. clean ( cx) ,
30813132
3082- ty:: Param ( ref p) => Generic ( p. name . to_string ( ) ) ,
3133+ ty:: Param ( ref p) => {
3134+ if let Some ( bounds) = cx. impl_trait_bounds . borrow_mut ( ) . remove ( & p. index . into ( ) ) {
3135+ ImplTrait ( bounds)
3136+ } else {
3137+ Generic ( p. name . to_string ( ) )
3138+ }
3139+ }
30833140
30843141 ty:: Opaque ( def_id, substs) => {
30853142 // Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,
0 commit comments