@@ -15,14 +15,11 @@ use smallvec::{SmallVec, smallvec};
15
15
use thin_vec:: ThinVec ;
16
16
use tracing:: instrument;
17
17
18
- use super :: errors:: {
19
- InvalidAbi , InvalidAbiSuggestion , MisplacedRelaxTraitBound , TupleStructWithDefault ,
20
- UnionWithDefault ,
21
- } ;
18
+ use super :: errors:: { InvalidAbi , InvalidAbiSuggestion , TupleStructWithDefault , UnionWithDefault } ;
22
19
use super :: stability:: { enabled_names, gate_unstable_abi} ;
23
20
use super :: {
24
21
AstOwner , FnDeclKind , ImplTraitContext , ImplTraitPosition , LoweringContext , ParamMode ,
25
- ResolverAstLoweringExt ,
22
+ RelaxedBoundForbiddenReason , RelaxedBoundPolicy , ResolverAstLoweringExt ,
26
23
} ;
27
24
28
25
pub ( super ) struct ItemLowerer < ' a , ' hir > {
@@ -427,6 +424,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
427
424
|this| {
428
425
let bounds = this. lower_param_bounds (
429
426
bounds,
427
+ RelaxedBoundPolicy :: Forbidden ( RelaxedBoundForbiddenReason :: SuperTrait ) ,
430
428
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
431
429
) ;
432
430
let items = this. arena . alloc_from_iter (
@@ -447,6 +445,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
447
445
|this| {
448
446
this. lower_param_bounds (
449
447
bounds,
448
+ RelaxedBoundPolicy :: Allowed ,
450
449
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
451
450
)
452
451
} ,
@@ -938,6 +937,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
938
937
hir:: TraitItemKind :: Type (
939
938
this. lower_param_bounds (
940
939
bounds,
940
+ RelaxedBoundPolicy :: Allowed ,
941
941
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
942
942
) ,
943
943
ty,
@@ -1723,61 +1723,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
1723
1723
assert ! ( self . impl_trait_defs. is_empty( ) ) ;
1724
1724
assert ! ( self . impl_trait_bounds. is_empty( ) ) ;
1725
1725
1726
- // Error if `?Trait` bounds in where clauses don't refer directly to type parameters.
1727
- // Note: we used to clone these bounds directly onto the type parameter (and avoid lowering
1728
- // these into hir when we lower thee where clauses), but this makes it quite difficult to
1729
- // keep track of the Span info. Now, `<dyn HirTyLowerer>::add_implicit_sized_bound`
1730
- // checks both param bounds and where clauses for `?Sized`.
1731
- for pred in & generics. where_clause . predicates {
1732
- let WherePredicateKind :: BoundPredicate ( bound_pred) = & pred. kind else {
1733
- continue ;
1734
- } ;
1735
- let compute_is_param = || {
1736
- // Check if the where clause type is a plain type parameter.
1737
- match self
1738
- . resolver
1739
- . get_partial_res ( bound_pred. bounded_ty . id )
1740
- . and_then ( |r| r. full_res ( ) )
1741
- {
1742
- Some ( Res :: Def ( DefKind :: TyParam , def_id) )
1743
- if bound_pred. bound_generic_params . is_empty ( ) =>
1744
- {
1745
- generics
1746
- . params
1747
- . iter ( )
1748
- . any ( |p| def_id == self . local_def_id ( p. id ) . to_def_id ( ) )
1749
- }
1750
- // Either the `bounded_ty` is not a plain type parameter, or
1751
- // it's not found in the generic type parameters list.
1752
- _ => false ,
1753
- }
1754
- } ;
1755
- // We only need to compute this once per `WherePredicate`, but don't
1756
- // need to compute this at all unless there is a Maybe bound.
1757
- let mut is_param: Option < bool > = None ;
1758
- for bound in & bound_pred. bounds {
1759
- if !matches ! (
1760
- * bound,
1761
- GenericBound :: Trait ( PolyTraitRef {
1762
- modifiers: TraitBoundModifiers { polarity: BoundPolarity :: Maybe ( _) , .. } ,
1763
- ..
1764
- } )
1765
- ) {
1766
- continue ;
1767
- }
1768
- let is_param = * is_param. get_or_insert_with ( compute_is_param) ;
1769
- if !is_param && !self . tcx . features ( ) . more_maybe_bounds ( ) {
1770
- self . tcx
1771
- . sess
1772
- . create_feature_err (
1773
- MisplacedRelaxTraitBound { span : bound. span ( ) } ,
1774
- sym:: more_maybe_bounds,
1775
- )
1776
- . emit ( ) ;
1777
- }
1778
- }
1779
- }
1780
-
1781
1726
let mut predicates: SmallVec < [ hir:: WherePredicate < ' hir > ; 4 ] > = SmallVec :: new ( ) ;
1782
1727
predicates. extend ( generics. params . iter ( ) . filter_map ( |param| {
1783
1728
self . lower_generic_bound_predicate (
@@ -1787,6 +1732,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1787
1732
& param. bounds ,
1788
1733
param. colon_span ,
1789
1734
generics. span ,
1735
+ RelaxedBoundPolicy :: Allowed ,
1790
1736
itctx,
1791
1737
PredicateOrigin :: GenericParam ,
1792
1738
)
@@ -1796,7 +1742,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1796
1742
. where_clause
1797
1743
. predicates
1798
1744
. iter ( )
1799
- . map ( |predicate| self . lower_where_predicate ( predicate) ) ,
1745
+ . map ( |predicate| self . lower_where_predicate ( predicate, & generics . params ) ) ,
1800
1746
) ;
1801
1747
1802
1748
let mut params: SmallVec < [ hir:: GenericParam < ' hir > ; 4 ] > = self
@@ -1873,6 +1819,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1873
1819
bounds : & [ GenericBound ] ,
1874
1820
colon_span : Option < Span > ,
1875
1821
parent_span : Span ,
1822
+ rbp : RelaxedBoundPolicy < ' _ > ,
1876
1823
itctx : ImplTraitContext ,
1877
1824
origin : PredicateOrigin ,
1878
1825
) -> Option < hir:: WherePredicate < ' hir > > {
@@ -1881,7 +1828,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1881
1828
return None ;
1882
1829
}
1883
1830
1884
- let bounds = self . lower_param_bounds ( bounds, itctx) ;
1831
+ let bounds = self . lower_param_bounds ( bounds, rbp , itctx) ;
1885
1832
1886
1833
let param_span = ident. span ;
1887
1834
@@ -1933,7 +1880,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
1933
1880
Some ( hir:: WherePredicate { hir_id, span, kind } )
1934
1881
}
1935
1882
1936
- fn lower_where_predicate ( & mut self , pred : & WherePredicate ) -> hir:: WherePredicate < ' hir > {
1883
+ fn lower_where_predicate (
1884
+ & mut self ,
1885
+ pred : & WherePredicate ,
1886
+ params : & [ ast:: GenericParam ] ,
1887
+ ) -> hir:: WherePredicate < ' hir > {
1937
1888
let hir_id = self . lower_node_id ( pred. id ) ;
1938
1889
let span = self . lower_span ( pred. span ) ;
1939
1890
self . lower_attrs ( hir_id, & pred. attrs , span) ;
@@ -1942,17 +1893,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
1942
1893
bound_generic_params,
1943
1894
bounded_ty,
1944
1895
bounds,
1945
- } ) => hir:: WherePredicateKind :: BoundPredicate ( hir:: WhereBoundPredicate {
1946
- bound_generic_params : self
1947
- . lower_generic_params ( bound_generic_params, hir:: GenericParamSource :: Binder ) ,
1948
- bounded_ty : self
1949
- . lower_ty ( bounded_ty, ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ) ,
1950
- bounds : self . lower_param_bounds (
1951
- bounds,
1952
- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1953
- ) ,
1954
- origin : PredicateOrigin :: WhereClause ,
1955
- } ) ,
1896
+ } ) => {
1897
+ let rbp = if bound_generic_params. is_empty ( ) {
1898
+ RelaxedBoundPolicy :: AllowedIfOnTyParam ( bounded_ty. id , params)
1899
+ } else {
1900
+ RelaxedBoundPolicy :: Forbidden ( RelaxedBoundForbiddenReason :: LateBoundVarsInScope )
1901
+ } ;
1902
+ hir:: WherePredicateKind :: BoundPredicate ( hir:: WhereBoundPredicate {
1903
+ bound_generic_params : self . lower_generic_params (
1904
+ bound_generic_params,
1905
+ hir:: GenericParamSource :: Binder ,
1906
+ ) ,
1907
+ bounded_ty : self . lower_ty (
1908
+ bounded_ty,
1909
+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1910
+ ) ,
1911
+ bounds : self . lower_param_bounds (
1912
+ bounds,
1913
+ rbp,
1914
+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1915
+ ) ,
1916
+ origin : PredicateOrigin :: WhereClause ,
1917
+ } )
1918
+ }
1956
1919
WherePredicateKind :: RegionPredicate ( WhereRegionPredicate { lifetime, bounds } ) => {
1957
1920
hir:: WherePredicateKind :: RegionPredicate ( hir:: WhereRegionPredicate {
1958
1921
lifetime : self . lower_lifetime (
@@ -1962,6 +1925,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1962
1925
) ,
1963
1926
bounds : self . lower_param_bounds (
1964
1927
bounds,
1928
+ RelaxedBoundPolicy :: Allowed ,
1965
1929
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1966
1930
) ,
1967
1931
in_where_clause : true ,
0 commit comments