Skip to content

Commit c64db00

Browse files
committed
Fallback to general error handling in ICE cases.
1 parent b6dfa8c commit c64db00

10 files changed

+60
-41
lines changed

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

+28-17
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
247247
match category {
248248
ConstraintCategory::AssignmentToUpvar |
249249
ConstraintCategory::CallArgumentToUpvar =>
250-
self.report_closure_error(mir, infcx, fr, outlived_fr, span),
250+
self.report_closure_error(mir, infcx, mir_def_id, fr, outlived_fr, category, span),
251251
_ =>
252252
self.report_general_error(mir, infcx, mir_def_id, fr, outlived_fr, category, span),
253253
}
@@ -257,33 +257,44 @@ impl<'tcx> RegionInferenceContext<'tcx> {
257257
&self,
258258
mir: &Mir<'tcx>,
259259
infcx: &InferCtxt<'_, '_, 'tcx>,
260+
mir_def_id: DefId,
260261
fr: RegionVid,
261262
outlived_fr: RegionVid,
263+
category: &ConstraintCategory,
262264
span: &Span,
263265
) {
266+
let fr_name_and_span = self.get_var_name_and_span_for_region(
267+
infcx.tcx, mir, fr);
268+
let outlived_fr_name_and_span = self.get_var_name_and_span_for_region(
269+
infcx.tcx, mir,outlived_fr);
270+
271+
if fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none() {
272+
return self.report_general_error(mir, infcx, mir_def_id, fr, outlived_fr, category,
273+
span);
274+
}
275+
264276
let diag = &mut infcx.tcx.sess.struct_span_err(
265277
*span, &format!("borrowed data escapes outside of closure"),
266278
);
267279

268-
let (outlived_fr_name, outlived_fr_span) = self.get_var_name_and_span_for_region(
269-
infcx.tcx, mir, outlived_fr);
270-
271-
if let Some(name) = outlived_fr_name {
272-
diag.span_label(
273-
outlived_fr_span,
274-
format!("`{}` is declared here, outside of the closure body", name),
275-
);
280+
if let Some((outlived_fr_name, outlived_fr_span)) = outlived_fr_name_and_span {
281+
if let Some(name) = outlived_fr_name {
282+
diag.span_label(
283+
outlived_fr_span,
284+
format!("`{}` is declared here, outside of the closure body", name),
285+
);
286+
}
276287
}
277288

278-
let (fr_name, fr_span) = self.get_var_name_and_span_for_region(infcx.tcx, mir, fr);
289+
if let Some((fr_name, fr_span)) = fr_name_and_span {
290+
if let Some(name) = fr_name {
291+
diag.span_label(
292+
fr_span,
293+
format!("`{}` is a reference that is only valid in the closure body", name),
294+
);
279295

280-
if let Some(name) = fr_name {
281-
diag.span_label(
282-
fr_span,
283-
format!("`{}` is a reference that is only valid in the closure body", name),
284-
);
285-
286-
diag.span_label(*span, format!("`{}` escapes the closure body here", name));
296+
diag.span_label(*span, format!("`{}` escapes the closure body here", name));
297+
}
287298
}
288299

289300
diag.emit();

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/var_name.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
2222
tcx: TyCtxt<'_, '_, 'tcx>,
2323
mir: &Mir<'tcx>,
2424
fr: RegionVid,
25-
) -> (Option<Symbol>, Span) {
25+
) -> Option<(Option<Symbol>, Span)> {
2626
debug!("get_var_name_and_span_for_region(fr={:?})", fr);
2727
assert!(self.universal_regions.is_universal_region(fr));
2828

@@ -37,7 +37,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
3737
self.get_argument_index_for_region(tcx, fr)
3838
.map(|index| self.get_argument_name_and_span_for_region(mir, index))
3939
})
40-
.unwrap_or_else(|| span_bug!(mir.span, "can't find var name for free region {:?}", fr))
4140
}
4241

4342
/// Search the upvars (if any) to find one that references fr. Return its index.

src/test/ui/borrowck/issue-7573.nll.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ warning: not reporting region error due to nll
44
LL | let mut lines_to_use: Vec<&CrateId> = Vec::new();
55
| ^
66

7-
error: unsatisfied lifetime constraints
7+
error: borrowed data escapes outside of closure
88
--> $DIR/issue-7573.rs:32:9
99
|
1010
LL | let mut lines_to_use: Vec<&CrateId> = Vec::new();
11-
| ---------------- lifetime `'2` appears in the type of `lines_to_use`
11+
| ---------------- `lines_to_use` is declared here, outside of the closure body
1212
LL | //~^ NOTE cannot infer an appropriate lifetime
1313
LL | let push_id = |installed_id: &CrateId| {
14-
| - let's call the lifetime of this reference `'1`
14+
| ------------ `installed_id` is a reference that is only valid in the closure body
1515
...
1616
LL | lines_to_use.push(installed_id);
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `installed_id` escapes the closure body here
1818

1919
error: aborting due to previous error
2020

src/test/ui/in-band-lifetimes/impl/dyn-trait.nll.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ warning: not reporting region error due to nll
44
LL | static_val(x); //~ ERROR cannot infer
55
| ^
66

7-
error: unsatisfied lifetime constraints
7+
error: borrowed data escapes outside of closure
88
--> $DIR/dyn-trait.rs:32:5
99
|
10+
LL | fn with_dyn_debug_static<'a>(x: Box<dyn Debug + 'a>) {
11+
| - `x` is a reference that is only valid in the closure body
1012
LL | static_val(x); //~ ERROR cannot infer
11-
| ^^^^^^^^^^^^^ argument requires that `'a` must outlive `'static`
13+
| ^^^^^^^^^^^^^ `x` escapes the closure body here
1214

1315
error: aborting due to previous error
1416

src/test/ui/issue-16683.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ warning: not reporting region error due to nll
1010
LL | self.a(); //~ ERROR cannot infer
1111
| ^
1212

13-
error: unsatisfied lifetime constraints
13+
error: borrowed data escapes outside of closure
1414
--> $DIR/issue-16683.rs:14:9
1515
|
1616
LL | fn b(&self) {
17-
| - let's call the lifetime of this reference `'1`
17+
| ----- `self` is a reference that is only valid in the closure body
1818
LL | self.a(); //~ ERROR cannot infer
19-
| ^^^^^^^^ argument requires that `'1` must outlive `'a`
19+
| ^^^^^^^^ `self` escapes the closure body here
2020

2121
error: aborting due to previous error
2222

src/test/ui/issue-17758.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ warning: not reporting region error due to nll
1010
LL | self.foo();
1111
| ^^^
1212

13-
error: unsatisfied lifetime constraints
13+
error: borrowed data escapes outside of closure
1414
--> $DIR/issue-17758.rs:17:9
1515
|
1616
LL | fn bar(&self) {
17-
| - let's call the lifetime of this reference `'1`
17+
| ----- `self` is a reference that is only valid in the closure body
1818
LL | self.foo();
19-
| ^^^^^^^^^^ argument requires that `'1` must outlive `'a`
19+
| ^^^^^^^^^^ `self` escapes the closure body here
2020

2121
error: aborting due to previous error
2222

src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-comparing-against-free.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ warning: not reporting region error due to nll
44
LL | foo(cell, |cell_a, cell_x| {
55
| ^^^
66

7-
error: unsatisfied lifetime constraints
7+
error: borrowed data escapes outside of closure
88
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:33:20
99
|
1010
LL | foo(cell, |cell_a, cell_x| {
11-
| ------ ------ lifetime `'1` appears in this argument
11+
| ------ ------ `cell_x` is a reference that is only valid in the closure body
1212
| |
13-
| lifetime `'2` appears in this argument
13+
| `cell_a` is declared here, outside of the closure body
1414
LL | //~^ WARNING not reporting region error due to nll
1515
LL | cell_a.set(cell_x.get()); // forces 'x: 'a, error in closure
16-
| ^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`
16+
| ^^^^^^^^^^^^ `cell_x` escapes the closure body here
1717

1818
note: No external requirements
1919
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:31:15

src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-no-bound.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ LL | | });
2323
= note: number of external vids: 2
2424
= note: where '_#1r: '_#0r
2525

26-
error: unsatisfied lifetime constraints
26+
error: borrowed data escapes outside of closure
2727
--> $DIR/propagate-approximated-shorter-to-static-no-bound.rs:45:5
2828
|
29+
LL | fn supply<'a, 'b>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>) {
30+
| ------ `cell_a` is a reference that is only valid in the closure body
2931
LL | / establish_relationships(&cell_a, &cell_b, |_outlives, x, y| {
3032
LL | | //~^ ERROR
3133
LL | |
3234
LL | | // Only works if 'x: 'y:
3335
LL | | demand_y(x, y, x.get()) //~ WARNING not reporting region error due to nll
3436
LL | | });
35-
| |______^ argument requires that `'a` must outlive `'static`
37+
| |______^ `cell_a` escapes the closure body here
3638

3739
note: No external requirements
3840
--> $DIR/propagate-approximated-shorter-to-static-no-bound.rs:44:1

src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-wrong-bound.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ LL | | });
2323
= note: number of external vids: 3
2424
= note: where '_#1r: '_#0r
2525

26-
error: unsatisfied lifetime constraints
26+
error: borrowed data escapes outside of closure
2727
--> $DIR/propagate-approximated-shorter-to-static-wrong-bound.rs:48:5
2828
|
29+
LL | fn supply<'a, 'b>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>) {
30+
| ------ `cell_a` is a reference that is only valid in the closure body
2931
LL | / establish_relationships(&cell_a, &cell_b, |_outlives1, _outlives2, x, y| {
3032
LL | | //~^ ERROR
3133
LL | | // Only works if 'x: 'y:
3234
LL | | demand_y(x, y, x.get())
3335
LL | | //~^ WARNING not reporting region error due to nll
3436
LL | | });
35-
| |______^ argument requires that `'a` must outlive `'static`
37+
| |______^ `cell_a` escapes the closure body here
3638

3739
note: No external requirements
3840
--> $DIR/propagate-approximated-shorter-to-static-wrong-bound.rs:47:1

src/test/ui/nll/issue-50716.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error: unsatisfied lifetime constraints
1+
error: borrowed data escapes outside of closure
22
--> $DIR/issue-50716.rs:25:14
33
|
4+
LL | fn foo<'a, T: 'static>(s: Box<<&'a T as A>::X>)
5+
| - `s` is a reference that is only valid in the closure body
6+
...
47
LL | let _x = *s; //~ ERROR
5-
| ^^ assignment requires that `'a` must outlive `'static`
8+
| ^^ `s` escapes the closure body here
69

710
error: aborting due to previous error
811

0 commit comments

Comments
 (0)