Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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"
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any way this can look like

let note = format!("closure implements `{}`, so references to captured variables can't escape the closure", closure_kind.as_str());

Copy link
Contributor Author

@InvalidPathException InvalidPathException Nov 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did think about this. Correct me if I'm wrong, RegionNameSource stores these as &'static str but format! gives String, so we cannot really get around it (at least in this way). If we do refactor then it might be a lot of diff for something we do not touch for years.


Expand Down
4 changes: 2 additions & 2 deletions tests/ui/async-await/async-closures/not-lending.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Loading