Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 516ec23

Browse files
authoredMay 1, 2019
Rollup merge of rust-lang#60327 - matthewjasper:handle-local-outlives-lbl, r=nikomatsakis
Search for incompatible universes in borrow errors If we have a borrow that has to live for `'static` we need to check for any regions in incompatible universes when trying to find the cause. closes rust-lang#60274
2 parents 834bd19 + a962274 commit 516ec23

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed
 

‎src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
674674
borrow_region: RegionVid,
675675
outlived_region: RegionVid,
676676
) -> (ConstraintCategory, bool, Span, Option<RegionName>) {
677-
let (category, from_closure, span) =
678-
self.best_blame_constraint(mir, borrow_region, |r| r == outlived_region);
677+
let (category, from_closure, span) = self.best_blame_constraint(
678+
mir,
679+
borrow_region,
680+
|r| self.provides_universal_region(r, borrow_region, outlived_region)
681+
);
679682
let outlived_fr_name =
680683
self.give_region_a_name(infcx, mir, upvars, mir_def_id, outlived_region, &mut 1);
681684
(category, from_closure, span, outlived_fr_name)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Test that we handle the case when a local variable is borrowed for `'static`
2+
// due to an outlives constraint involving a region in an incompatible universe
3+
4+
pub trait Outlives<'this> {}
5+
6+
impl<'this, T> Outlives<'this> for T where T: 'this {}
7+
trait Reference {
8+
type AssociatedType;
9+
}
10+
11+
impl<'a, T: 'a> Reference for &'a T {
12+
type AssociatedType = &'a ();
13+
}
14+
15+
fn assert_static_via_hrtb<G>(_: G) where for<'a> G: Outlives<'a> {}
16+
17+
fn assert_static_via_hrtb_with_assoc_type<T>(_: &'_ T)
18+
where
19+
for<'a> &'a T: Reference<AssociatedType = &'a ()>,
20+
{}
21+
22+
fn main() {
23+
let local = 0;
24+
assert_static_via_hrtb(&local); //~ ERROR `local` does not live long enough
25+
assert_static_via_hrtb_with_assoc_type(&&local); //~ ERROR `local` does not live long enough
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0597]: `local` does not live long enough
2+
--> $DIR/local-outlives-static-via-hrtb.rs:24:28
3+
|
4+
LL | assert_static_via_hrtb(&local);
5+
| -----------------------^^^^^^-
6+
| | |
7+
| | borrowed value does not live long enough
8+
| argument requires that `local` is borrowed for `'static`
9+
LL | assert_static_via_hrtb_with_assoc_type(&&local);
10+
LL | }
11+
| - `local` dropped here while still borrowed
12+
13+
error[E0597]: `local` does not live long enough
14+
--> $DIR/local-outlives-static-via-hrtb.rs:25:45
15+
|
16+
LL | assert_static_via_hrtb_with_assoc_type(&&local);
17+
| ----------------------------------------^^^^^^-
18+
| | |
19+
| | borrowed value does not live long enough
20+
| argument requires that `local` is borrowed for `'static`
21+
LL | }
22+
| - `local` dropped here while still borrowed
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)
Please sign in to comment.