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

On error due to reference introduced by default binding modes, point out where reference was introduced #70933

Closed
CAD97 opened this issue Apr 8, 2020 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Apr 8, 2020

Simple example:

fn example(x: &Option<S>) {
    match x {
        Some(val) => drop::<S>(val),
        None => (),
    }
}
error[E0308]: mismatched types
 --> src/lib.rs:6:32
  |
6 |         Some(val) => drop::<S>(val),
  |         ^^^^^^^^^              ^^^
  |         |                      |
  |         |                      expected struct `S`, found `&S`
  |         |                      help: consider dereferencing the borrow: `*val`
  |         note: borrow introduced here because pattern has type `&std::option::Option<crate::S>`
  |         help: consider destructuring the borrow here: `&Some(val)`

(plus if/when Some(&val) works to destrucure the borrow, suggest that instead)

Initially suggested on irlo, cc @joshtriplett @rpjohnst

This should help remove some of the pain points people have had around default binding modes ("match ergonomics") introducing needless borrows, as the error message now also points at the pattern, like it did before default binding modes were introduced.

(As a side note: a (clippy) lint for binding a Copy value by reference that could be bound by copy would also help here, independently of improving the error message.)

@rustbot modify labels: +A-diagnostics

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 8, 2020
@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 8, 2020
@Centril
Copy link
Contributor

Centril commented Apr 8, 2020

Duplicate of #70047.

@Centril Centril closed this as completed Apr 8, 2020
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-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants