Skip to content

Wrong prompt when failed to pattern match boxed enum type #68628

New issue

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

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

Already on GitHub? # to your account

Closed
bachue opened this issue Jan 29, 2020 · 3 comments
Closed

Wrong prompt when failed to pattern match boxed enum type #68628

bachue opened this issue Jan 29, 2020 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bachue
Copy link

bachue commented Jan 29, 2020

For example:

#[allow(dead_code)]
enum A {
    V1(u8),
    V2(u32),
}

fn main() {
    let a = Some(Box::new(A::V1(1u8)));
    
    if let Some(b) = a.as_ref() {
        if let A::V1(v) = b {
            println!("{:?}", v);
        }
    }
}

The rust 1.40 compiler will failed to compile this code and tell me:

error[E0308]: mismatched types
  --> src/main.rs:11:16
   |
11 |         if let A::V1(v) = b {
   |                ^^^^^^^^   -
   |                |          |
   |                |          this match expression has type `std::boxed::Box<A>`
   |                |          help: consider dereferencing the boxed value: `*b`
   |                expected struct `std::boxed::Box`, found enum `A`
   |
   = note: expected type `std::boxed::Box<A>`
              found type `A`

But unfortunately it's wrong. If I replace b by *b, it will failed again and tell me:

error[E0308]: mismatched types
  --> src/main.rs:11:16
   |
11 |         if let A::V1(v) = *b {
   |                ^^^^^^^^   --
   |                |          |
   |                |          this match expression has type `std::boxed::Box<A>`
   |                |          help: consider dereferencing the boxed value: `**b`
   |                expected struct `std::boxed::Box`, found enum `A`
   |
   = note: expected type `std::boxed::Box<A>`
              found type `A`

Although the second prompt is correct and it works. But I think using b.as_ref() is the better idea.

@jonas-schievink jonas-schievink added 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. labels Jan 29, 2020
@estebank
Copy link
Contributor

No suggestion is presented anymore for the first case. We should have a suggestion for **b.

@kadiwa4
Copy link
Contributor

kadiwa4 commented Dec 24, 2024

The current output is correct and helpful (nightly-2024-12-22):

error[E0308]: mismatched types
  --> src/main.rs:11:16
   |
11 |         if let A::V1(v) = b {
   |                ^^^^^^^^   - this expression has type `&Box<A>`
   |                |
   |                expected `Box<A>`, found `A`
   |
   = note: expected struct `Box<A>`
                found enum `A`
help: consider dereferencing to access the inner value using the Deref trait
   |
11 |         if let A::V1(v) = &**b {
   |                           ~~~~

For more information about this error, try `rustc --explain E0308`.

A test could be added to tests/ui/suggestions/suggest-deref-in-match-issue-132784.rs.
@rustbot label: +E-needs-test

@rustbot rustbot added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Dec 24, 2024
estebank added a commit to estebank/rust that referenced this issue Dec 28, 2024
```
error[E0308]: mismatched types
  --> $DIR/suggest-dereferencing-through-Deref.rs:13:16
   |
LL |         if let A::V1(v) = b {
   |                ^^^^^^^^   - this expression has type `&Box<A>`
   |                |
   |                expected `Box<A>`, found `A`
   |
   = note: expected struct `Box<A>`
                found enum `A`
help: consider dereferencing to access the inner value using the Deref trait
   |
LL |         if let A::V1(v) = &**b {
   |                           ~~~~

error[E0308]: mismatched types
  --> $DIR/suggest-dereferencing-through-Deref.rs:23:16
   |
LL |         if let A::V1(v) = *b {
   |                ^^^^^^^^   -- this expression has type `Box<A>`
   |                |
   |                expected `Box<A>`, found `A`
   |
   = note: expected struct `Box<A>`
                found enum `A`
help: consider dereferencing the boxed value
   |
LL |         if let A::V1(v) = **b {
   |                           ~~~
```

Fix rust-lang#68628
@compiler-errors
Copy link
Member

This was fixed in #132939.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. 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.

6 participants