Use parenthetical notation for Fn traits#125778
Conversation
Always use the `Fn(T) -> R` format when printing closure traits instead of `Fn<(T,), Output = R>`. Fix rust-lang#67100: ``` error[E0277]: expected a `Fn()` closure, found `F` --> file.rs:6:13 | 6 | call_fn(f) | ------- ^ expected an `Fn()` closure, found `F` | | | required by a bound introduced by this call | = note: wrap the `F` in a closure with no arguments: `|| { /* code */ }` note: required by a bound in `call_fn` --> file.rs:1:15 | 1 | fn call_fn<F: Fn() -> ()>(f: &F) { | ^^^^^^^^^^ required by this bound in `call_fn` help: consider further restricting this bound | 5 | fn call_any<F: std::any::Any + Fn()>(f: &F) { | ++++++ ```
|
To be clear, since it was not acknowledged in the PR description, this is not a general fix for #67100. Specifically, the suggestion is not going to be right when the trait bound suggestion needs a return type, since all we have is a trait ref (no return type) and not a projection predicate (has a return type). Slightly modifying the test from the issue: fn call_fn<F: Fn() -> i32>(f: &F) {
f()
}
fn call_any<F: std::any::Any>(f: &F) {
call_fn(f)
}
fn main() {
call_any(&|| { 1 });
}...which suggests |
|
@compiler-errors you're absolutely right. I missed that because I looked at the suggestion to turn |
|
@bors r=compiler-errors |
|
☀️ Test successful - checks-actions |
|
Finished benchmarking commit (1d52972): comparison URL. Overall result: ❌ regressions - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)Results (primary -3.4%, secondary -3.2%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (secondary -2.7%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 670.518s -> 669.197s (-0.20%) |
Always use the
Fn(T) -> Rformat when printing closure traits instead ofFn<(T,), Output = R>.Address #67100: