Closed
Description
After #71599, coercing (FnDef | Closure) x (FnDef | Closure)
is possible. It fails when one of them is capturing closure. But currently the error message is not that good.
For example:
fn main() {
// Good
let _: fn(usize) -> usize = match true {
true => |a| a + 1,
false => |a| a - 1,
};
// Bad
let b = 2;
let _: fn(usize) -> usize = match true {
true => |a| a + 1,
false => |a| a - b,
};
}
Emits:
error[E0308]: `match` arms have incompatible types
--> src\main.rs:12:18
|
10 | let _: fn(usize) -> usize = match true {
| _________________________________-
11 | | true => |a| a + 1,
| | --------- this is found to be of type `fn(usize) -> usize`
12 | | false => |a| a - b,
| | ^^^^^^^^^ expected fn pointer, found closure
13 | | };
| |_____- `match` arms have incompatible types
|
= note: expected fn pointer `fn(usize) -> usize`
found closure `[closure@src\main.rs:12:18: 12:27 b:_]`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
We actually can hint user to use non-capturing closure here.
This is related to #71895