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 dereferencing boxed enum when matching #57741

Closed
estebank opened this issue Jan 18, 2019 · 1 comment
Closed

Suggest dereferencing boxed enum when matching #57741

estebank opened this issue Jan 18, 2019 · 1 comment
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`

Comments

@estebank
Copy link
Contributor

Given the following code:

enum E {
    A(usize),
    B(usize),
}

fn main() {
    let x = Box::new(E::A(3));
    let y = match x {
        E::A(a) | E::B(a) => a,
    };
}

the compiler outputs:

error[E0308]: mismatched types
 --> src/main.rs:9:9
  |
8 |     let y = match x {
  |                   - this match expression has type `std::boxed::Box<E>`
9 |         E::A(a) | E::B(a) => a,
  |         ^^^^^^^ expected struct `std::boxed::Box`, found enum `E`
  |
  = note: expected type `std::boxed::Box<E>`
             found type `E`

error[E0308]: mismatched types
 --> src/main.rs:9:19
  |
8 |     let y = match x {
  |                   - this match expression has type `std::boxed::Box<E>`
9 |         E::A(a) | E::B(a) => a,
  |                   ^^^^^^^ expected struct `std::boxed::Box`, found enum `E`
  |
  = note: expected type `std::boxed::Box<E>`
             found type `E`

Adding changing line 8 to let y = match *x { makes the code compile correctly (partly thanks to match ergonomics, I believe).

The error message would ideally be closer to

error[E0308]: mismatched types
 --> src/main.rs:9:9
  |
8 |     let y = match x {
  |                   -
  |                   |
  |                   this match expression has type `std::boxed::Box<E>`
  |                   help: dereference the boxed value: `*x`
9 |         E::A(a) | E::B(a) => a,
  |         ^^^^^^^   ^^^^^^^ expected struct `std::boxed::Box`, found enum `E`
  |         |
  |         expected struct `std::boxed::Box`, found enum `E`
  |
  = note: expected type `std::boxed::Box<E>`
             found type `E`

Adding the suggestion should be relatively easy, condensing the multiple alternative match arm discriminants with mismatched type errors into one might be more involved.


@nikomatsakis should we consider extending the match ergonomics to also account for Box?


Case taken from an old reddit post from a newcomer to the language.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Jan 18, 2019
@davidtwco
Copy link
Member

Submitted #57783 for the suggestion - this doesn't condense duplicate errors from the match arms.

@davidtwco davidtwco self-assigned this Jan 20, 2019
Centril added a commit to Centril/rust that referenced this issue Jan 21, 2019
Add "dereference boxed value" suggestion.

Contributes to rust-lang#57741.

This PR adds a `help: consider dereferencing the boxed value` suggestion to discriminants of match statements when the match arms have type `T` and the discriminant has type `Box<T>`.

r? @estebank
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`
Projects
None yet
Development

No branches or pull requests

2 participants