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

Implement match ergonomics (binding modes) in type inference #888

Closed
flodiebold opened this issue Feb 24, 2019 · 0 comments
Closed

Implement match ergonomics (binding modes) in type inference #888

flodiebold opened this issue Feb 24, 2019 · 0 comments
Labels

Comments

@flodiebold
Copy link
Member

E.g.

let x = Some(1);
match &x {
    Some(n) => n, // n should be &i32
}
match &mut x {
    Some(n) => n, // n should be &mut i32
}

and so on. We currently just infer wrong types in this situation. To fix this, when walking through patterns with infer_pat, we need to keep track of the 'binding mode' and apply it when creating bindings. This is also explained in the rust reference; and I think rustc handles it here.

bors bot added a commit that referenced this issue Mar 17, 2019
982: Implement BindingMode for pattern matching. r=flodiebold a=mjkillough

Implement `BindingMode` for pattern matching, so that types can be
correctly inferred using match ergonomics. The binding mode defaults to
`Move` (referred to as 'BindingMode::BindByValue` in rustc), and is
updated by automatic dereferencing of the value being matched.

Fixes #888.

 - [Binding modes in The Reference](https://doc.rust-lang.org/reference/patterns.html#binding-modes)
 - [`rustc` implementation](https://github.com/rust-lang/rust/blob/e17c48e2f21eefd59748e364234efc7037a3ec96/src/librustc_typeck/check/_match.rs#L77) (and [definition of `BindingMode`](https://github.com/rust-lang/rust/blob/e957ed9d10ec589bdd523b88b4b44c41b1ecf763/src/librustc/ty/binding.rs))
 - [Match Ergonomics RFC](https://github.com/rust-lang/rfcs/blob/master/text/2005-match-ergonomics.md#binding-mode-rules)

Co-authored-by: Michael Killough <[email protected]>
@bors bors bot closed this as completed in #982 Mar 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant