@@ -16,14 +16,11 @@ use smallvec::{SmallVec, smallvec};
1616use  thin_vec:: ThinVec ; 
1717use  tracing:: instrument; 
1818
19- use  super :: errors:: { 
20-     InvalidAbi ,  InvalidAbiSuggestion ,  MisplacedRelaxTraitBound ,  TupleStructWithDefault , 
21-     UnionWithDefault , 
22- } ; 
19+ use  super :: errors:: { InvalidAbi ,  InvalidAbiSuggestion ,  TupleStructWithDefault ,  UnionWithDefault } ; 
2320use  super :: stability:: { enabled_names,  gate_unstable_abi} ; 
2421use  super :: { 
2522    AstOwner ,  FnDeclKind ,  ImplTraitContext ,  ImplTraitPosition ,  LoweringContext ,  ParamMode , 
26-     ResolverAstLoweringExt , 
23+     RelaxedBoundForbiddenReason ,   RelaxedBoundPolicy ,   ResolverAstLoweringExt , 
2724} ; 
2825
2926pub ( super )  struct  ItemLowerer < ' a ,  ' hir >  { 
@@ -435,6 +432,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
435432                    |this| { 
436433                        let  bounds = this. lower_param_bounds ( 
437434                            bounds, 
435+                             RelaxedBoundPolicy :: Forbidden ( RelaxedBoundForbiddenReason :: SuperTrait ) , 
438436                            ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) , 
439437                        ) ; 
440438                        let  items = this. arena . alloc_from_iter ( 
@@ -455,6 +453,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
455453                    |this| { 
456454                        this. lower_param_bounds ( 
457455                            bounds, 
456+                             RelaxedBoundPolicy :: Allowed , 
458457                            ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) , 
459458                        ) 
460459                    } , 
@@ -940,6 +939,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
940939                        hir:: TraitItemKind :: Type ( 
941940                            this. lower_param_bounds ( 
942941                                bounds, 
942+                                 RelaxedBoundPolicy :: Allowed , 
943943                                ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) , 
944944                            ) , 
945945                            ty, 
@@ -1677,61 +1677,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
16771677        assert ! ( self . impl_trait_defs. is_empty( ) ) ; 
16781678        assert ! ( self . impl_trait_bounds. is_empty( ) ) ; 
16791679
1680-         // Error if `?Trait` bounds in where clauses don't refer directly to type parameters. 
1681-         // Note: we used to clone these bounds directly onto the type parameter (and avoid lowering 
1682-         // these into hir when we lower thee where clauses), but this makes it quite difficult to 
1683-         // keep track of the Span info. Now, `<dyn HirTyLowerer>::add_implicit_sized_bound` 
1684-         // checks both param bounds and where clauses for `?Sized`. 
1685-         for  pred in  & generics. where_clause . predicates  { 
1686-             let  WherePredicateKind :: BoundPredicate ( bound_pred)  = & pred. kind  else  { 
1687-                 continue ; 
1688-             } ; 
1689-             let  compute_is_param = || { 
1690-                 // Check if the where clause type is a plain type parameter. 
1691-                 match  self 
1692-                     . resolver 
1693-                     . get_partial_res ( bound_pred. bounded_ty . id ) 
1694-                     . and_then ( |r| r. full_res ( ) ) 
1695-                 { 
1696-                     Some ( Res :: Def ( DefKind :: TyParam ,  def_id) ) 
1697-                         if  bound_pred. bound_generic_params . is_empty ( )  =>
1698-                     { 
1699-                         generics
1700-                             . params 
1701-                             . iter ( ) 
1702-                             . any ( |p| def_id == self . local_def_id ( p. id ) . to_def_id ( ) ) 
1703-                     } 
1704-                     // Either the `bounded_ty` is not a plain type parameter, or 
1705-                     // it's not found in the generic type parameters list. 
1706-                     _ => false , 
1707-                 } 
1708-             } ; 
1709-             // We only need to compute this once per `WherePredicate`, but don't 
1710-             // need to compute this at all unless there is a Maybe bound. 
1711-             let  mut  is_param:  Option < bool >  = None ; 
1712-             for  bound in  & bound_pred. bounds  { 
1713-                 if  !matches ! ( 
1714-                     * bound, 
1715-                     GenericBound :: Trait ( PolyTraitRef  { 
1716-                         modifiers:  TraitBoundModifiers  {  polarity:  BoundPolarity :: Maybe ( _) ,  .. } , 
1717-                         ..
1718-                     } ) 
1719-                 )  { 
1720-                     continue ; 
1721-                 } 
1722-                 let  is_param = * is_param. get_or_insert_with ( compute_is_param) ; 
1723-                 if  !is_param && !self . tcx . features ( ) . more_maybe_bounds ( )  { 
1724-                     self . tcx 
1725-                         . sess 
1726-                         . create_feature_err ( 
1727-                             MisplacedRelaxTraitBound  {  span :  bound. span ( )  } , 
1728-                             sym:: more_maybe_bounds, 
1729-                         ) 
1730-                         . emit ( ) ; 
1731-                 } 
1732-             } 
1733-         } 
1734- 
17351680        let  mut  predicates:  SmallVec < [ hir:: WherePredicate < ' hir > ;  4 ] >  = SmallVec :: new ( ) ; 
17361681        predicates. extend ( generics. params . iter ( ) . filter_map ( |param| { 
17371682            self . lower_generic_bound_predicate ( 
@@ -1741,6 +1686,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17411686                & param. bounds , 
17421687                param. colon_span , 
17431688                generics. span , 
1689+                 RelaxedBoundPolicy :: Allowed , 
17441690                itctx, 
17451691                PredicateOrigin :: GenericParam , 
17461692            ) 
@@ -1750,7 +1696,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17501696                . where_clause 
17511697                . predicates 
17521698                . iter ( ) 
1753-                 . map ( |predicate| self . lower_where_predicate ( predicate) ) , 
1699+                 . map ( |predicate| self . lower_where_predicate ( predicate,   & generics . params ) ) , 
17541700        ) ; 
17551701
17561702        let  mut  params:  SmallVec < [ hir:: GenericParam < ' hir > ;  4 ] >  = self 
@@ -1827,6 +1773,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18271773        bounds :  & [ GenericBound ] , 
18281774        colon_span :  Option < Span > , 
18291775        parent_span :  Span , 
1776+         rbp :  RelaxedBoundPolicy < ' _ > , 
18301777        itctx :  ImplTraitContext , 
18311778        origin :  PredicateOrigin , 
18321779    )  -> Option < hir:: WherePredicate < ' hir > >  { 
@@ -1835,7 +1782,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18351782            return  None ; 
18361783        } 
18371784
1838-         let  bounds = self . lower_param_bounds ( bounds,  itctx) ; 
1785+         let  bounds = self . lower_param_bounds ( bounds,  rbp ,   itctx) ; 
18391786
18401787        let  param_span = ident. span ; 
18411788
@@ -1887,7 +1834,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
18871834        Some ( hir:: WherePredicate  {  hir_id,  span,  kind } ) 
18881835    } 
18891836
1890-     fn  lower_where_predicate ( & mut  self ,  pred :  & WherePredicate )  -> hir:: WherePredicate < ' hir >  { 
1837+     fn  lower_where_predicate ( 
1838+         & mut  self , 
1839+         pred :  & WherePredicate , 
1840+         params :  & [ ast:: GenericParam ] , 
1841+     )  -> hir:: WherePredicate < ' hir >  { 
18911842        let  hir_id = self . lower_node_id ( pred. id ) ; 
18921843        let  span = self . lower_span ( pred. span ) ; 
18931844        self . lower_attrs ( hir_id,  & pred. attrs ,  span) ; 
@@ -1896,17 +1847,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
18961847                bound_generic_params, 
18971848                bounded_ty, 
18981849                bounds, 
1899-             } )  => hir:: WherePredicateKind :: BoundPredicate ( hir:: WhereBoundPredicate  { 
1900-                 bound_generic_params :  self 
1901-                     . lower_generic_params ( bound_generic_params,  hir:: GenericParamSource :: Binder ) , 
1902-                 bounded_ty :  self 
1903-                     . lower_ty ( bounded_ty,  ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ) , 
1904-                 bounds :  self . lower_param_bounds ( 
1905-                     bounds, 
1906-                     ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) , 
1907-                 ) , 
1908-                 origin :  PredicateOrigin :: WhereClause , 
1909-             } ) , 
1850+             } )  => { 
1851+                 let  rbp = if  bound_generic_params. is_empty ( )  { 
1852+                     RelaxedBoundPolicy :: AllowedIfOnTyParam ( bounded_ty. id ,  params) 
1853+                 }  else  { 
1854+                     RelaxedBoundPolicy :: Forbidden ( RelaxedBoundForbiddenReason :: LateBoundVarsInScope ) 
1855+                 } ; 
1856+                 hir:: WherePredicateKind :: BoundPredicate ( hir:: WhereBoundPredicate  { 
1857+                     bound_generic_params :  self . lower_generic_params ( 
1858+                         bound_generic_params, 
1859+                         hir:: GenericParamSource :: Binder , 
1860+                     ) , 
1861+                     bounded_ty :  self . lower_ty ( 
1862+                         bounded_ty, 
1863+                         ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) , 
1864+                     ) , 
1865+                     bounds :  self . lower_param_bounds ( 
1866+                         bounds, 
1867+                         rbp, 
1868+                         ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) , 
1869+                     ) , 
1870+                     origin :  PredicateOrigin :: WhereClause , 
1871+                 } ) 
1872+             } 
19101873            WherePredicateKind :: RegionPredicate ( WhereRegionPredicate  {  lifetime,  bounds } )  => { 
19111874                hir:: WherePredicateKind :: RegionPredicate ( hir:: WhereRegionPredicate  { 
19121875                    lifetime :  self . lower_lifetime ( 
@@ -1916,6 +1879,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19161879                    ) , 
19171880                    bounds :  self . lower_param_bounds ( 
19181881                        bounds, 
1882+                         RelaxedBoundPolicy :: Allowed , 
19191883                        ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) , 
19201884                    ) , 
19211885                    in_where_clause :  true , 
0 commit comments