@@ -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
- AstOwner , FnDeclKind , ImplTraitContext , ImplTraitPosition , LoweringContext , ParamMode ,
25
- ResolverAstLoweringExt ,
21
+ AstOwner , FnDeclKind , ImplTraitContext , ImplTraitPosition , LoweringContext ,
22
+ MaybeBoundForbiddenReason , MaybeBoundPolicy , ParamMode , 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
+ MaybeBoundPolicy :: Forbidden ( MaybeBoundForbiddenReason :: 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
+ MaybeBoundPolicy :: 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
+ MaybeBoundPolicy :: Allowed ,
941
941
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
942
942
) ,
943
943
ty,
@@ -1703,61 +1703,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
1703
1703
assert ! ( self . impl_trait_defs. is_empty( ) ) ;
1704
1704
assert ! ( self . impl_trait_bounds. is_empty( ) ) ;
1705
1705
1706
- // Error if `?Trait` bounds in where clauses don't refer directly to type parameters.
1707
- // Note: we used to clone these bounds directly onto the type parameter (and avoid lowering
1708
- // these into hir when we lower thee where clauses), but this makes it quite difficult to
1709
- // keep track of the Span info. Now, `<dyn HirTyLowerer>::add_implicit_sized_bound`
1710
- // checks both param bounds and where clauses for `?Sized`.
1711
- for pred in & generics. where_clause . predicates {
1712
- let WherePredicateKind :: BoundPredicate ( bound_pred) = & pred. kind else {
1713
- continue ;
1714
- } ;
1715
- let compute_is_param = || {
1716
- // Check if the where clause type is a plain type parameter.
1717
- match self
1718
- . resolver
1719
- . get_partial_res ( bound_pred. bounded_ty . id )
1720
- . and_then ( |r| r. full_res ( ) )
1721
- {
1722
- Some ( Res :: Def ( DefKind :: TyParam , def_id) )
1723
- if bound_pred. bound_generic_params . is_empty ( ) =>
1724
- {
1725
- generics
1726
- . params
1727
- . iter ( )
1728
- . any ( |p| def_id == self . local_def_id ( p. id ) . to_def_id ( ) )
1729
- }
1730
- // Either the `bounded_ty` is not a plain type parameter, or
1731
- // it's not found in the generic type parameters list.
1732
- _ => false ,
1733
- }
1734
- } ;
1735
- // We only need to compute this once per `WherePredicate`, but don't
1736
- // need to compute this at all unless there is a Maybe bound.
1737
- let mut is_param: Option < bool > = None ;
1738
- for bound in & bound_pred. bounds {
1739
- if !matches ! (
1740
- * bound,
1741
- GenericBound :: Trait ( PolyTraitRef {
1742
- modifiers: TraitBoundModifiers { polarity: BoundPolarity :: Maybe ( _) , .. } ,
1743
- ..
1744
- } )
1745
- ) {
1746
- continue ;
1747
- }
1748
- let is_param = * is_param. get_or_insert_with ( compute_is_param) ;
1749
- if !is_param && !self . tcx . features ( ) . more_maybe_bounds ( ) {
1750
- self . tcx
1751
- . sess
1752
- . create_feature_err (
1753
- MisplacedRelaxTraitBound { span : bound. span ( ) } ,
1754
- sym:: more_maybe_bounds,
1755
- )
1756
- . emit ( ) ;
1757
- }
1758
- }
1759
- }
1760
-
1761
1706
let mut predicates: SmallVec < [ hir:: WherePredicate < ' hir > ; 4 ] > = SmallVec :: new ( ) ;
1762
1707
predicates. extend ( generics. params . iter ( ) . filter_map ( |param| {
1763
1708
self . lower_generic_bound_predicate (
@@ -1767,6 +1712,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1767
1712
& param. bounds ,
1768
1713
param. colon_span ,
1769
1714
generics. span ,
1715
+ MaybeBoundPolicy :: Allowed ,
1770
1716
itctx,
1771
1717
PredicateOrigin :: GenericParam ,
1772
1718
)
@@ -1776,7 +1722,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1776
1722
. where_clause
1777
1723
. predicates
1778
1724
. iter ( )
1779
- . map ( |predicate| self . lower_where_predicate ( predicate) ) ,
1725
+ . map ( |predicate| self . lower_where_predicate ( predicate, & generics . params ) ) ,
1780
1726
) ;
1781
1727
1782
1728
let mut params: SmallVec < [ hir:: GenericParam < ' hir > ; 4 ] > = self
@@ -1853,6 +1799,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1853
1799
bounds : & [ GenericBound ] ,
1854
1800
colon_span : Option < Span > ,
1855
1801
parent_span : Span ,
1802
+ mbp : MaybeBoundPolicy < ' _ > ,
1856
1803
itctx : ImplTraitContext ,
1857
1804
origin : PredicateOrigin ,
1858
1805
) -> Option < hir:: WherePredicate < ' hir > > {
@@ -1861,7 +1808,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1861
1808
return None ;
1862
1809
}
1863
1810
1864
- let bounds = self . lower_param_bounds ( bounds, itctx) ;
1811
+ let bounds = self . lower_param_bounds ( bounds, mbp , itctx) ;
1865
1812
1866
1813
let param_span = ident. span ;
1867
1814
@@ -1913,7 +1860,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
1913
1860
Some ( hir:: WherePredicate { hir_id, span, kind } )
1914
1861
}
1915
1862
1916
- fn lower_where_predicate ( & mut self , pred : & WherePredicate ) -> hir:: WherePredicate < ' hir > {
1863
+ fn lower_where_predicate (
1864
+ & mut self ,
1865
+ pred : & WherePredicate ,
1866
+ params : & [ ast:: GenericParam ] ,
1867
+ ) -> hir:: WherePredicate < ' hir > {
1917
1868
let hir_id = self . lower_node_id ( pred. id ) ;
1918
1869
let span = self . lower_span ( pred. span ) ;
1919
1870
self . lower_attrs ( hir_id, & pred. attrs , span) ;
@@ -1922,17 +1873,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
1922
1873
bound_generic_params,
1923
1874
bounded_ty,
1924
1875
bounds,
1925
- } ) => hir:: WherePredicateKind :: BoundPredicate ( hir:: WhereBoundPredicate {
1926
- bound_generic_params : self
1927
- . lower_generic_params ( bound_generic_params, hir:: GenericParamSource :: Binder ) ,
1928
- bounded_ty : self
1929
- . lower_ty ( bounded_ty, ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ) ,
1930
- bounds : self . lower_param_bounds (
1931
- bounds,
1932
- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1933
- ) ,
1934
- origin : PredicateOrigin :: WhereClause ,
1935
- } ) ,
1876
+ } ) => {
1877
+ let mbp = match bound_generic_params. is_empty ( ) {
1878
+ true => MaybeBoundPolicy :: AllowedIfOwnTyParam ( bounded_ty. id , params) ,
1879
+ false => MaybeBoundPolicy :: Forbidden ( MaybeBoundForbiddenReason :: Other ) ,
1880
+ } ;
1881
+ hir:: WherePredicateKind :: BoundPredicate ( hir:: WhereBoundPredicate {
1882
+ bound_generic_params : self . lower_generic_params (
1883
+ bound_generic_params,
1884
+ hir:: GenericParamSource :: Binder ,
1885
+ ) ,
1886
+ bounded_ty : self . lower_ty (
1887
+ bounded_ty,
1888
+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1889
+ ) ,
1890
+ bounds : self . lower_param_bounds (
1891
+ bounds,
1892
+ mbp,
1893
+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1894
+ ) ,
1895
+ origin : PredicateOrigin :: WhereClause ,
1896
+ } )
1897
+ }
1936
1898
WherePredicateKind :: RegionPredicate ( WhereRegionPredicate { lifetime, bounds } ) => {
1937
1899
hir:: WherePredicateKind :: RegionPredicate ( hir:: WhereRegionPredicate {
1938
1900
lifetime : self . lower_lifetime (
@@ -1942,6 +1904,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1942
1904
) ,
1943
1905
bounds : self . lower_param_bounds (
1944
1906
bounds,
1907
+ MaybeBoundPolicy :: Allowed ,
1945
1908
ImplTraitContext :: Disallowed ( ImplTraitPosition :: Bound ) ,
1946
1909
) ,
1947
1910
in_where_clause : true ,
0 commit comments