@@ -632,8 +632,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
632
632
// and ban them. Type variables instantiated inside binders aren't
633
633
// well-supported at the moment, so this doesn't work.
634
634
// In the future, this should be fixed and this error should be removed.
635
- let def = self . map . defs . get ( & lifetime. id ) ;
636
- if let Some ( & Region :: LateBound ( _, def_id, _) ) = def {
635
+ let def = self . map . defs . get ( & lifetime. id ) . cloned ( ) ;
636
+ if let Some ( Region :: LateBound ( _, def_id, _) ) = def {
637
637
if let Some ( node_id) = self . tcx . hir . as_local_node_id ( def_id) {
638
638
// Ensure that the parent of the def is an item, not HRTB
639
639
let parent_id = self . tcx . hir . get_parent_node ( node_id) ;
@@ -651,6 +651,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
651
651
"`impl Trait` can only capture lifetimes \
652
652
bound at the fn or impl level"
653
653
) ;
654
+ self . uninsert_lifetime_on_error ( lifetime, def. unwrap ( ) ) ;
654
655
}
655
656
}
656
657
}
@@ -2377,6 +2378,14 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2377
2378
}
2378
2379
}
2379
2380
}
2381
+
2382
+ /// Sometimes we resolve a lifetime, but later find that it is an
2383
+ /// error (esp. around impl trait). In that case, we remove the
2384
+ /// entry into `map.defs` so as not to confuse later code.
2385
+ fn uninsert_lifetime_on_error ( & mut self , lifetime_ref : & ' tcx hir:: Lifetime , bad_def : Region ) {
2386
+ let old_value = self . map . defs . remove ( & lifetime_ref. id ) ;
2387
+ assert_eq ! ( old_value, Some ( bad_def) ) ;
2388
+ }
2380
2389
}
2381
2390
2382
2391
///////////////////////////////////////////////////////////////////////////
0 commit comments