@@ -269,29 +269,11 @@ enum ImplTraitContext<'b, 'a> {
269
269
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
270
270
///
271
271
ReturnPositionOpaqueTy {
272
- /// `DefId` for the parent function, used to look up necessary
273
- /// information later.
274
- fn_def_id : LocalDefId ,
275
272
/// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
276
273
origin : hir:: OpaqueTyOrigin ,
277
274
} ,
278
275
/// Impl trait in type aliases.
279
- TypeAliasesOpaqueTy {
280
- /// Set of lifetimes that this opaque type can capture, if it uses
281
- /// them. This includes lifetimes bound since we entered this context.
282
- /// For example:
283
- ///
284
- /// ```
285
- /// type A<'b> = impl for<'a> Trait<'a, Out = impl Sized + 'a>;
286
- /// ```
287
- ///
288
- /// Here the inner opaque type captures `'a` because it uses it. It doesn't
289
- /// need to capture `'b` because it already inherits the lifetime
290
- /// parameter from `A`.
291
- // FIXME(impl_trait): but `required_region_bounds` will ICE later
292
- // anyway.
293
- capturable_lifetimes : & ' b mut FxHashSet < hir:: ParamName > ,
294
- } ,
276
+ TypeAliasesOpaqueTy ,
295
277
/// `impl Trait` is not accepted in this position.
296
278
Disallowed ( ImplTraitPosition ) ,
297
279
}
@@ -325,12 +307,8 @@ impl<'a> ImplTraitContext<'_, 'a> {
325
307
use self :: ImplTraitContext :: * ;
326
308
match self {
327
309
Universal ( params, parent) => Universal ( params, * parent) ,
328
- ReturnPositionOpaqueTy { fn_def_id, origin } => {
329
- ReturnPositionOpaqueTy { fn_def_id : * fn_def_id, origin : * origin }
330
- }
331
- TypeAliasesOpaqueTy { capturable_lifetimes } => {
332
- TypeAliasesOpaqueTy { capturable_lifetimes }
333
- }
310
+ ReturnPositionOpaqueTy { origin } => ReturnPositionOpaqueTy { origin : * origin } ,
311
+ TypeAliasesOpaqueTy => TypeAliasesOpaqueTy ,
334
312
Disallowed ( pos) => Disallowed ( * pos) ,
335
313
}
336
314
}
@@ -1011,7 +989,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1011
989
hir:: TypeBindingKind :: Equality { term }
1012
990
}
1013
991
AssocConstraintKind :: Bound { ref bounds } => {
1014
- let mut capturable_lifetimes;
1015
992
let mut parent_def_id = self . current_hir_id_owner ;
1016
993
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
1017
994
let ( desugar_to_impl_trait, itctx) = match itctx {
@@ -1044,13 +1021,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1044
1021
//
1045
1022
// FIXME: this is only needed until `impl Trait` is allowed in type aliases.
1046
1023
ImplTraitContext :: Disallowed ( _) if self . is_in_dyn_type => {
1047
- capturable_lifetimes = FxHashSet :: default ( ) ;
1048
- (
1049
- true ,
1050
- ImplTraitContext :: TypeAliasesOpaqueTy {
1051
- capturable_lifetimes : & mut capturable_lifetimes,
1052
- } ,
1053
- )
1024
+ ( true , ImplTraitContext :: TypeAliasesOpaqueTy )
1054
1025
}
1055
1026
1056
1027
// We are in the parameter position, but not within a dyn type:
@@ -1309,28 +1280,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1309
1280
TyKind :: ImplTrait ( def_node_id, ref bounds) => {
1310
1281
let span = t. span ;
1311
1282
match itctx {
1312
- ImplTraitContext :: ReturnPositionOpaqueTy { fn_def_id, origin } => self
1313
- . lower_opaque_impl_trait (
1314
- span,
1315
- Some ( fn_def_id) ,
1316
- origin,
1317
- def_node_id,
1318
- None ,
1319
- |this| this. lower_param_bounds ( bounds, itctx) ,
1320
- ) ,
1321
- ImplTraitContext :: TypeAliasesOpaqueTy { ref capturable_lifetimes } => {
1322
- // Reset capturable lifetimes, any nested impl trait
1323
- // types will inherit lifetimes from this opaque type,
1324
- // so don't need to capture them again.
1325
- let nested_itctx = ImplTraitContext :: TypeAliasesOpaqueTy {
1326
- capturable_lifetimes : & mut FxHashSet :: default ( ) ,
1327
- } ;
1283
+ ImplTraitContext :: ReturnPositionOpaqueTy { origin } => self
1284
+ . lower_opaque_impl_trait ( span, origin, def_node_id, |this| {
1285
+ this. lower_param_bounds ( bounds, itctx)
1286
+ } ) ,
1287
+ ImplTraitContext :: TypeAliasesOpaqueTy => {
1288
+ let nested_itctx = ImplTraitContext :: TypeAliasesOpaqueTy ;
1328
1289
self . lower_opaque_impl_trait (
1329
1290
span,
1330
- None ,
1331
1291
hir:: OpaqueTyOrigin :: TyAlias ,
1332
1292
def_node_id,
1333
- Some ( capturable_lifetimes) ,
1334
1293
|this| this. lower_param_bounds ( bounds, nested_itctx) ,
1335
1294
)
1336
1295
}
@@ -1392,10 +1351,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1392
1351
fn lower_opaque_impl_trait (
1393
1352
& mut self ,
1394
1353
span : Span ,
1395
- fn_def_id : Option < LocalDefId > ,
1396
1354
origin : hir:: OpaqueTyOrigin ,
1397
1355
opaque_ty_node_id : NodeId ,
1398
- capturable_lifetimes : Option < & FxHashSet < hir:: ParamName > > ,
1399
1356
lower_bounds : impl FnOnce ( & mut Self ) -> hir:: GenericBounds < ' hir > ,
1400
1357
) -> hir:: TyKind < ' hir > {
1401
1358
// Make sure we know that some funky desugaring has been going on here.
@@ -1409,19 +1366,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1409
1366
1410
1367
let mut collected_lifetimes = FxHashMap :: default ( ) ;
1411
1368
self . with_hir_id_owner ( opaque_ty_node_id, |lctx| {
1412
- let lifetime_stash = std:: mem:: replace (
1413
- & mut lctx. captured_lifetimes ,
1414
- Some ( ( opaque_ty_def_id, FxHashMap :: default ( ) , FxHashSet :: default ( ) ) ) ,
1415
- ) ;
1369
+ let capture_framework = if origin == hir:: OpaqueTyOrigin :: TyAlias {
1370
+ None
1371
+ } else {
1372
+ Some ( ( opaque_ty_def_id, FxHashMap :: default ( ) , FxHashSet :: default ( ) ) )
1373
+ } ;
1374
+ let lifetime_stash = std:: mem:: replace ( & mut lctx. captured_lifetimes , capture_framework) ;
1416
1375
let hir_bounds = lower_bounds ( lctx) ;
1417
- collected_lifetimes =
1418
- std:: mem:: replace ( & mut lctx. captured_lifetimes , lifetime_stash) . unwrap ( ) . 1 ;
1419
-
1420
- if let Some ( capturable_lifetimes) = capturable_lifetimes {
1421
- collected_lifetimes. retain ( |_, ( _, _, p_name, _) | {
1422
- capturable_lifetimes. contains ( & p_name. normalize_to_macros_2_0 ( ) )
1423
- } ) ;
1424
- }
1376
+ collected_lifetimes = std:: mem:: replace ( & mut lctx. captured_lifetimes , lifetime_stash)
1377
+ . map_or_else ( FxHashMap :: default, |c| c. 1 ) ;
1425
1378
debug ! ( ?collected_lifetimes) ;
1426
1379
1427
1380
let lifetime_defs = lctx. arena . alloc_from_iter ( collected_lifetimes. iter ( ) . map (
@@ -1586,7 +1539,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1586
1539
Some ( ( node_id, _) ) if kind. impl_trait_return_allowed ( ) => {
1587
1540
let fn_def_id = self . resolver . local_def_id ( node_id) ;
1588
1541
ImplTraitContext :: ReturnPositionOpaqueTy {
1589
- fn_def_id,
1590
1542
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1591
1543
}
1592
1544
}
@@ -1867,7 +1819,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1867
1819
// `impl Future` opaque type that `async fn` implicitly
1868
1820
// generates.
1869
1821
let context = ImplTraitContext :: ReturnPositionOpaqueTy {
1870
- fn_def_id,
1871
1822
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1872
1823
} ;
1873
1824
self . lower_ty ( ty, context)
@@ -2118,28 +2069,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2118
2069
self . lower_generic_params ( & p. bound_generic_params , itctx. reborrow ( ) ) ;
2119
2070
2120
2071
let trait_ref = self . with_in_scope_lifetime_defs ( & p. bound_generic_params , |this| {
2121
- // Any impl Trait types defined within this scope can capture
2122
- // lifetimes bound on this predicate.
2123
- let lt_def_names = p. bound_generic_params . iter ( ) . filter_map ( |param| match param. kind {
2124
- GenericParamKind :: Lifetime { .. } => {
2125
- Some ( ParamName :: Plain ( param. ident . normalize_to_macros_2_0 ( ) ) )
2126
- }
2127
- _ => None ,
2128
- } ) ;
2129
- if let ImplTraitContext :: TypeAliasesOpaqueTy { ref mut capturable_lifetimes } = itctx {
2130
- capturable_lifetimes. extend ( lt_def_names. clone ( ) ) ;
2131
- }
2132
2072
if let Some ( ( _, _, binders) ) = & mut this. captured_lifetimes {
2133
2073
binders. insert ( p. trait_ref . ref_id ) ;
2134
2074
}
2135
2075
2136
2076
let trait_ref = this. lower_trait_ref ( & p. trait_ref , itctx. reborrow ( ) ) ;
2137
2077
2138
- if let ImplTraitContext :: TypeAliasesOpaqueTy { ref mut capturable_lifetimes } = itctx {
2139
- for param in lt_def_names {
2140
- capturable_lifetimes. remove ( & param) ;
2141
- }
2142
- }
2143
2078
if let Some ( ( _, _, binders) ) = & mut this. captured_lifetimes {
2144
2079
binders. remove ( & p. trait_ref . ref_id ) ;
2145
2080
}
0 commit comments