Skip to content

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

Closed
@bachue

Description

@bachue

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions