Rollup merge of #67687 - estebank:issue-67634, r=matthewjasper

Do not ICE on lifetime error involving closures

Fix #67634.
This commit is contained in:
Yuki Okushi 2019-12-30 14:07:53 +09:00 committed by GitHub
commit 3928aceb49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View File

@ -882,9 +882,27 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_label(
drop_span,
format!(
"...but `{}` will be dropped here, when the function `{}` returns",
"...but `{}` will be dropped here, when the {} returns",
name,
self.infcx.tcx.hir().name(fn_hir_id),
self.infcx
.tcx
.hir()
.opt_name(fn_hir_id)
.map(|name| format!("function `{}`", name))
.unwrap_or_else(|| {
match &self
.infcx
.tcx
.typeck_tables_of(self.mir_def_id)
.node_type(fn_hir_id)
.kind
{
ty::Closure(..) => "enclosing closure",
ty::Generator(..) => "enclosing generator",
kind => bug!("expected closure or generator, found {:?}", kind),
}
.to_string()
})
),
);

View File

@ -0,0 +1,3 @@
fn main() {
[0].iter().flat_map(|a| [0].iter().map(|_| &a)); //~ ERROR `a` does not live long enough
}

View File

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