-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #125259 - compiler-errors:fn-mut-as-a-treat, r=oli-obk
An async closure may implement `FnMut`/`Fn` if it has no self-borrows There's no reason that async closures may not implement `FnMut` or `Fn` if they don't actually borrow anything with the closure's env lifetime. Specifically, #123660 made it so that we don't always need to borrow captures from the closure's env. See the doc comment on `should_reborrow_from_env_of_parent_coroutine_closure`: https://github.com/rust-lang/rust/blob/c00957a3e269219413041a4e3565f33b1f9d0779/compiler/rustc_hir_typeck/src/upvar.rs#L1777-L1823 If there are no such borrows, then we are free to implement `FnMut` and `Fn` as permitted by our closure's inferred `ClosureKind`. As far as I can tell, this change makes `async || {}` work in precisely the set of places they used to work before #120361. Fixes #125247. r? oli-obk
- Loading branch information
Showing
4 changed files
with
70 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//@ check-pass | ||
//@ edition: 2021 | ||
|
||
// Demonstrates that an async closure may implement `FnMut` (not just `async FnMut`!) | ||
// if it has no self-borrows. In this case, `&Ty` is not borrowed from the closure env, | ||
// since it's fine to reborrow it with its original lifetime. See the doc comment on | ||
// `should_reborrow_from_env_of_parent_coroutine_closure` for more detail for when we | ||
// must borrow from the closure env. | ||
|
||
#![feature(async_closure)] | ||
|
||
fn main() {} | ||
|
||
fn needs_fn_mut<T>(x: impl FnMut() -> T) {} | ||
|
||
fn hello(x: &Ty) { | ||
needs_fn_mut(async || { x.hello(); }); | ||
} | ||
|
||
struct Ty; | ||
impl Ty { | ||
fn hello(&self) {} | ||
} |