Skip to content

Field pattern shorthand can be interpreted as an item reference #42876

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

Open
qnighy opened this issue Jun 24, 2017 · 7 comments
Open

Field pattern shorthand can be interpreted as an item reference #42876

qnighy opened this issue Jun 24, 2017 · 7 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-patterns Relating to patterns and pattern matching C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@qnighy
Copy link
Contributor

qnighy commented Jun 24, 2017

B { X } is a shorthand for B { X: X } and X here can be intepreted as an item reference, rather than a binding. I don't know if this is actually a bug or not. Possible fixes are:

  • Leave it as is.
  • Leave it as is with some warning.
  • Force X be interpreted as a binding and let it fail to compile (due to local-global shadowing).
const X : i32 = 10;
struct B {
    X: i32,
}
fn main() {
    match (B { X: 9 }) {
        B { X } => println!("Foo!"),
        B { .. } => println!("Bar!"),
    }
}
  • Current behavior: Bar! is printed.
  • Possible expected behaviors: as-is or some warning/error emitted.
  • Meta: rustc 1.20.0-nightly (ab5bec255 2017-06-22)
@Mark-Simulacrum Mark-Simulacrum added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jun 24, 2017
@Mark-Simulacrum
Copy link
Member

Nominating for @rust-lang/lang. I don't think changing things here at this point is going to be easy (feature is stable) but perhaps we could do a warning cycle if we wanted to not allow this.

@petrochenkov
Copy link
Contributor

I don't think this is a bug, just the shorthand desugaring and "existing entity vs fresh binding" disambiguation working orthogonally and as expected. (I.e. a perfect behavior for a corner case with near-zero practical impact.)

Force X be interpreted as a binding and let it fail to compile (due to local-global shadowing).

The wording for the "local-global shadowing" error is misleading, it's actually a "can't disambiguate between existing entity and fresh binding" error, not shadowing error (and it's reported more often than strictly necessary, I was planning to fix it, but still never got to this). If we can force X to be interpreted as a binding in some context, then we can disambiguate (in favor of the binding) and there should be no error.

@durka
Copy link
Contributor

durka commented Jun 24, 2017

I don't see what this has to do with the shorthand? If the pattern is B { X: X } then it also prints "Bar!".

@eddyb
Copy link
Member

eddyb commented Jun 24, 2017

Is there no warning on the field name of X (in the struct definition)?

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Jul 6, 2017

I'm of the opinion that

(a) Bar { X } should always be equivalent Bar { X: X }
and (b) I would expect Bar { X } to be a binding

hence I'd prefer to have a lint targeting this case specifically.

@aturon aturon removed the I-nominated label Jul 6, 2017
@Mark-Simulacrum Mark-Simulacrum added C-bug Category: This is a bug. A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed C-bug Category: This is a bug. labels Jul 28, 2017
@arielb1
Copy link
Contributor

arielb1 commented Nov 26, 2017

+1 for having this a lint

@steveklabnik
Copy link
Member

Triage: not aware of any movement on this issue.

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.
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.
@fmease fmease added the A-patterns Relating to patterns and pattern matching label Apr 30, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-patterns Relating to patterns and pattern matching C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

10 participants