@@ -632,8 +632,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
632632 // and ban them. Type variables instantiated inside binders aren't
633633 // well-supported at the moment, so this doesn't work.
634634 // 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 {
637637 if let Some ( node_id) = self . tcx . hir . as_local_node_id ( def_id) {
638638 // Ensure that the parent of the def is an item, not HRTB
639639 let parent_id = self . tcx . hir . get_parent_node ( node_id) ;
@@ -651,6 +651,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
651651 "`impl Trait` can only capture lifetimes \
652652 bound at the fn or impl level"
653653 ) ;
654+ self . uninsert_lifetime_on_error ( lifetime, def. unwrap ( ) ) ;
654655 }
655656 }
656657 }
@@ -2377,6 +2378,14 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
23772378 }
23782379 }
23792380 }
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+ }
23802389}
23812390
23822391///////////////////////////////////////////////////////////////////////////
0 commit comments