Skip to content

Commit a2a019a

Browse files
committed
do not introduce *false* results from lifetime resolution
1 parent b36917b commit a2a019a

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/librustc/middle/resolve_lifetime.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -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
///////////////////////////////////////////////////////////////////////////

src/test/ui/error-codes/E0657.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn free_fn_capture_hrtb_in_impl_trait()
1919
-> Box<for<'a> Id<impl Lt<'a>>>
2020
//~^ ERROR `impl Trait` can only capture lifetimes bound at the fn or impl level [E0657]
2121
{
22-
()
22+
() //~ ERROR mismatched types
2323
}
2424

2525
struct Foo;
@@ -28,7 +28,7 @@ impl Foo {
2828
-> Box<for<'a> Id<impl Lt<'a>>>
2929
//~^ ERROR `impl Trait` can only capture lifetimes bound at the fn or impl level
3030
{
31-
()
31+
() //~ ERROR mismatched types
3232
}
3333
}
3434

src/test/ui/error-codes/E0657.stderr

+21-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ error[E0657]: `impl Trait` can only capture lifetimes bound at the fn or impl le
1010
LL | -> Box<for<'a> Id<impl Lt<'a>>>
1111
| ^^
1212

13-
error: aborting due to 2 previous errors
13+
error[E0308]: mismatched types
14+
--> $DIR/E0657.rs:22:5
15+
|
16+
LL | () //~ ERROR mismatched types
17+
| ^^ expected struct `std::boxed::Box`, found ()
18+
|
19+
= note: expected type `std::boxed::Box<Id<_> + 'static>`
20+
found type `()`
21+
22+
error[E0308]: mismatched types
23+
--> $DIR/E0657.rs:31:9
24+
|
25+
LL | () //~ ERROR mismatched types
26+
| ^^ expected struct `std::boxed::Box`, found ()
27+
|
28+
= note: expected type `std::boxed::Box<Id<_> + 'static>`
29+
found type `()`
30+
31+
error: aborting due to 4 previous errors
1432

15-
For more information about this error, try `rustc --explain E0657`.
33+
Some errors occurred: E0308, E0657.
34+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)