Skip to content

Commit 097240b

Browse files
estebankjonas-schievink
authored andcommitted
Do not ICE on lifetime error involving closures
1 parent e780933 commit 097240b

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,27 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
951951
err.span_label(
952952
drop_span,
953953
format!(
954-
"...but `{}` will be dropped here, when the function `{}` returns",
954+
"...but `{}` will be dropped here, when the {} returns",
955955
name,
956-
self.infcx.tcx.hir().name(fn_hir_id),
956+
self.infcx
957+
.tcx
958+
.hir()
959+
.opt_name(fn_hir_id)
960+
.map(|name| format!("function `{}`", name))
961+
.unwrap_or_else(|| {
962+
match &self
963+
.infcx
964+
.tcx
965+
.typeck_tables_of(self.mir_def_id)
966+
.node_type(fn_hir_id)
967+
.kind
968+
{
969+
ty::Closure(..) => "enclosing closure",
970+
ty::Generator(..) => "enclosing generator",
971+
kind => bug!("expected closure or generator, found {:?}", kind),
972+
}
973+
.to_string()
974+
})
957975
),
958976
);
959977

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
[0].iter().flat_map(|a| [0].iter().map(|_| &a)); //~ ERROR `a` does not live long enough
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0597]: `a` does not live long enough
2+
--> $DIR/unnamed-closure-doesnt-life-long-enough-issue-67634.rs:2:49
3+
|
4+
LL | [0].iter().flat_map(|a| [0].iter().map(|_| &a));
5+
| - ^- ...but `a` will be dropped here, when the enclosing closure returns
6+
| | |
7+
| | `a` would have to be valid for `'_`...
8+
| has type `&i32`
9+
|
10+
= note: functions cannot return a borrow to data owned within the function's scope, functions can only return borrows to data passed as arguments
11+
= note: to learn more, visit <https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#dangling-references>
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)