@@ -1344,12 +1344,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1344
1344
Scope :: Binder {
1345
1345
where_bound_origin : Some ( hir:: PredicateOrigin :: ImplTrait ) , ..
1346
1346
} => {
1347
- let mut err = self . tcx . sess . struct_span_err (
1348
- lifetime_ref. ident . span ,
1349
- "`impl Trait` can only mention lifetimes bound at the fn or impl level" ,
1350
- ) ;
1351
- err. span_note ( self . tcx . def_span ( region_def_id) , "lifetime declared here" ) ;
1352
- err. emit ( ) ;
1347
+ self . tcx . sess . emit_err ( errors:: LateBoundInApit :: Lifetime {
1348
+ span : lifetime_ref. ident . span ,
1349
+ param_span : self . tcx . def_span ( region_def_id) ,
1350
+ } ) ;
1353
1351
return ;
1354
1352
}
1355
1353
Scope :: Root { .. } => break ,
@@ -1379,6 +1377,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1379
1377
let mut late_depth = 0 ;
1380
1378
let mut scope = self . scope ;
1381
1379
let mut crossed_anon_const = false ;
1380
+
1382
1381
let result = loop {
1383
1382
match * scope {
1384
1383
Scope :: Body { s, .. } => {
@@ -1446,6 +1445,50 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1446
1445
return ;
1447
1446
}
1448
1447
1448
+ // We may fail to resolve higher-ranked ty/const vars that are mentioned by APIT.
1449
+ // AST-based resolution does not care for impl-trait desugaring, which are the
1450
+ // responsibility of lowering. This may create a mismatch between the resolution
1451
+ // AST found (`param_def_id`) which points to HRTB, and what HIR allows.
1452
+ // ```
1453
+ // fn foo(x: impl for<T> Trait<Assoc = impl Trait2<T>>) {}
1454
+ // ```
1455
+ //
1456
+ // In such case, walk back the binders to diagnose it properly.
1457
+ let mut scope = self . scope ;
1458
+ loop {
1459
+ match * scope {
1460
+ Scope :: Binder {
1461
+ where_bound_origin : Some ( hir:: PredicateOrigin :: ImplTrait ) , ..
1462
+ } => {
1463
+ let guar = self . tcx . sess . emit_err ( match self . tcx . def_kind ( param_def_id) {
1464
+ DefKind :: TyParam => errors:: LateBoundInApit :: Type {
1465
+ span : self . tcx . hir ( ) . span ( hir_id) ,
1466
+ param_span : self . tcx . def_span ( param_def_id) ,
1467
+ } ,
1468
+ DefKind :: ConstParam => errors:: LateBoundInApit :: Const {
1469
+ span : self . tcx . hir ( ) . span ( hir_id) ,
1470
+ param_span : self . tcx . def_span ( param_def_id) ,
1471
+ } ,
1472
+ kind => {
1473
+ bug ! ( "unexpected def-kind: {}" , kind. descr( param_def_id. to_def_id( ) ) )
1474
+ }
1475
+ } ) ;
1476
+ self . map . defs . insert ( hir_id, ResolvedArg :: Error ( guar) ) ;
1477
+ return ;
1478
+ }
1479
+ Scope :: Root { .. } => break ,
1480
+ Scope :: Binder { s, .. }
1481
+ | Scope :: Body { s, .. }
1482
+ | Scope :: Elision { s, .. }
1483
+ | Scope :: ObjectLifetimeDefault { s, .. }
1484
+ | Scope :: Supertrait { s, .. }
1485
+ | Scope :: TraitRefBoundary { s, .. }
1486
+ | Scope :: AnonConstBoundary { s } => {
1487
+ scope = s;
1488
+ }
1489
+ }
1490
+ }
1491
+
1449
1492
self . tcx . sess . delay_span_bug (
1450
1493
self . tcx . hir ( ) . span ( hir_id) ,
1451
1494
format ! ( "could not resolve {param_def_id:?}" ) ,
0 commit comments