feat(rustc_hir_typeck): suggest impl Fn return for capturing closures#159594
Conversation
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @tiif (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-impl-fn-return-for-capturing-closure.rs:43:5 | ||
| | | ||
| LL | fn one_arg_ref(a: i32) -> fn(&i32) -> &i32 { | ||
| | ---------------- expected `for<'a> fn(&'a i32) -> &'a i32` because of return type | ||
| LL | / |x| { | ||
| LL | | | ||
| LL | | println!("{}", a); | ||
| LL | | x | ||
| LL | | } | ||
| | |_____^ expected fn pointer, found closure | ||
| | | ||
| = note: expected fn pointer `for<'a> fn(&'a i32) -> &'a i32` | ||
| found closure `{closure@$DIR/suggest-impl-fn-return-for-capturing-closure.rs:43:5: 43:8}` | ||
| note: closures can only be coerced to `fn` types if they do not capture any variables | ||
| --> $DIR/suggest-impl-fn-return-for-capturing-closure.rs:45:24 | ||
| | | ||
| LL | println!("{}", a); | ||
| | ^ `a` captured here | ||
| help: change the return type to return a type-erased closure instead | ||
| | | ||
| LL - fn one_arg_ref(a: i32) -> fn(&i32) -> &i32 { | ||
| LL + fn one_arg_ref(a: i32) -> impl Fn(&'_ i32) -> &'_ i32 { | ||
| | |
There was a problem hiding this comment.
In this case I think it would make sense to suggest impl for<'a> Fn(&'a i32) -> &'_ i32.
|
@bors r+ Let's merge, but leave the ticket open to account for more accurate lifetime handling, as well as detecting if it would need to be |
…uwer Rollup of 18 pull requests Successful merges: - #159600 (`rust-analyzer` subtree update) - #158046 (proc_macro: preserve file module spans for inner attrs) - #159000 (Small cleanups to the incr comp session code) - #159189 (Account for type alias projections in E0308 "expected/found" shortening logic) - #159449 (Enable single Location to issue multiple borrows) - #159544 (Suggest valid command-line crate names) - #159587 (Improve `AttrItem::span`) - #159594 (feat(rustc_hir_typeck): suggest `impl Fn` return for capturing closures) - #159597 (std: use `arc4random_buf` from libc) - #159599 (Resolver: Record at least 1 ambiguous trait if main decl is not a trait.) - #158061 (Make `pin!()` more foolproof.) - #159460 (Do not mark unnormalized const aliases as rigid when normalizing param env) - #159529 (Add regression test for nested replacement ranges in `collect_tokens`) - #159571 (Remove unused bundled library lookup for the local crate) - #159585 (Minor `TokenStream` improvements) - #159586 (Separate `InterpCx` usage by `ConstAnalysis` phases) - #159602 (Remove `ItemLike`) - #159603 (Clarify `push_stream`/`push_tree`) Failed merges: - #159590 (Remove some dead code)
Adresses #159481