Open
Description
The following code:
fn tuple_closures_test(n: u32) {
let (print_it, is_zero) = match 0 {
0 => (|| println!("zero"), true),
_ => (|| println!("other"), false),
};
print_it();
}
Produces this error:
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): `match` arms have incompatible types
--> src/lib.rs:13:14
|
11 | let (print_it, is_zero) = match 0 {
| _______________________________-
12 | | 0 => (|| println!("zero"), true),
| | ---------------------------
| | ||
| | |the expected closure
| | this is found to be of type `([closure@src/lib.rs:12:15: 12:34], bool)`
13 | | _ => (|| println!("other"), false),
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected closure, found a different closure
14 | | };
| |_____- `match` arms have incompatible types
|
= note: expected tuple `([closure@src/lib.rs:12:15: 12:34], _)`
found tuple `([closure@src/lib.rs:13:15: 13:35], _)`
= note: no two closures, even if identical, have the same type
= help: consider boxing your closure and/or using it as a trait object
But I think it should compile by coercing the closures to fn
types. The following compiles:
fn test(n: u32) {
let print_it = match n {
0 => || println!("zero"),
_ => || println!("other"),
};
print_it();
}
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
BGR360 commentedon Sep 12, 2022
@rustbot label C-bug T-compiler A-closures A-coercions
compiler-errors commentedon Oct 22, 2022
As far as I'm aware, coercion only really goes one level deep when it comes to types -- we don't make an attempt to coerce similar cases when the closures are boxed, wrapped in a struct, etc., so this seems like consistent behavior for tuples.
So I'm not really certain this is a bug, or an extension to coercion behavior that needs to be better well-defined and investigated for feasibility.