Skip to content

Commit df041f8

Browse files
Fix async closure note to report FnOnce
1 parent 87f9dcd commit df041f8

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,12 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
331331

332332
let closure_kind = match def_ty {
333333
DefiningTy::Closure(_, args) => args.as_closure().kind(),
334-
DefiningTy::CoroutineClosure(_, args) => args.as_coroutine_closure().kind(),
334+
DefiningTy::CoroutineClosure(_, args) => {
335+
match args.as_coroutine_closure().kind() {
336+
ty::ClosureKind::Fn => ty::ClosureKind::FnOnce,
337+
kind => kind,
338+
}
339+
}
335340
_ => {
336341
// Can't have BrEnv in functions, constants or coroutines.
337342
bug!("BrEnv outside of closure.");
@@ -353,7 +358,8 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
353358
can't escape the closure"
354359
}
355360
ty::ClosureKind::FnOnce => {
356-
bug!("BrEnv in a `FnOnce` closure");
361+
"closure implements `FnOnce`, so references to captured variables \
362+
can't escape the closure call"
357363
}
358364
};
359365

tests/ui/async-await/async-closures/not-lending.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let x = async move || -> &String { &s };
77
| | return type of async closure `{async closure body@$DIR/not-lending.rs:12:42: 12:48}` contains a lifetime `'2`
88
| lifetime `'1` represents this closure's body
99
|
10-
= note: closure implements `Fn`, so references to captured variables can't escape the closure
10+
= note: closure implements `FnOnce`, so references to captured variables can't escape the closure call
1111

1212
error: lifetime may not live long enough
1313
--> $DIR/not-lending.rs:16:31
@@ -18,7 +18,7 @@ LL | let x = async move || { &s };
1818
| | return type of async closure `{async closure body@$DIR/not-lending.rs:16:31: 16:37}` contains a lifetime `'2`
1919
| lifetime `'1` represents this closure's body
2020
|
21-
= note: closure implements `Fn`, so references to captured variables can't escape the closure
21+
= note: closure implements `FnOnce`, so references to captured variables can't escape the closure call
2222

2323
error: aborting due to 2 previous errors
2424

0 commit comments

Comments
 (0)