New issue

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

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

Already on GitHub? Sign in to your account

compiler suggests invalid code with ref pattern #54180

Open
ehuss opened this Issue Sep 13, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@ehuss
Copy link
Contributor

ehuss commented Sep 13, 2018

The following example:

struct S {
    f1: i32,
}

fn main() {
    let s = S { f1: 123 };
    let S { ref f1 } = s;
}

Gives the following suggestion:

warning: unused variable: `f1`
 --> src/main.rs:9:15
  |
9 |     let S{ref f1} = s;
  |               ^^ help: try ignoring the field: `f1: _`
  |
  = note: #[warn(unused_variables)] on by default

When the suggestion is applied, the code does not compile:

error: expected `,`
 --> src/main.rs:9:17
  |
9 |     let S { ref f1: _ } = s;
  |                 ^^

error[E0027]: pattern does not mention field `f1`
 --> src/main.rs:9:9
  |
9 |     let S { ref f1: _ } = s;
  |         ^^^^^^^^^^^^^^^ missing field `f1`

error: aborting due to 2 previous errors

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

I'm not sure what the best suggestion would be. Removing the ref might be an option (I'm not sure if using a wildcard is equivalent in this case?). Suggesting .. is another option.

This suggestion was changed in 1.28 (previous versions gave a different, wrong suggestion). Tested up to rustc 1.30.0-nightly (2d4e34c 2018-09-09).

@csmoe

This comment has been minimized.

Copy link
Member

csmoe commented Sep 13, 2018

warning: unused variable: `f1`
 --> src/main.rs:9:15
  |
9 |     let S{ref f1} = s;
- |               ^^ help: tryignoring the field: `f1: _`
+ |           ^^^^^^ help: try ignoring the field: `f1: _`
  |
  = note: #[warn(unused_variables)] on by default

the ambiguity in the lint caused by the wrong span.
(btw, this won't be an issue in edtion-2018 since ref isn't "recommended" there https://rust-lang-nursery.github.io/edition-guide/rust-2018/ownership-and-lifetimes/default-match-bindings.html)

@ambaxter

This comment has been minimized.

Copy link

ambaxter commented Dec 6, 2018

There is an issue regarding this with the 2018 edition.

@ehuss

This comment has been minimized.

Copy link
Contributor

ehuss commented Dec 6, 2018

Just to be clear, AFAIK there are no lints for transitioning to default binding mode (edition idiom or otherwise), so the comment that it won't be an issue in 2018 isn't correct, this issue still applies to all editions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment