Skip to content

Unhelpful (incorrect?) match_default_bindings hint #45925

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

Closed
mark-i-m opened this issue Nov 11, 2017 · 2 comments
Closed

Unhelpful (incorrect?) match_default_bindings hint #45925

mark-i-m opened this issue Nov 11, 2017 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.

Comments

@mark-i-m
Copy link
Member

Here is the whole error output:

error: non-reference pattern used to match a reference (see issue #42640)
   --> src/coord.rs:194:23
    |
194 |             .filter(|&(ref k, _)| self.hash(k) == server)
    |                       ^^^^^^^^^^ help: consider using: `&(ref k, _)`
    |
    = help: add #![feature(match_default_bindings)] to the crate attributes to enable

error: aborting due to previous error

error: Could not compile `kv_2pc`.

To learn more, run the command again with --verbose.

Specifically, notice the hint:

help: consider using: `&(ref k, _)`

which suggests doing something I am already doing 😛 ...

The code snippet in question:

    fn send_prepare(&self, server: &str, txn: TxnId, staged: &Vec<(String, String)>) -> Flags {
        // Only send to the server the items on that server!
        let mut staged = staged
            .iter()
            .filter(|&(ref k, _)| self.hash(k) == server)
            .collect();

        retry_forever!{
            do {
                //     SendToServer(s, {Prepare})
                let server = SyncClient::connect(server, client::Options::default()).unwrap();
                server.send_to_server(txn, Flags::PREPARE, Some(staged.clone()))
            },

            retry if Err(tarpc::Error::Io(_)),

            match {
                Ok(flags) => flags,
                err => panic!("Error with (de)serialization: {:?}", err)
            }
        }
    }

Changing .filter(|&(ref k, _)| self.hash(k) == server) to .filter(|pair| self.hash(&pair.0) == server) sidesteps the problem.

$ rustc --version --verbose
rustc 1.23.0-nightly (a35a3abcd 2017-11-10)
binary: rustc
commit-hash: a35a3abcda67a729edbb7d649dbc663c6feabd4c
commit-date: 2017-11-10
host: x86_64-unknown-linux-gnu
release: 1.23.0-nightly
LLVM version: 4.0
@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. labels Nov 11, 2017
@estebank
Copy link
Contributor

Minimal repro:

fn foo(s: &str) -> bool { true }
fn main() {
    let x = vec![(String::new(), String::new())];
    x.iter()
        .filter(|&(ref a, _)| foo(a))
        .collect();
}

Code that needs to be updated:

let mut err = feature_gate::feature_err(
&tcx.sess.parse_sess,
"match_default_bindings",
pat.span,
feature_gate::GateIssue::Language,
"non-reference pattern used to match a reference",
);
if let Ok(snippet) = tcx.sess.codemap().span_to_snippet(pat.span) {
err.span_suggestion(pat.span, "consider using", format!("&{}", &snippet));
}

@arielb1
Copy link
Contributor

arielb1 commented Nov 13, 2017

Actual minimal example:

fn main() {
    let x = &&();
    let &() = x;
}

I think we should just expand our span to contain surrounding reference patterns.

bors added a commit that referenced this issue Nov 26, 2017
…ielb1

Be more obvious when suggesting dereference

Include `&` span when suggesting dereference on a span that is already a reference:

```
error: non-reference pattern used to match a reference (see issue #42640)
  --> dont-suggest-dereference-on-arg.rs:16:19
   |
16 |         .filter(|&(ref a, _)| foo(a))
   |                  ^^^^^^^^^^^ help: consider using: `&&(ref k, _)`
   |
   = help: add #![feature(match_default_bindings)] to the crate attributes to enable
```

Fix #45925.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants