@@ -121,6 +121,9 @@ struct LoweringContext<'a, 'hir: 'a> {
121
121
local_id_to_def_id : SortedMap < ItemLocalId , LocalDefId > ,
122
122
trait_map : FxHashMap < ItemLocalId , Box < [ TraitCandidate ] > > ,
123
123
124
+ impl_trait_defs : Vec < hir:: GenericParam < ' hir > > ,
125
+ impl_trait_bounds : Vec < hir:: WherePredicate < ' hir > > ,
126
+
124
127
/// NodeIds that are lowered inside the current HIR owner.
125
128
node_id_to_local_id : FxHashMap < NodeId , hir:: ItemLocalId > ,
126
129
@@ -244,13 +247,13 @@ pub trait ResolverAstLowering {
244
247
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
245
248
/// and if so, what meaning it has.
246
249
#[ derive( Debug ) ]
247
- enum ImplTraitContext < ' b , ' a > {
250
+ enum ImplTraitContext {
248
251
/// Treat `impl Trait` as shorthand for a new universal generic parameter.
249
252
/// Example: `fn foo(x: impl Debug)`, where `impl Debug` is conceptually
250
253
/// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
251
254
///
252
255
/// Newly generated parameters should be inserted into the given `Vec`.
253
- Universal ( & ' b mut Vec < hir :: GenericParam < ' a > > , & ' b mut Vec < hir :: WherePredicate < ' a > > , LocalDefId ) ,
256
+ Universal ( LocalDefId ) ,
254
257
255
258
/// Treat `impl Trait` as shorthand for a new opaque type.
256
259
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
@@ -290,11 +293,11 @@ enum ImplTraitPosition {
290
293
ImplReturn ,
291
294
}
292
295
293
- impl < ' a > ImplTraitContext < ' _ , ' a > {
294
- fn reborrow < ' this > ( & ' this mut self ) -> ImplTraitContext < ' this , ' a > {
296
+ impl ImplTraitContext {
297
+ fn reborrow < ' this > ( & ' this mut self ) -> ImplTraitContext {
295
298
use self :: ImplTraitContext :: * ;
296
299
match self {
297
- Universal ( params , bounds , parent) => Universal ( params , bounds , * parent) ,
300
+ Universal ( parent) => Universal ( * parent) ,
298
301
ReturnPositionOpaqueTy { origin } => ReturnPositionOpaqueTy { origin : * origin } ,
299
302
TypeAliasesOpaqueTy => TypeAliasesOpaqueTy ,
300
303
Disallowed ( pos) => Disallowed ( * pos) ,
@@ -701,34 +704,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
701
704
& mut self ,
702
705
generics : & Generics ,
703
706
parent_node_id : NodeId ,
704
- f : impl FnOnce (
705
- & mut Self ,
706
- & mut Vec < hir:: GenericParam < ' hir > > ,
707
- & mut Vec < hir:: WherePredicate < ' hir > > ,
708
- ) -> T ,
707
+ f : impl FnOnce ( & mut Self ) -> T ,
709
708
) -> ( & ' hir hir:: Generics < ' hir > , T ) {
710
- let mut impl_trait_defs = Vec :: new ( ) ;
711
- let mut impl_trait_bounds = Vec :: new ( ) ;
712
- let mut lowered_generics = self . lower_generics_mut (
713
- generics,
714
- ImplTraitContext :: Universal (
715
- & mut impl_trait_defs,
716
- & mut impl_trait_bounds,
717
- self . current_hir_id_owner ,
718
- ) ,
719
- ) ;
720
- let res = f ( self , & mut impl_trait_defs, & mut impl_trait_bounds) ;
709
+ let mut lowered_generics = self
710
+ . lower_generics_mut ( generics, ImplTraitContext :: Universal ( self . current_hir_id_owner ) ) ;
711
+ let res = f ( self ) ;
721
712
722
713
let extra_lifetimes = self . resolver . take_extra_lifetime_params ( parent_node_id) ;
714
+ let impl_trait_defs = std:: mem:: take ( & mut self . impl_trait_defs ) ;
723
715
lowered_generics. params . extend (
724
716
extra_lifetimes
725
717
. into_iter ( )
726
718
. filter_map ( |( ident, node_id, res) | {
727
719
self . lifetime_res_to_generic_param ( ident, node_id, res)
728
720
} )
729
- . chain ( impl_trait_defs) ,
721
+ . chain ( impl_trait_defs. into_iter ( ) ) ,
730
722
) ;
731
- lowered_generics. predicates . extend ( impl_trait_bounds) ;
723
+ let impl_trait_bounds = std:: mem:: take ( & mut self . impl_trait_bounds ) ;
724
+ lowered_generics. predicates . extend ( impl_trait_bounds. into_iter ( ) ) ;
732
725
733
726
let lowered_generics = lowered_generics. into_generics ( self . arena ) ;
734
727
( lowered_generics, res)
@@ -898,7 +891,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
898
891
fn lower_assoc_ty_constraint (
899
892
& mut self ,
900
893
constraint : & AssocConstraint ,
901
- mut itctx : ImplTraitContext < ' _ , ' hir > ,
894
+ mut itctx : ImplTraitContext ,
902
895
) -> hir:: TypeBinding < ' hir > {
903
896
debug ! ( "lower_assoc_ty_constraint(constraint={:?}, itctx={:?})" , constraint, itctx) ;
904
897
@@ -962,7 +955,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
962
955
// so desugar to
963
956
//
964
957
// fn foo(x: dyn Iterator<Item = impl Debug>)
965
- ImplTraitContext :: Universal ( _ , _ , parent) if self . is_in_dyn_type => {
958
+ ImplTraitContext :: Universal ( parent) if self . is_in_dyn_type => {
966
959
parent_def_id = parent;
967
960
( true , itctx)
968
961
}
@@ -1036,7 +1029,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1036
1029
fn lower_generic_arg (
1037
1030
& mut self ,
1038
1031
arg : & ast:: GenericArg ,
1039
- itctx : ImplTraitContext < ' _ , ' hir > ,
1032
+ itctx : ImplTraitContext ,
1040
1033
) -> hir:: GenericArg < ' hir > {
1041
1034
match arg {
1042
1035
ast:: GenericArg :: Lifetime ( lt) => GenericArg :: Lifetime ( self . lower_lifetime ( & lt) ) ,
@@ -1103,7 +1096,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1103
1096
}
1104
1097
}
1105
1098
1106
- fn lower_ty ( & mut self , t : & Ty , itctx : ImplTraitContext < ' _ , ' hir > ) -> & ' hir hir:: Ty < ' hir > {
1099
+ fn lower_ty ( & mut self , t : & Ty , itctx : ImplTraitContext ) -> & ' hir hir:: Ty < ' hir > {
1107
1100
self . arena . alloc ( self . lower_ty_direct ( t, itctx) )
1108
1101
}
1109
1102
@@ -1113,7 +1106,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1113
1106
qself : & Option < QSelf > ,
1114
1107
path : & Path ,
1115
1108
param_mode : ParamMode ,
1116
- itctx : ImplTraitContext < ' _ , ' hir > ,
1109
+ itctx : ImplTraitContext ,
1117
1110
) -> hir:: Ty < ' hir > {
1118
1111
let id = self . lower_node_id ( t. id ) ;
1119
1112
let qpath = self . lower_qpath ( t. id , qself, path, param_mode, itctx) ;
@@ -1128,7 +1121,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1128
1121
self . ty ( span, hir:: TyKind :: Tup ( tys) )
1129
1122
}
1130
1123
1131
- fn lower_ty_direct ( & mut self , t : & Ty , mut itctx : ImplTraitContext < ' _ , ' hir > ) -> hir:: Ty < ' hir > {
1124
+ fn lower_ty_direct ( & mut self , t : & Ty , mut itctx : ImplTraitContext ) -> hir:: Ty < ' hir > {
1132
1125
let kind = match t. kind {
1133
1126
TyKind :: Infer => hir:: TyKind :: Infer ,
1134
1127
TyKind :: Err => hir:: TyKind :: Err ,
@@ -1235,40 +1228,32 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1235
1228
|this| this. lower_param_bounds ( bounds, nested_itctx) ,
1236
1229
)
1237
1230
}
1238
- ImplTraitContext :: Universal (
1239
- in_band_ty_params,
1240
- in_band_ty_bounds,
1241
- parent_def_id,
1242
- ) => {
1231
+ ImplTraitContext :: Universal ( parent_def_id) => {
1243
1232
// Add a definition for the in-band `Param`.
1244
1233
let def_id = self . resolver . local_def_id ( def_node_id) ;
1245
1234
1246
- let hir_bounds = self . lower_param_bounds (
1247
- bounds,
1248
- ImplTraitContext :: Universal (
1249
- in_band_ty_params,
1250
- in_band_ty_bounds,
1251
- parent_def_id,
1252
- ) ,
1253
- ) ;
1235
+ let hir_bounds = self
1236
+ . lower_param_bounds ( bounds, ImplTraitContext :: Universal ( parent_def_id) ) ;
1254
1237
// Set the name to `impl Bound1 + Bound2`.
1255
1238
let ident = Ident :: from_str_and_span ( & pprust:: ty_to_string ( t) , span) ;
1256
- in_band_ty_params . push ( hir:: GenericParam {
1239
+ let param = hir:: GenericParam {
1257
1240
hir_id : self . lower_node_id ( def_node_id) ,
1258
1241
name : ParamName :: Plain ( self . lower_ident ( ident) ) ,
1259
1242
pure_wrt_drop : false ,
1260
1243
span : self . lower_span ( span) ,
1261
1244
kind : hir:: GenericParamKind :: Type { default : None , synthetic : true } ,
1262
1245
colon_span : None ,
1263
- } ) ;
1246
+ } ;
1247
+ self . impl_trait_defs . push ( param) ;
1248
+
1264
1249
if let Some ( preds) = self . lower_generic_bound_predicate (
1265
1250
ident,
1266
1251
def_node_id,
1267
1252
& GenericParamKind :: Type { default : None } ,
1268
1253
hir_bounds,
1269
1254
hir:: PredicateOrigin :: ImplTrait ,
1270
1255
) {
1271
- in_band_ty_bounds . push ( preds)
1256
+ self . impl_trait_bounds . push ( preds)
1272
1257
}
1273
1258
1274
1259
hir:: TyKind :: Path ( hir:: QPath :: Resolved (
@@ -1442,21 +1427,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1442
1427
fn lower_fn_decl (
1443
1428
& mut self ,
1444
1429
decl : & FnDecl ,
1445
- mut in_band_ty_params : Option < (
1446
- NodeId ,
1447
- & mut Vec < hir:: GenericParam < ' hir > > ,
1448
- & mut Vec < hir:: WherePredicate < ' hir > > ,
1449
- ) > ,
1430
+ fn_node_id : Option < NodeId > ,
1450
1431
kind : FnDeclKind ,
1451
1432
make_ret_async : Option < NodeId > ,
1452
1433
) -> & ' hir hir:: FnDecl < ' hir > {
1453
1434
debug ! (
1454
1435
"lower_fn_decl(\
1455
1436
fn_decl: {:?}, \
1456
- in_band_ty_params : {:?}, \
1437
+ fn_node_id : {:?}, \
1457
1438
kind: {:?}, \
1458
1439
make_ret_async: {:?})",
1459
- decl, in_band_ty_params , kind, make_ret_async,
1440
+ decl, fn_node_id , kind, make_ret_async,
1460
1441
) ;
1461
1442
1462
1443
let c_variadic = decl. c_variadic ( ) ;
@@ -1469,10 +1450,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1469
1450
inputs = & inputs[ ..inputs. len ( ) - 1 ] ;
1470
1451
}
1471
1452
let inputs = self . arena . alloc_from_iter ( inputs. iter ( ) . map ( |param| {
1472
- if let Some ( ( _ , ibty , ibpb ) ) = & mut in_band_ty_params {
1453
+ if fn_node_id . is_some ( ) {
1473
1454
self . lower_ty_direct (
1474
1455
& param. ty ,
1475
- ImplTraitContext :: Universal ( ibty , ibpb , self . current_hir_id_owner ) ,
1456
+ ImplTraitContext :: Universal ( self . current_hir_id_owner ) ,
1476
1457
)
1477
1458
} else {
1478
1459
self . lower_ty_direct (
@@ -1494,15 +1475,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1494
1475
let output = if let Some ( ret_id) = make_ret_async {
1495
1476
self . lower_async_fn_ret_ty (
1496
1477
& decl. output ,
1497
- in_band_ty_params . expect ( "`make_ret_async` but no `fn_def_id`" ) . 0 ,
1478
+ fn_node_id . expect ( "`make_ret_async` but no `fn_def_id`" ) ,
1498
1479
ret_id,
1499
1480
)
1500
1481
} else {
1501
1482
match decl. output {
1502
1483
FnRetTy :: Ty ( ref ty) => {
1503
- let context = match in_band_ty_params {
1504
- Some ( ( node_id , _ , _ ) ) if kind. impl_trait_return_allowed ( ) => {
1505
- let fn_def_id = self . resolver . local_def_id ( node_id ) ;
1484
+ let context = match fn_node_id {
1485
+ Some ( fn_node_id ) if kind. impl_trait_return_allowed ( ) => {
1486
+ let fn_def_id = self . resolver . local_def_id ( fn_node_id ) ;
1506
1487
ImplTraitContext :: ReturnPositionOpaqueTy {
1507
1488
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1508
1489
}
@@ -1788,7 +1769,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1788
1769
fn lower_param_bound (
1789
1770
& mut self ,
1790
1771
tpb : & GenericBound ,
1791
- itctx : ImplTraitContext < ' _ , ' hir > ,
1772
+ itctx : ImplTraitContext ,
1792
1773
) -> hir:: GenericBound < ' hir > {
1793
1774
match tpb {
1794
1775
GenericBound :: Trait ( p, modifier) => hir:: GenericBound :: Trait (
@@ -1966,11 +1947,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1966
1947
}
1967
1948
}
1968
1949
1969
- fn lower_trait_ref (
1970
- & mut self ,
1971
- p : & TraitRef ,
1972
- itctx : ImplTraitContext < ' _ , ' hir > ,
1973
- ) -> hir:: TraitRef < ' hir > {
1950
+ fn lower_trait_ref ( & mut self , p : & TraitRef , itctx : ImplTraitContext ) -> hir:: TraitRef < ' hir > {
1974
1951
let path = match self . lower_qpath ( p. ref_id , & None , & p. path , ParamMode :: Explicit , itctx) {
1975
1952
hir:: QPath :: Resolved ( None , path) => path,
1976
1953
qpath => panic ! ( "lower_trait_ref: unexpected QPath `{:?}`" , qpath) ,
@@ -1982,7 +1959,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1982
1959
fn lower_poly_trait_ref (
1983
1960
& mut self ,
1984
1961
p : & PolyTraitRef ,
1985
- mut itctx : ImplTraitContext < ' _ , ' hir > ,
1962
+ mut itctx : ImplTraitContext ,
1986
1963
) -> hir:: PolyTraitRef < ' hir > {
1987
1964
let bound_generic_params = self . lower_generic_params ( & p. bound_generic_params ) ;
1988
1965
@@ -1993,22 +1970,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1993
1970
hir:: PolyTraitRef { bound_generic_params, trait_ref, span : self . lower_span ( p. span ) }
1994
1971
}
1995
1972
1996
- fn lower_mt ( & mut self , mt : & MutTy , itctx : ImplTraitContext < ' _ , ' hir > ) -> hir:: MutTy < ' hir > {
1973
+ fn lower_mt ( & mut self , mt : & MutTy , itctx : ImplTraitContext ) -> hir:: MutTy < ' hir > {
1997
1974
hir:: MutTy { ty : self . lower_ty ( & mt. ty , itctx) , mutbl : mt. mutbl }
1998
1975
}
1999
1976
2000
1977
fn lower_param_bounds (
2001
1978
& mut self ,
2002
1979
bounds : & [ GenericBound ] ,
2003
- itctx : ImplTraitContext < ' _ , ' hir > ,
1980
+ itctx : ImplTraitContext ,
2004
1981
) -> hir:: GenericBounds < ' hir > {
2005
1982
self . arena . alloc_from_iter ( self . lower_param_bounds_mut ( bounds, itctx) )
2006
1983
}
2007
1984
2008
1985
fn lower_param_bounds_mut < ' s > (
2009
1986
& ' s mut self ,
2010
1987
bounds : & ' s [ GenericBound ] ,
2011
- mut itctx : ImplTraitContext < ' s , ' hir > ,
1988
+ mut itctx : ImplTraitContext ,
2012
1989
) -> impl Iterator < Item = hir:: GenericBound < ' hir > > + Captures < ' s > + Captures < ' a > {
2013
1990
bounds. iter ( ) . map ( move |bound| self . lower_param_bound ( bound, itctx. reborrow ( ) ) )
2014
1991
}
0 commit comments