@@ -11,6 +11,7 @@ use rustc_hir::{
1111} ;
1212use rustc_index:: { IndexSlice , IndexVec } ;
1313use rustc_middle:: span_bug;
14+ use rustc_middle:: ty:: data_structures:: IndexMap ;
1415use rustc_middle:: ty:: { ResolverAstLowering , TyCtxt } ;
1516use rustc_span:: def_id:: DefId ;
1617use rustc_span:: edit_distance:: find_best_match_for_name;
@@ -326,7 +327,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
326327 let ident = self . lower_ident ( * ident) ;
327328 let ( generics, ( ty, rhs) ) = self . lower_generics (
328329 generics,
329- id,
330330 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
331331 |this| {
332332 let ty = this. lower_ty_alloc (
@@ -380,7 +380,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
380380 ) ;
381381
382382 let itctx = ImplTraitContext :: Universal ;
383- let ( generics, decl) = this. lower_generics ( generics, id , itctx, |this| {
383+ let ( generics, decl) = this. lower_generics ( generics, itctx, |this| {
384384 this. lower_fn_decl ( decl, id, * fn_sig_span, FnDeclKind :: Fn , coroutine_kind)
385385 } ) ;
386386 let sig = hir:: FnSig {
@@ -434,7 +434,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
434434 add_ty_alias_where_clause ( & mut generics, after_where_clause, true ) ;
435435 let ( generics, ty) = self . lower_generics (
436436 & generics,
437- id,
438437 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
439438 |this| match ty {
440439 None => {
@@ -461,7 +460,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
461460 let ident = self . lower_ident ( * ident) ;
462461 let ( generics, variants) = self . lower_generics (
463462 generics,
464- id,
465463 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
466464 |this| {
467465 this. arena . alloc_from_iter (
@@ -475,7 +473,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
475473 let ident = self . lower_ident ( * ident) ;
476474 let ( generics, struct_def) = self . lower_generics (
477475 generics,
478- id,
479476 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
480477 |this| this. lower_variant_data ( hir_id, i, struct_def) ,
481478 ) ;
@@ -485,7 +482,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
485482 let ident = self . lower_ident ( * ident) ;
486483 let ( generics, vdata) = self . lower_generics (
487484 generics,
488- id,
489485 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
490486 |this| this. lower_variant_data ( hir_id, i, vdata) ,
491487 ) ;
@@ -513,7 +509,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
513509 // parent lifetime.
514510 let itctx = ImplTraitContext :: Universal ;
515511 let ( generics, ( of_trait, lowered_ty) ) =
516- self . lower_generics ( ast_generics, id , itctx, |this| {
512+ self . lower_generics ( ast_generics, itctx, |this| {
517513 let of_trait = of_trait
518514 . as_deref ( )
519515 . map ( |of_trait| this. lower_trait_impl_header ( of_trait) ) ;
@@ -555,7 +551,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
555551 let ident = self . lower_ident ( * ident) ;
556552 let ( generics, ( safety, items, bounds) ) = self . lower_generics (
557553 generics,
558- id,
559554 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
560555 |this| {
561556 let bounds = this. lower_param_bounds (
@@ -586,7 +581,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
586581 let ident = self . lower_ident ( * ident) ;
587582 let ( generics, bounds) = self . lower_generics (
588583 generics,
589- id,
590584 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
591585 |this| {
592586 this. lower_param_bounds (
@@ -798,14 +792,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
798792 ForeignItemKind :: Fn ( Fn { sig, ident, generics, define_opaque, .. } ) => {
799793 let fdec = & sig. decl ;
800794 let itctx = ImplTraitContext :: Universal ;
801- let ( generics, ( decl, fn_args) ) =
802- self . lower_generics ( generics, i. id , itctx, |this| {
803- (
804- // Disallow `impl Trait` in foreign items.
805- this. lower_fn_decl ( fdec, i. id , sig. span , FnDeclKind :: ExternFn , None ) ,
806- this. lower_fn_params_to_idents ( fdec) ,
807- )
808- } ) ;
795+ let ( generics, ( decl, fn_args) ) = self . lower_generics ( generics, itctx, |this| {
796+ (
797+ // Disallow `impl Trait` in foreign items.
798+ this. lower_fn_decl ( fdec, i. id , sig. span , FnDeclKind :: ExternFn , None ) ,
799+ this. lower_fn_params_to_idents ( fdec) ,
800+ )
801+ } ) ;
809802
810803 // Unmarked safety in unsafe block defaults to unsafe.
811804 let header = self . lower_fn_header ( sig. header , hir:: Safety :: Unsafe , attrs) ;
@@ -995,7 +988,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
995988 } ) => {
996989 let ( generics, kind) = self . lower_generics (
997990 generics,
998- i. id ,
999991 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
1000992 |this| {
1001993 let ty = this. lower_ty_alloc (
@@ -1097,7 +1089,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
10971089 add_ty_alias_where_clause ( & mut generics, after_where_clause, false ) ;
10981090 let ( generics, kind) = self . lower_generics (
10991091 & generics,
1100- i. id ,
11011092 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
11021093 |this| {
11031094 let ty = ty. as_ref ( ) . map ( |x| {
@@ -1109,7 +1100,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11091100 hir:: TraitItemKind :: Type (
11101101 this. lower_param_bounds (
11111102 bounds,
1112- RelaxedBoundPolicy :: Allowed ,
1103+ RelaxedBoundPolicy :: Allowed ( & mut Default :: default ( ) ) ,
11131104 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
11141105 ) ,
11151106 ty,
@@ -1260,7 +1251,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
12601251 * ident,
12611252 self . lower_generics (
12621253 generics,
1263- i. id ,
12641254 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
12651255 |this| {
12661256 let ty = this. lower_ty_alloc (
@@ -1305,7 +1295,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
13051295 * ident,
13061296 self . lower_generics (
13071297 & generics,
1308- i. id ,
13091298 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
13101299 |this| match ty {
13111300 None => {
@@ -1744,7 +1733,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17441733 ) -> ( & ' hir hir:: Generics < ' hir > , hir:: FnSig < ' hir > ) {
17451734 let header = self . lower_fn_header ( sig. header , hir:: Safety :: Safe , attrs) ;
17461735 let itctx = ImplTraitContext :: Universal ;
1747- let ( generics, decl) = self . lower_generics ( generics, id , itctx, |this| {
1736+ let ( generics, decl) = self . lower_generics ( generics, itctx, |this| {
17481737 this. lower_fn_decl ( & sig. decl , id, sig. span , kind, coroutine_kind)
17491738 } ) ;
17501739 ( generics, hir:: FnSig { header, decl, span : self . lower_span ( sig. span ) } )
@@ -1903,14 +1892,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
19031892 fn lower_generics < T > (
19041893 & mut self ,
19051894 generics : & Generics ,
1906- parent_node_id : NodeId ,
19071895 itctx : ImplTraitContext ,
19081896 f : impl FnOnce ( & mut Self ) -> T ,
19091897 ) -> ( & ' hir hir:: Generics < ' hir > , T ) {
19101898 assert ! ( self . impl_trait_defs. is_empty( ) ) ;
19111899 assert ! ( self . impl_trait_bounds. is_empty( ) ) ;
19121900
19131901 let mut predicates: SmallVec < [ hir:: WherePredicate < ' hir > ; 4 ] > = SmallVec :: new ( ) ;
1902+ // We need to make sure that generic params don't have multiple relaxed bounds for the same trait
1903+ // across generic param bounds and where bounds.
1904+ let mut dedup_map: IndexMap < LocalDefId , _ > = Default :: default ( ) ;
19141905 predicates. extend ( generics. params . iter ( ) . filter_map ( |param| {
19151906 self . lower_generic_bound_predicate (
19161907 param. ident ,
@@ -1919,25 +1910,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
19191910 & param. bounds ,
19201911 param. colon_span ,
19211912 generics. span ,
1922- RelaxedBoundPolicy :: Allowed ,
1913+ RelaxedBoundPolicy :: Allowed (
1914+ dedup_map. entry ( self . local_def_id ( param. id ) ) . or_default ( ) ,
1915+ ) ,
19231916 itctx,
19241917 PredicateOrigin :: GenericParam ,
19251918 )
19261919 } ) ) ;
1927- predicates. extend (
1928- generics
1929- . where_clause
1930- . predicates
1931- . iter ( )
1932- . map ( |predicate| self . lower_where_predicate ( predicate, & generics. params ) ) ,
1933- ) ;
1920+ predicates. extend ( generics. where_clause . predicates . iter ( ) . map ( |predicate| {
1921+ self . lower_where_predicate ( predicate, & generics. params , & mut dedup_map)
1922+ } ) ) ;
19341923
19351924 let mut params: SmallVec < [ hir:: GenericParam < ' hir > ; 4 ] > = self
19361925 . lower_generic_params_mut ( & generics. params , hir:: GenericParamSource :: Generics )
19371926 . collect ( ) ;
19381927
19391928 // Introduce extra lifetimes if late resolution tells us to.
1940- let extra_lifetimes = self . resolver . extra_lifetime_params ( parent_node_id ) ;
1929+ let extra_lifetimes = self . resolver . extra_lifetime_params ( self . owner . id ) ;
19411930 params. extend ( extra_lifetimes. into_iter ( ) . map ( |& ( ident, node_id, kind) | {
19421931 self . lifetime_res_to_generic_param (
19431932 ident,
@@ -2006,7 +1995,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20061995 bounds : & [ GenericBound ] ,
20071996 colon_span : Option < Span > ,
20081997 parent_span : Span ,
2009- rbp : RelaxedBoundPolicy ,
1998+ rbp : RelaxedBoundPolicy < ' _ > ,
20101999 itctx : ImplTraitContext ,
20112000 origin : PredicateOrigin ,
20122001 ) -> Option < hir:: WherePredicate < ' hir > > {
@@ -2071,6 +2060,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20712060 & mut self ,
20722061 pred : & WherePredicate ,
20732062 params : & [ ast:: GenericParam ] ,
2063+ dedup_map : & mut IndexMap < LocalDefId , IndexMap < DefId , Span > > ,
20742064 ) -> hir:: WherePredicate < ' hir > {
20752065 let hir_id = self . lower_node_id ( pred. id ) ;
20762066 let span = self . lower_span ( pred. span ) ;
@@ -2087,7 +2077,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20872077 && let Res :: Def ( DefKind :: TyParam , def_id) = res
20882078 && params. iter ( ) . any ( |p| def_id == self . local_def_id ( p. id ) . to_def_id ( ) )
20892079 {
2090- RelaxedBoundPolicy :: Allowed
2080+ RelaxedBoundPolicy :: Allowed ( dedup_map . entry ( def_id . expect_local ( ) ) . or_default ( ) )
20912081 } else {
20922082 RelaxedBoundPolicy :: Forbidden ( RelaxedBoundForbiddenReason :: WhereBound )
20932083 } ;
@@ -2117,7 +2107,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21172107 ) ,
21182108 bounds : self . lower_param_bounds (
21192109 bounds,
2120- RelaxedBoundPolicy :: Allowed ,
2110+ RelaxedBoundPolicy :: Allowed ( & mut Default :: default ( ) ) ,
21212111 ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
21222112 ) ,
21232113 in_where_clause : true ,
0 commit comments