Skip to content

Commit 3928ace

Browse files
authored
Rollup merge of rust-lang#67687 - estebank:issue-67634, r=matthewjasper
Do not ICE on lifetime error involving closures Fix rust-lang#67634.
2 parents f70847a + 5e1b366 commit 3928ace

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
@@ -882,9 +882,27 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
882882
err.span_label(
883883
drop_span,
884884
format!(
885-
"...but `{}` will be dropped here, when the function `{}` returns",
885+
"...but `{}` will be dropped here, when the {} returns",
886886
name,
887-
self.infcx.tcx.hir().name(fn_hir_id),
887+
self.infcx
888+
.tcx
889+
.hir()
890+
.opt_name(fn_hir_id)
891+
.map(|name| format!("function `{}`", name))
892+
.unwrap_or_else(|| {
893+
match &self
894+
.infcx
895+
.tcx
896+
.typeck_tables_of(self.mir_def_id)
897+
.node_type(fn_hir_id)
898+
.kind
899+
{
900+
ty::Closure(..) => "enclosing closure",
901+
ty::Generator(..) => "enclosing generator",
902+
kind => bug!("expected closure or generator, found {:?}", kind),
903+
}
904+
.to_string()
905+
})
888906
),
889907
);
890908

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)