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

Suggest Box::new on E0308 when appropriate #63506

Closed
estebank opened this issue Aug 12, 2019 · 0 comments · Fixed by #63507
Closed

Suggest Box::new on E0308 when appropriate #63506

estebank opened this issue Aug 12, 2019 · 0 comments · Fixed by #63507
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

When encountering a boxed value as expected and a stack allocated value that could be boxed to fulfill the expectation, like in the following snippet, suggest Box::new wrapping:

fn main() {
    let x: Box<Fn() -> _> = || {
        Err(())?;
        Ok(())
    };
}
error[E0308]: mismatched types
 --> src/main.rs:3:29
  |
3 |       let x: Box<Fn() -> _> = || {
  |  _____________________________^
4 | |         Err(())?;
5 | |         Ok(())
6 | |     };
  | |_____^ expected struct `std::boxed::Box`, found closure
  |
  = note: expected type `std::boxed::Box<dyn std::ops::Fn() -> _>`
             found type `[closure@src/main.rs:3:29: 6:6]`

Should suggest:

fn main() {
    let x: Box<Fn() -> _> = Box::new(|| {
        Err(())?;
        Ok(())
    });
}
@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority A-closures Area: Closures (`|…| { … }`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Aug 12, 2019
Centril added a commit to Centril/rust that referenced this issue Aug 14, 2019
…ntril

When needing type annotations in local bindings, account for impl Trait and closures

Fix rust-lang#46680, fix rust-lang#63504, fix rust-lang#63506, fix rust-lang#40014, cc rust-lang#63502.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. P-low Low priority 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.

1 participant