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

Detect when if/else is tail expression has mismatched types that could be a boxed trait #69107

Closed
estebank opened this issue Feb 12, 2020 · 0 comments · Fixed by #75608
Closed
Labels
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. D-papercut Diagnostics: An error or lint that needs small tweaks. 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

Given the following

struct S;
struct Y;

trait Trait {}

impl Trait for S {}
impl Trait for Y {}

fn baz() -> impl Trait {
    if true {
        S
    } else {
        Y
    }
}

we currently emit

error[E0308]: `if` and `else` have incompatible types
  --> src/main.rs:13:9
   |
10 | /     if true {
11 | |         S
   | |         - expected because of this
12 | |     } else {
13 | |         Y
   | |         ^ expected struct `S`, found struct `Y`
14 | |     }
   | |_____- `if` and `else` have incompatible types

We should suggest changing the return type from impl Trait to Box<dyn Trait> and boxing the two branches. I think this is the last missing suggestion we can give involding impl Trait and dyn Trait returns.

Follow up to #68110.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority 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` D-papercut Diagnostics: An error or lint that needs small tweaks. labels Feb 12, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 16, 2020
@bors bors closed this as completed in 41dc394 Sep 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. D-papercut Diagnostics: An error or lint that needs small tweaks. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
2 participants