@@ -995,12 +995,12 @@ impl<'a> TyLoweringContext<'a> {
995
995
996
996
pub ( crate ) fn lower_type_bound (
997
997
& ' a self ,
998
- bound : & ' a TypeBound ,
998
+ bound : & ' a Interned < TypeBound > ,
999
999
self_ty : Ty ,
1000
1000
ignore_bindings : bool ,
1001
1001
) -> impl Iterator < Item = QuantifiedWhereClause > + ' a {
1002
1002
let mut bindings = None ;
1003
- let trait_ref = match bound {
1003
+ let trait_ref = match bound. as_ref ( ) {
1004
1004
TypeBound :: Path ( path, TraitBoundModifier :: None ) => {
1005
1005
bindings = self . lower_trait_ref_from_path ( path, Some ( self_ty) ) ;
1006
1006
bindings
@@ -1055,10 +1055,10 @@ impl<'a> TyLoweringContext<'a> {
1055
1055
1056
1056
fn assoc_type_bindings_from_type_bound (
1057
1057
& ' a self ,
1058
- bound : & ' a TypeBound ,
1058
+ bound : & ' a Interned < TypeBound > ,
1059
1059
trait_ref : TraitRef ,
1060
1060
) -> impl Iterator < Item = QuantifiedWhereClause > + ' a {
1061
- let last_segment = match bound {
1061
+ let last_segment = match bound. as_ref ( ) {
1062
1062
TypeBound :: Path ( path, TraitBoundModifier :: None ) | TypeBound :: ForLifetime ( _, path) => {
1063
1063
path. segments ( ) . last ( )
1064
1064
}
@@ -1121,7 +1121,63 @@ impl<'a> TyLoweringContext<'a> {
1121
1121
) ;
1122
1122
}
1123
1123
} else {
1124
- let ty = self . lower_ty ( type_ref) ;
1124
+ let ty = ' ty: {
1125
+ if matches ! (
1126
+ self . impl_trait_mode,
1127
+ ImplTraitLoweringState :: Param ( _)
1128
+ | ImplTraitLoweringState :: Variable ( _)
1129
+ ) {
1130
+ // Find the generic index for the target of our `bound`
1131
+ let target_param_idx = self
1132
+ . resolver
1133
+ . where_predicates_in_scope ( )
1134
+ . find_map ( |p| match p {
1135
+ WherePredicate :: TypeBound {
1136
+ target : WherePredicateTypeTarget :: TypeOrConstParam ( idx) ,
1137
+ bound : b,
1138
+ } if b == bound => Some ( idx) ,
1139
+ _ => None ,
1140
+ } ) ;
1141
+ if let Some ( target_param_idx) = target_param_idx {
1142
+ let mut counter = 0 ;
1143
+ for ( idx, data) in self . generics ( ) . params . type_or_consts . iter ( )
1144
+ {
1145
+ // Count the number of `impl Trait` things that appear before
1146
+ // the target of our `bound`.
1147
+ // Our counter within `impl_trait_mode` should be that number
1148
+ // to properly lower each types within `type_ref`
1149
+ if data. type_param ( ) . is_some_and ( |p| {
1150
+ p. provenance == TypeParamProvenance :: ArgumentImplTrait
1151
+ } ) {
1152
+ counter += 1 ;
1153
+ }
1154
+ if idx == * target_param_idx {
1155
+ break ;
1156
+ }
1157
+ }
1158
+ let mut ext = TyLoweringContext :: new_maybe_unowned (
1159
+ self . db ,
1160
+ self . resolver ,
1161
+ self . owner ,
1162
+ )
1163
+ . with_type_param_mode ( self . type_param_mode ) ;
1164
+ match & self . impl_trait_mode {
1165
+ ImplTraitLoweringState :: Param ( _) => {
1166
+ ext. impl_trait_mode =
1167
+ ImplTraitLoweringState :: Param ( Cell :: new ( counter) ) ;
1168
+ }
1169
+ ImplTraitLoweringState :: Variable ( _) => {
1170
+ ext. impl_trait_mode = ImplTraitLoweringState :: Variable (
1171
+ Cell :: new ( counter) ,
1172
+ ) ;
1173
+ }
1174
+ _ => unreachable ! ( ) ,
1175
+ }
1176
+ break ' ty ext. lower_ty ( type_ref) ;
1177
+ }
1178
+ }
1179
+ self . lower_ty ( type_ref)
1180
+ } ;
1125
1181
let alias_eq =
1126
1182
AliasEq { alias : AliasTy :: Projection ( projection_ty. clone ( ) ) , ty } ;
1127
1183
predicates. push ( crate :: wrap_empty_binders ( WhereClause :: AliasEq ( alias_eq) ) ) ;
@@ -1403,8 +1459,14 @@ pub(crate) fn generic_predicates_for_param_query(
1403
1459
assoc_name : Option < Name > ,
1404
1460
) -> Arc < [ Binders < QuantifiedWhereClause > ] > {
1405
1461
let resolver = def. resolver ( db. upcast ( ) ) ;
1406
- let ctx = TyLoweringContext :: new ( db, & resolver, def. into ( ) )
1407
- . with_type_param_mode ( ParamLoweringMode :: Variable ) ;
1462
+ let ctx = if let GenericDefId :: FunctionId ( _) = def {
1463
+ TyLoweringContext :: new ( db, & resolver, def. into ( ) )
1464
+ . with_impl_trait_mode ( ImplTraitLoweringMode :: Variable )
1465
+ . with_type_param_mode ( ParamLoweringMode :: Variable )
1466
+ } else {
1467
+ TyLoweringContext :: new ( db, & resolver, def. into ( ) )
1468
+ . with_type_param_mode ( ParamLoweringMode :: Variable )
1469
+ } ;
1408
1470
let generics = generics ( db. upcast ( ) , def) ;
1409
1471
1410
1472
// we have to filter out all other predicates *first*, before attempting to lower them
@@ -1490,8 +1552,14 @@ pub(crate) fn trait_environment_query(
1490
1552
def : GenericDefId ,
1491
1553
) -> Arc < TraitEnvironment > {
1492
1554
let resolver = def. resolver ( db. upcast ( ) ) ;
1493
- let ctx = TyLoweringContext :: new ( db, & resolver, def. into ( ) )
1494
- . with_type_param_mode ( ParamLoweringMode :: Placeholder ) ;
1555
+ let ctx = if let GenericDefId :: FunctionId ( _) = def {
1556
+ TyLoweringContext :: new ( db, & resolver, def. into ( ) )
1557
+ . with_impl_trait_mode ( ImplTraitLoweringMode :: Param )
1558
+ . with_type_param_mode ( ParamLoweringMode :: Placeholder )
1559
+ } else {
1560
+ TyLoweringContext :: new ( db, & resolver, def. into ( ) )
1561
+ . with_type_param_mode ( ParamLoweringMode :: Placeholder )
1562
+ } ;
1495
1563
let mut traits_in_scope = Vec :: new ( ) ;
1496
1564
let mut clauses = Vec :: new ( ) ;
1497
1565
for pred in resolver. where_predicates_in_scope ( ) {
@@ -1549,8 +1617,14 @@ pub(crate) fn generic_predicates_query(
1549
1617
def : GenericDefId ,
1550
1618
) -> Arc < [ Binders < QuantifiedWhereClause > ] > {
1551
1619
let resolver = def. resolver ( db. upcast ( ) ) ;
1552
- let ctx = TyLoweringContext :: new ( db, & resolver, def. into ( ) )
1553
- . with_type_param_mode ( ParamLoweringMode :: Variable ) ;
1620
+ let ctx = if let GenericDefId :: FunctionId ( _) = def {
1621
+ TyLoweringContext :: new ( db, & resolver, def. into ( ) )
1622
+ . with_impl_trait_mode ( ImplTraitLoweringMode :: Variable )
1623
+ . with_type_param_mode ( ParamLoweringMode :: Variable )
1624
+ } else {
1625
+ TyLoweringContext :: new ( db, & resolver, def. into ( ) )
1626
+ . with_type_param_mode ( ParamLoweringMode :: Variable )
1627
+ } ;
1554
1628
let generics = generics ( db. upcast ( ) , def) ;
1555
1629
1556
1630
let mut predicates = resolver
0 commit comments