Skip to content

Change & pat to only work with &T, and &mut with &mut T. #20554

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

Merged
merged 1 commit into from
Jan 6, 2015

Conversation

huonw
Copy link
Member

@huonw huonw commented Jan 5, 2015

This implements RFC 179 by making the pattern &<pat> require matching
against a variable of type &T, and introducing the pattern &mut <pat> which only works with variables of type &mut T.

The pattern &mut x currently parses as &(mut x) i.e. a pattern match
through a &T or a &mut T that binds the variable x to have type
T and to be mutable. This should be rewritten as follows, for example,

for &mut x in slice.iter() {

becomes

for &x in slice.iter() {
    let mut x = x;

Due to this, this is a

[breaking-change]

Closes #20496.

@rust-highfive
Copy link
Contributor

r? @eddyb

(rust_highfive has picked a reviewer for you, use r? to override)

@nrc nrc assigned nrc and unassigned eddyb Jan 5, 2015
let &_ = bar;
let &mut _ //~ ERROR expected `&int`, found `&mut _`
= bar;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test to check that after let &mut x = ..., x is not actually mutable (unless it is let mut &mut x, I guess).

@nrc
Copy link
Member

nrc commented Jan 5, 2015

r=me with the extra test

This implements RFC 179 by making the pattern `&<pat>` require matching
against a variable of type `&T`, and introducing the pattern `&mut
<pat>` which only works with variables of type `&mut T`.

The pattern `&mut x` currently parses as `&(mut x)` i.e. a pattern match
through a `&T` or a `&mut T` that binds the variable `x` to have type
`T` and to be mutable. This should be rewritten as follows, for example,

    for &mut x in slice.iter() {

becomes

    for &x in slice.iter() {
        let mut x = x;

Due to this, this is a

[breaking-change]

Closes rust-lang#20496.
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jan 5, 2015
Conflicts:
	src/librustc_typeck/check/_match.rs
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jan 6, 2015
Conflicts:
	src/librustc_typeck/check/_match.rs
@bors bors merged commit bf6c007 into rust-lang:master Jan 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement new &mut pattern syntax
5 participants