-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
rustc_borrowck: fix async closure error note to report AsyncFn rather than Fn #148713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,9 +111,12 @@ impl RegionName { | |
| | RegionNameSource::NamedEarlyParamRegion(span) => { | ||
| diag.span_label(*span, format!("lifetime `{self}` defined here")); | ||
| } | ||
| RegionNameSource::SynthesizedFreeEnvRegion(span, note) => { | ||
| RegionNameSource::SynthesizedFreeEnvRegion(span, closure_trait) => { | ||
| diag.span_label(*span, format!("lifetime `{self}` represents this closure's body")); | ||
| diag.note(*note); | ||
| diag.note(format!( | ||
| "closure implements `{closure_trait}`, so references to captured variables \ | ||
| can't escape the closure" | ||
| )); | ||
| } | ||
| RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::CannotMatchHirTy( | ||
| span, | ||
|
|
@@ -326,9 +329,15 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> { | |
| ty::LateParamRegionKind::ClosureEnv => { | ||
| let def_ty = self.regioncx.universal_regions().defining_ty; | ||
|
|
||
| let closure_kind = match def_ty { | ||
| DefiningTy::Closure(_, args) => args.as_closure().kind(), | ||
| DefiningTy::CoroutineClosure(_, args) => args.as_coroutine_closure().kind(), | ||
| let (is_lending_coroutine_closure, closure_kind) = match def_ty { | ||
| DefiningTy::Closure(_, args) => (false, args.as_closure().kind()), | ||
| DefiningTy::CoroutineClosure(_, args) => { | ||
| let args = args.as_coroutine_closure(); | ||
| ( | ||
| !args.tupled_upvars_ty().is_ty_var() && args.has_self_borrows(), | ||
| args.kind(), | ||
| ) | ||
| } | ||
| _ => { | ||
| // Can't have BrEnv in functions, constants or coroutines. | ||
| bug!("BrEnv outside of closure."); | ||
|
|
@@ -340,23 +349,19 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> { | |
| bug!("Closure is not defined by a closure expr"); | ||
| }; | ||
| let region_name = self.synthesize_region_name(); | ||
| let note = match closure_kind { | ||
| ty::ClosureKind::Fn => { | ||
| "closure implements `Fn`, so references to captured variables \ | ||
| can't escape the closure" | ||
| } | ||
| ty::ClosureKind::FnMut => { | ||
| "closure implements `FnMut`, so references to captured variables \ | ||
| can't escape the closure" | ||
| } | ||
| ty::ClosureKind::FnOnce => { | ||
| bug!("BrEnv in a `FnOnce` closure"); | ||
| } | ||
| let closure_trait = match (is_lending_coroutine_closure, closure_kind) { | ||
| (false, kind) => kind.as_str(), | ||
| (true, ty::ClosureKind::Fn) => "AsyncFn", | ||
| (true, ty::ClosureKind::FnMut) => "AsyncFnMut", | ||
| (true, ty::ClosureKind::FnOnce) => "AsyncFnOnce", | ||
| }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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());
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did think about this. Correct me if I'm wrong, |
||
|
|
||
| Some(RegionName { | ||
| name: region_name, | ||
| source: RegionNameSource::SynthesizedFreeEnvRegion(fn_decl_span, note), | ||
| source: RegionNameSource::SynthesizedFreeEnvRegion( | ||
| fn_decl_span, | ||
| closure_trait, | ||
| ), | ||
| }) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so the fix here would be to check whether
args.has_self_borrowsand if so, talk aboutAsyncFn(Mut)instead