@@ -158,13 +158,14 @@ enum Scope<'a> {
158
158
s : ScopeRef < ' a > ,
159
159
} ,
160
160
161
- /// Disallows capturing non-lifetime binders from parent scopes.
161
+ /// Disallows capturing late-bound vars from parent scopes.
162
162
///
163
163
/// This is necessary for something like `for<T> [(); { /* references T */ }]:`,
164
164
/// since we don't do something more correct like replacing any captured
165
165
/// late-bound vars with early-bound params in the const's own generics.
166
- AnonConstBoundary {
166
+ LateBoundary {
167
167
s : ScopeRef < ' a > ,
168
+ what : & ' static str ,
168
169
} ,
169
170
170
171
Root {
@@ -216,7 +217,9 @@ impl<'a> fmt::Debug for TruncatedScopeDebug<'a> {
216
217
. field ( "s" , & ".." )
217
218
. finish ( ) ,
218
219
Scope :: TraitRefBoundary { s : _ } => f. debug_struct ( "TraitRefBoundary" ) . finish ( ) ,
219
- Scope :: AnonConstBoundary { s : _ } => f. debug_struct ( "AnonConstBoundary" ) . finish ( ) ,
220
+ Scope :: LateBoundary { s : _, what } => {
221
+ f. debug_struct ( "LateBoundary" ) . field ( "what" , what) . finish ( )
222
+ }
220
223
Scope :: Root { opt_parent_item } => {
221
224
f. debug_struct ( "Root" ) . field ( "opt_parent_item" , & opt_parent_item) . finish ( )
222
225
}
@@ -318,7 +321,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
318
321
break ( vec ! [ ] , BinderScopeType :: Normal ) ;
319
322
}
320
323
321
- Scope :: ObjectLifetimeDefault { s, .. } | Scope :: AnonConstBoundary { s } => {
324
+ Scope :: ObjectLifetimeDefault { s, .. } | Scope :: LateBoundary { s, .. } => {
322
325
scope = s;
323
326
}
324
327
@@ -697,9 +700,12 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
697
700
} ) => {
698
701
intravisit:: walk_ty ( self , ty) ;
699
702
700
- // Elided lifetimes are not allowed in non-return
701
- // position impl Trait
702
- let scope = Scope :: TraitRefBoundary { s : self . scope } ;
703
+ // Elided lifetimes and late-bound lifetimes (from the parent)
704
+ // are not allowed in non-return position impl Trait
705
+ let scope = Scope :: LateBoundary {
706
+ s : & Scope :: TraitRefBoundary { s : self . scope } ,
707
+ what : "type alias impl trait" ,
708
+ } ;
703
709
self . with ( scope, |this| intravisit:: walk_item ( this, opaque_ty) ) ;
704
710
705
711
return ;
@@ -979,7 +985,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
979
985
}
980
986
981
987
fn visit_anon_const ( & mut self , c : & ' tcx hir:: AnonConst ) {
982
- self . with ( Scope :: AnonConstBoundary { s : self . scope } , |this| {
988
+ self . with ( Scope :: LateBoundary { s : self . scope , what : "constant" } , |this| {
983
989
intravisit:: walk_anon_const ( this, c) ;
984
990
} ) ;
985
991
}
@@ -1174,6 +1180,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1174
1180
let mut late_depth = 0 ;
1175
1181
let mut scope = self . scope ;
1176
1182
let mut outermost_body = None ;
1183
+ let mut crossed_late_boundary = None ;
1177
1184
let result = loop {
1178
1185
match * scope {
1179
1186
Scope :: Body { id, s } => {
@@ -1258,8 +1265,12 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1258
1265
1259
1266
Scope :: ObjectLifetimeDefault { s, .. }
1260
1267
| Scope :: Supertrait { s, .. }
1261
- | Scope :: TraitRefBoundary { s, .. }
1262
- | Scope :: AnonConstBoundary { s } => {
1268
+ | Scope :: TraitRefBoundary { s, .. } => {
1269
+ scope = s;
1270
+ }
1271
+
1272
+ Scope :: LateBoundary { s, what } => {
1273
+ crossed_late_boundary = Some ( what) ;
1263
1274
scope = s;
1264
1275
}
1265
1276
}
@@ -1268,6 +1279,22 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1268
1279
if let Some ( mut def) = result {
1269
1280
if let ResolvedArg :: EarlyBound ( ..) = def {
1270
1281
// Do not free early-bound regions, only late-bound ones.
1282
+ } else if let ResolvedArg :: LateBound ( _, _, param_def_id) = def
1283
+ && let Some ( what) = crossed_late_boundary
1284
+ {
1285
+ let use_span = lifetime_ref. ident . span ;
1286
+ let def_span = self . tcx . def_span ( param_def_id) ;
1287
+ let guar = match self . tcx . def_kind ( param_def_id) {
1288
+ DefKind :: LifetimeParam => {
1289
+ self . tcx . sess . emit_err ( errors:: CannotCaptureLateBound :: Lifetime {
1290
+ use_span,
1291
+ def_span,
1292
+ what,
1293
+ } )
1294
+ }
1295
+ _ => unreachable ! ( ) ,
1296
+ } ;
1297
+ def = ResolvedArg :: Error ( guar) ;
1271
1298
} else if let Some ( body_id) = outermost_body {
1272
1299
let fn_id = self . tcx . hir ( ) . body_owner ( body_id) ;
1273
1300
match self . tcx . hir ( ) . get ( fn_id) {
@@ -1322,7 +1349,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1322
1349
| Scope :: ObjectLifetimeDefault { s, .. }
1323
1350
| Scope :: Supertrait { s, .. }
1324
1351
| Scope :: TraitRefBoundary { s, .. }
1325
- | Scope :: AnonConstBoundary { s } => {
1352
+ | Scope :: LateBoundary { s, .. } => {
1326
1353
scope = s;
1327
1354
}
1328
1355
}
@@ -1341,7 +1368,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1341
1368
// search.
1342
1369
let mut late_depth = 0 ;
1343
1370
let mut scope = self . scope ;
1344
- let mut crossed_anon_const = false ;
1371
+ let mut crossed_late_boundary = None ;
1345
1372
1346
1373
let result = loop {
1347
1374
match * scope {
@@ -1376,28 +1403,32 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1376
1403
scope = s;
1377
1404
}
1378
1405
1379
- Scope :: AnonConstBoundary { s } => {
1380
- crossed_anon_const = true ;
1406
+ Scope :: LateBoundary { s, what } => {
1407
+ crossed_late_boundary = Some ( what ) ;
1381
1408
scope = s;
1382
1409
}
1383
1410
}
1384
1411
} ;
1385
1412
1386
1413
if let Some ( def) = result {
1387
- if let ResolvedArg :: LateBound ( ..) = def && crossed_anon_const {
1414
+ if let ResolvedArg :: LateBound ( ..) = def
1415
+ && let Some ( what) = crossed_late_boundary
1416
+ {
1388
1417
let use_span = self . tcx . hir ( ) . span ( hir_id) ;
1389
1418
let def_span = self . tcx . def_span ( param_def_id) ;
1390
1419
let guar = match self . tcx . def_kind ( param_def_id) {
1391
1420
DefKind :: ConstParam => {
1392
- self . tcx . sess . emit_err ( errors:: CannotCaptureLateBoundInAnonConst :: Const {
1421
+ self . tcx . sess . emit_err ( errors:: CannotCaptureLateBound :: Const {
1393
1422
use_span,
1394
1423
def_span,
1424
+ what,
1395
1425
} )
1396
1426
}
1397
1427
DefKind :: TyParam => {
1398
- self . tcx . sess . emit_err ( errors:: CannotCaptureLateBoundInAnonConst :: Type {
1428
+ self . tcx . sess . emit_err ( errors:: CannotCaptureLateBound :: Type {
1399
1429
use_span,
1400
1430
def_span,
1431
+ what,
1401
1432
} )
1402
1433
}
1403
1434
_ => unreachable ! ( ) ,
@@ -1446,7 +1477,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1446
1477
| Scope :: ObjectLifetimeDefault { s, .. }
1447
1478
| Scope :: Supertrait { s, .. }
1448
1479
| Scope :: TraitRefBoundary { s, .. }
1449
- | Scope :: AnonConstBoundary { s } => {
1480
+ | Scope :: LateBoundary { s, .. } => {
1450
1481
scope = s;
1451
1482
}
1452
1483
}
@@ -1526,7 +1557,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1526
1557
| Scope :: ObjectLifetimeDefault { s, .. }
1527
1558
| Scope :: Supertrait { s, .. }
1528
1559
| Scope :: TraitRefBoundary { s, .. }
1529
- | Scope :: AnonConstBoundary { s } => {
1560
+ | Scope :: LateBoundary { s, .. } => {
1530
1561
scope = s;
1531
1562
}
1532
1563
}
@@ -1831,7 +1862,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1831
1862
1832
1863
Scope :: Supertrait { s, .. }
1833
1864
| Scope :: TraitRefBoundary { s, .. }
1834
- | Scope :: AnonConstBoundary { s } => {
1865
+ | Scope :: LateBoundary { s, .. } => {
1835
1866
scope = s;
1836
1867
}
1837
1868
}
0 commit comments