Skip to content
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

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

Closed
estebank opened this issue Nov 2, 2018 · 3 comments · Fixed by #67741
Closed

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

estebank opened this issue Nov 2, 2018 · 3 comments · Fixed by #67741
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Nov 2, 2018

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`
@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 2, 2018
@petrochenkov
Copy link
Contributor

foo is not a binding in this case and it's indeed a type mismatch error.
It's probably not a good idea to give plain incorrect information in diagnostics.
Perhaps something like 765076f may be a better approach? I.e. explicitly telling that foo is not interpreted as a new binding.

@estebank
Copy link
Contributor Author

estebank commented Nov 2, 2018

@petrochenkov that's not a bad option. The proposed output is based on the output when writing struct foo(); instead.

@qnighy
Copy link
Contributor

qnighy commented Nov 3, 2018

Related: #42876

@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2019
Centril added a commit to Centril/rust that referenced this issue Mar 7, 2020
When encountering an Item in a pat context, point at the item def

```
error[E0308]: mismatched types
  --> $DIR/const-in-struct-pat.rs:8:17
   |
LL | struct foo;
   | ----------- `foo` defined here
...
LL |     let Thing { foo } = t;
   |                 ^^^ expected struct `std::string::String`, found struct `foo`
   |
   = note: `foo` is interpreted as a unit struct, not a new binding
help: you can bind the struct field to a different name
   |
LL |     let Thing { foo: other_foo } = t;
   |                 ^^^^^^^^^^^^^^
```
```
error[E0308]: mismatched types
  --> $DIR/const.rs:14:9
   |
LL | const FOO: Foo = Foo{bar: 5};
   | ----------------------------- constant defined here
...
LL |         FOO => {},
   |         ^^^
   |         |
   |         expected `&Foo`, found struct `Foo`
   |         `FOO` is interpreted as a constant, not a new binding
   |         help: use different name to introduce a new binding: `other_foo`
```

Fix rust-lang#55631, fix rust-lang#48062, cc rust-lang#42876.
@bors bors closed this as completed in e8bb6c0 Mar 7, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. 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.

3 participants