diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index 517f9e88cd9bd..42662411e4f04 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -331,7 +331,12 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> { let closure_kind = match def_ty { DefiningTy::Closure(_, args) => args.as_closure().kind(), - DefiningTy::CoroutineClosure(_, args) => args.as_coroutine_closure().kind(), + DefiningTy::CoroutineClosure(_, args) => { + match args.as_coroutine_closure().kind() { + ty::ClosureKind::Fn => ty::ClosureKind::FnOnce, + kind => kind, + } + } _ => { // Can't have BrEnv in functions, constants or coroutines. bug!("BrEnv outside of closure."); @@ -353,7 +358,8 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> { can't escape the closure" } ty::ClosureKind::FnOnce => { - bug!("BrEnv in a `FnOnce` closure"); + "closure implements `FnOnce`, so references to captured variables \ + can't escape the closure" } }; diff --git a/tests/ui/async-await/async-closures/not-lending.stderr b/tests/ui/async-await/async-closures/not-lending.stderr index f0028129caa02..26c0cab4304c2 100644 --- a/tests/ui/async-await/async-closures/not-lending.stderr +++ b/tests/ui/async-await/async-closures/not-lending.stderr @@ -7,7 +7,7 @@ LL | let x = async move || -> &String { &s }; | | return type of async closure `{async closure body@$DIR/not-lending.rs:12:42: 12:48}` contains a lifetime `'2` | lifetime `'1` represents this closure's body | - = note: closure implements `Fn`, so references to captured variables can't escape the closure + = note: closure implements `FnOnce`, so references to captured variables can't escape the closure error: lifetime may not live long enough --> $DIR/not-lending.rs:16:31 @@ -18,7 +18,7 @@ LL | let x = async move || { &s }; | | return type of async closure `{async closure body@$DIR/not-lending.rs:16:31: 16:37}` contains a lifetime `'2` | lifetime `'1` represents this closure's body | - = note: closure implements `Fn`, so references to captured variables can't escape the closure + = note: closure implements `FnOnce`, so references to captured variables can't escape the closure error: aborting due to 2 previous errors