Skip to content

Can't coerce closure to fn in a tuple #101705

Open
@camsteffen

Description

@camsteffen

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

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-coercionsArea: implicit and explicit `expr as Type` coercionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions