Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when coercing closures fails #72457

Closed
ldm0 opened this issue May 22, 2020 · 1 comment · Fixed by #78901
Closed

Improve error message when coercing closures fails #72457

ldm0 opened this issue May 22, 2020 · 1 comment · Fixed by #78901
Assignees
Labels
A-closures Area: Closures (`|…| { … }`) A-coercions Area: implicit and explicit `expr as Type` coercions A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ldm0
Copy link
Contributor

ldm0 commented May 22, 2020

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

@ldm0 ldm0 changed the title Improve error message when coercing closures failed. Improve error message when coercing closures fails May 22, 2020
@LeSeulArtichaut LeSeulArtichaut added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 22, 2020
@jonas-schievink jonas-schievink added A-closures Area: Closures (`|…| { … }`) A-coercions Area: implicit and explicit `expr as Type` coercions C-enhancement Category: An issue proposing an enhancement or a PR with one. labels May 22, 2020
@arora-aman
Copy link
Member

@rustbot claim

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jan 13, 2021
…tebank

diagnostics: Note capturing closures can't be coerced to fns

Fixes rust-lang#72457, fixes rust-lang#71895

r? `@estebank`
@bors bors closed this as completed in 1841a5f Jan 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-coercions Area: implicit and explicit `expr as Type` coercions A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants