Skip to content

Handle unit struct with same name as field in pattern destructuring #55631

Closed
@estebank

Description

@estebank

Given the following code

struct foo;
struct Thing { foo: String }

fn example(t: Thing) {
    let Thing { foo } = t;
}

generate the following error

error[E0530]: let bindings cannot shadow tuple structs
 --> src/lib.rs:5:17
  |
1 | struct foo;
  | ------------- a unit struct `foo` is defined here
...
5 |     let Thing { foo } = t;
  |                 ^^^ cannot be named the same as a unit struct

instead of

error[E0308]: mismatched types
 --> src/lib.rs:5:17
  |
5 |     let Thing { foo } = t;
  |                 ^^^ expected struct `std::string::String`, found struct `foo`
  |
  = note: expected type `std::string::String`
             found type `foo`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.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