@@ -16,14 +16,11 @@ use smallvec::{SmallVec, smallvec};
16
16
use thin_vec:: ThinVec ;
17
17
use tracing:: instrument;
18
18
19
- use super :: errors:: {
20
- InvalidAbi , InvalidAbiSuggestion , MisplacedRelaxTraitBound , TupleStructWithDefault ,
21
- UnionWithDefault ,
22
- } ;
19
+ use super :: errors:: { InvalidAbi , InvalidAbiSuggestion , TupleStructWithDefault , UnionWithDefault } ;
23
20
use super :: stability:: { enabled_names, gate_unstable_abi} ;
24
21
use super :: {
25
22
AstOwner , FnDeclKind , ImplTraitContext , ImplTraitPosition , LoweringContext , ParamMode ,
26
- ResolverAstLoweringExt ,
23
+ RelaxedBoundForbiddenReason , RelaxedBoundPolicy , ResolverAstLoweringExt ,
27
24
} ;
28
25
29
26
pub ( super ) struct ItemLowerer < ' a , ' hir > {
@@ -435,6 +432,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
435
432
|this| {
436
433
let bounds = this. lower_param_bounds (
437
434
bounds,
435
+ RelaxedBoundPolicy :: Forbidden ( RelaxedBoundForbiddenReason :: SuperTrait ) ,
438
436
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
439
437
) ;
440
438
let items = this. arena . alloc_from_iter (
@@ -455,6 +453,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
455
453
|this| {
456
454
this. lower_param_bounds (
457
455
bounds,
456
+ RelaxedBoundPolicy :: Allowed ,
458
457
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
459
458
)
460
459
} ,
@@ -940,6 +939,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
940
939
hir:: TraitItemKind :: Type (
941
940
this. lower_param_bounds (
942
941
bounds,
942
+ RelaxedBoundPolicy :: Allowed ,
943
943
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
944
944
) ,
945
945
ty,
@@ -1677,61 +1677,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
1677
1677
assert ! ( self . impl_trait_defs. is_empty( ) ) ;
1678
1678
assert ! ( self . impl_trait_bounds. is_empty( ) ) ;
1679
1679
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
-
1735
1680
let mut predicates: SmallVec < [ hir:: WherePredicate < ' hir > ; 4 ] > = SmallVec :: new ( ) ;
1736
1681
predicates. extend ( generics. params . iter ( ) . filter_map ( |param| {
1737
1682
self . lower_generic_bound_predicate (
@@ -1741,6 +1686,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1741
1686
& param. bounds ,
1742
1687
param. colon_span ,
1743
1688
generics. span ,
1689
+ RelaxedBoundPolicy :: Allowed ,
1744
1690
itctx,
1745
1691
PredicateOrigin :: GenericParam ,
1746
1692
)
@@ -1750,7 +1696,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1750
1696
. where_clause
1751
1697
. predicates
1752
1698
. iter ( )
1753
- . map ( |predicate| self . lower_where_predicate ( predicate) ) ,
1699
+ . map ( |predicate| self . lower_where_predicate ( predicate, & generics . params ) ) ,
1754
1700
) ;
1755
1701
1756
1702
let mut params: SmallVec < [ hir:: GenericParam < ' hir > ; 4 ] > = self
@@ -1827,6 +1773,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1827
1773
bounds : & [ GenericBound ] ,
1828
1774
colon_span : Option < Span > ,
1829
1775
parent_span : Span ,
1776
+ rbp : RelaxedBoundPolicy < ' _ > ,
1830
1777
itctx : ImplTraitContext ,
1831
1778
origin : PredicateOrigin ,
1832
1779
) -> Option < hir:: WherePredicate < ' hir > > {
@@ -1835,7 +1782,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1835
1782
return None ;
1836
1783
}
1837
1784
1838
- let bounds = self . lower_param_bounds ( bounds, itctx) ;
1785
+ let bounds = self . lower_param_bounds ( bounds, rbp , itctx) ;
1839
1786
1840
1787
let param_span = ident. span ;
1841
1788
@@ -1887,7 +1834,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
1887
1834
Some ( hir:: WherePredicate { hir_id, span, kind } )
1888
1835
}
1889
1836
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 > {
1891
1842
let hir_id = self . lower_node_id ( pred. id ) ;
1892
1843
let span = self . lower_span ( pred. span ) ;
1893
1844
self . lower_attrs ( hir_id, & pred. attrs , span) ;
@@ -1896,17 +1847,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
1896
1847
bound_generic_params,
1897
1848
bounded_ty,
1898
1849
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
+ }
1910
1873
WherePredicateKind :: RegionPredicate ( WhereRegionPredicate { lifetime, bounds } ) => {
1911
1874
hir:: WherePredicateKind :: RegionPredicate ( hir:: WhereRegionPredicate {
1912
1875
lifetime : self . lower_lifetime (
@@ -1916,6 +1879,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1916
1879
) ,
1917
1880
bounds : self . lower_param_bounds (
1918
1881
bounds,
1882
+ RelaxedBoundPolicy :: Allowed ,
1919
1883
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1920
1884
) ,
1921
1885
in_where_clause : true ,
0 commit comments