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

On E0308, detect incorrect attempts to use trait objects #102629

Closed
estebank opened this issue Oct 3, 2022 · 0 comments · Fixed by #120315
Closed

On E0308, detect incorrect attempts to use trait objects #102629

estebank opened this issue Oct 3, 2022 · 0 comments · Fixed by #120315
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Oct 3, 2022

https://users.rust-lang.org/t/expected-trait-object-dyn-trait-found-struct-struct/82198

When encountering cases where the user's intent is to have a trait object, but we produce an E0308, we should put some effort in sending them in the right direction. We should provide a suggestion with the right code to properly box and erase the type in the appropriate branches.

let a: dyn Trait = if true {
    Struct
} else {
    foo() // -> dyn Trait
};
let a: dyn Trait = if true {
    Struct
} else {
    foo() // -> Box<dyn Trait>
};
let a: dyn Trait = if true {
    Struct
} else {
    foo() // -> impl Trait
};
let a: Box<dyn Trait> = if true {
    Box::new(Struct)
} else {
    foo() // -> dyn Trait
};
let a: Box<dyn Trait> = if true {
    Box::new(Struct)
} else {
    foo() // -> Box<dyn Trait>
};
let a: Box<dyn Trait> = if true {
    Box::new(Struct)
} else {
    foo() // -> impl Trait
};
let a: Trait = if true {
    Struct
} else {
    foo() // -> dyn Trait
};
let a: Trait = if true {
    Struct
} else {
    foo() // -> Box<dyn Trait>
};
let a: Trait = if true {
    Struct
} else {
    foo() // -> impl Trait
};
let a: Box<dyn Trait> = if true {
    Struct
} else {
    foo() // -> Box<dyn Trait>
};
let a: dyn Trait = if true {
    Struct
} else {
    foo() // -> impl Trait
};

We also want to do this if the binding doesn't have an explicit type, but that will require looking at the diverging code branches to see if any of them is a "root" trait (if there are multiple trait objects of different traits, and any of them is super trait to all others), and for the explicit types if they implement the trait and can be coerced to the desired trait object.

@estebank estebank 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. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Oct 3, 2022
estebank added a commit to estebank/rust that referenced this issue Jan 23, 2024
When encountering

```rust
let _ = if true {
    Struct
} else {
    foo() // -> Box<dyn Trait>
};
```

if `Struct` implements `Trait`, suggest boxing the then arm tail expression.

Part of rust-lang#102629.
fmease added a commit to fmease/rust that referenced this issue Jan 23, 2024
Provide structured suggestion to use trait objects in some cases of `if` arm type divergence

```
error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:15:9
   |
LL |       let _ = if true {
   |  _____________-
LL | |         Struct
   | |         ------ expected because of this
LL | |     } else {
LL | |         foo()
   | |         ^^^^^ expected `Struct`, found `Box<dyn Trait>`
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected struct `Struct`
              found struct `Box<dyn Trait>`
help: `Struct` implements `Trait` so you can box it to coerce to the trait object `Box<dyn Trait>`
   |
LL |         Box::new(Struct)
   |         +++++++++      +

error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:20:9
   |
LL |       let _ = if true {
   |  _____________-
LL | |         foo()
   | |         ----- expected because of this
LL | |     } else {
LL | |         Struct
   | |         ^^^^^^ expected `Box<dyn Trait>`, found `Struct`
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected struct `Box<dyn Trait>`
              found struct `Struct`
   = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
help: store this in the heap by calling `Box::new`
   |
LL |         Box::new(Struct)
   |         +++++++++      +

error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:25:9
   |
LL |   fn bar() -> impl Trait {
   |               ---------- the found opaque type
...
LL |       let _ = if true {
   |  _____________-
LL | |         Struct
   | |         ------ expected because of this
LL | |     } else {
LL | |         bar()
   | |         ^^^^^ expected `Struct`, found opaque type
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note:   expected struct `Struct`
           found opaque type `impl Trait`
help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>`
   |
LL ~         Box::new(Struct) as Box<dyn Trait>
LL |     } else {
LL ~         Box::new(bar())
   |

error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:30:9
   |
LL |   fn bar() -> impl Trait {
   |               ---------- the expected opaque type
...
LL |       let _ = if true {
   |  _____________-
LL | |         bar()
   | |         ----- expected because of this
LL | |     } else {
LL | |         Struct
   | |         ^^^^^^ expected opaque type, found `Struct`
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected opaque type `impl Trait`
                   found struct `Struct`
help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>`
   |
LL ~         Box::new(bar()) as Box<dyn Trait>
LL |     } else {
LL ~         Box::new(Struct)
   |
```

Partially address rust-lang#102629.
fmease added a commit to fmease/rust that referenced this issue Jan 23, 2024
Provide structured suggestion to use trait objects in some cases of `if` arm type divergence

```
error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:15:9
   |
LL |       let _ = if true {
   |  _____________-
LL | |         Struct
   | |         ------ expected because of this
LL | |     } else {
LL | |         foo()
   | |         ^^^^^ expected `Struct`, found `Box<dyn Trait>`
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected struct `Struct`
              found struct `Box<dyn Trait>`
help: `Struct` implements `Trait` so you can box it to coerce to the trait object `Box<dyn Trait>`
   |
LL |         Box::new(Struct)
   |         +++++++++      +

error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:20:9
   |
LL |       let _ = if true {
   |  _____________-
LL | |         foo()
   | |         ----- expected because of this
LL | |     } else {
LL | |         Struct
   | |         ^^^^^^ expected `Box<dyn Trait>`, found `Struct`
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected struct `Box<dyn Trait>`
              found struct `Struct`
   = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
help: store this in the heap by calling `Box::new`
   |
LL |         Box::new(Struct)
   |         +++++++++      +

error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:25:9
   |
LL |   fn bar() -> impl Trait {
   |               ---------- the found opaque type
...
LL |       let _ = if true {
   |  _____________-
LL | |         Struct
   | |         ------ expected because of this
LL | |     } else {
LL | |         bar()
   | |         ^^^^^ expected `Struct`, found opaque type
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note:   expected struct `Struct`
           found opaque type `impl Trait`
help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>`
   |
LL ~         Box::new(Struct) as Box<dyn Trait>
LL |     } else {
LL ~         Box::new(bar())
   |

error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:30:9
   |
LL |   fn bar() -> impl Trait {
   |               ---------- the expected opaque type
...
LL |       let _ = if true {
   |  _____________-
LL | |         bar()
   | |         ----- expected because of this
LL | |     } else {
LL | |         Struct
   | |         ^^^^^^ expected opaque type, found `Struct`
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected opaque type `impl Trait`
                   found struct `Struct`
help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>`
   |
LL ~         Box::new(bar()) as Box<dyn Trait>
LL |     } else {
LL ~         Box::new(Struct)
   |
```

Partially address rust-lang#102629.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 24, 2024
Rollup merge of rust-lang#120261 - estebank:issue-102629, r=wesleywiser

Provide structured suggestion to use trait objects in some cases of `if` arm type divergence

```
error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:15:9
   |
LL |       let _ = if true {
   |  _____________-
LL | |         Struct
   | |         ------ expected because of this
LL | |     } else {
LL | |         foo()
   | |         ^^^^^ expected `Struct`, found `Box<dyn Trait>`
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected struct `Struct`
              found struct `Box<dyn Trait>`
help: `Struct` implements `Trait` so you can box it to coerce to the trait object `Box<dyn Trait>`
   |
LL |         Box::new(Struct)
   |         +++++++++      +

error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:20:9
   |
LL |       let _ = if true {
   |  _____________-
LL | |         foo()
   | |         ----- expected because of this
LL | |     } else {
LL | |         Struct
   | |         ^^^^^^ expected `Box<dyn Trait>`, found `Struct`
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected struct `Box<dyn Trait>`
              found struct `Struct`
   = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
help: store this in the heap by calling `Box::new`
   |
LL |         Box::new(Struct)
   |         +++++++++      +

error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:25:9
   |
LL |   fn bar() -> impl Trait {
   |               ---------- the found opaque type
...
LL |       let _ = if true {
   |  _____________-
LL | |         Struct
   | |         ------ expected because of this
LL | |     } else {
LL | |         bar()
   | |         ^^^^^ expected `Struct`, found opaque type
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note:   expected struct `Struct`
           found opaque type `impl Trait`
help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>`
   |
LL ~         Box::new(Struct) as Box<dyn Trait>
LL |     } else {
LL ~         Box::new(bar())
   |

error[E0308]: `if` and `else` have incompatible types
  --> $DIR/suggest-box-on-divergent-if-else-arms.rs:30:9
   |
LL |   fn bar() -> impl Trait {
   |               ---------- the expected opaque type
...
LL |       let _ = if true {
   |  _____________-
LL | |         bar()
   | |         ----- expected because of this
LL | |     } else {
LL | |         Struct
   | |         ^^^^^^ expected opaque type, found `Struct`
LL | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected opaque type `impl Trait`
                   found struct `Struct`
help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>`
   |
LL ~         Box::new(bar()) as Box<dyn Trait>
LL |     } else {
LL ~         Box::new(Struct)
   |
```

Partially address rust-lang#102629.
estebank added a commit to estebank/rust that referenced this issue Jan 24, 2024
When encountering a type mismatch error involving `dyn Trait`, mention
the existence of boxed trait objects if the other type involved
implements `Trait`.

Partially addresses rust-lang#102629.
@bors bors closed this as completed in 0cbef47 Jan 25, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 25, 2024
Rollup merge of rust-lang#120315 - estebank:issue-102629-2, r=wesleywiser

On E0308 involving `dyn Trait`, mention trait objects

When encountering a type mismatch error involving `dyn Trait`, mention the existence of boxed trait objects if the other type involved implements `Trait`.

Fix rust-lang#102629.
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` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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