summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2014-12-05 15:56:25 -0800
committerHuon Wilson <dbau.pp+github@gmail.com>2015-01-05 16:14:17 +1100
commitbf6c007760169e9c382d3700fd1cdd20037e4343 (patch)
tree248a48e3811afdf8908c5ebf260d67eade27aa92 /src/libsyntax/parse
parent5773bdefff2e47cc007f5cc2af3f80b30303d45a (diff)
downloadrust-bf6c007760169e9c382d3700fd1cdd20037e4343.tar.gz
rust-bf6c007760169e9c382d3700fd1cdd20037e4343.zip
Change `&` pat to only work with &T, and `&mut` with &mut T.
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.
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/parser.rs9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 37ac86a3324..d9183ef0c49 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -3357,11 +3357,16 @@ impl<'a> Parser<'a> {
             })
           }
           token::BinOp(token::And) | token::AndAnd => {
-            // parse &pat
+            // parse &pat and &mut pat
             let lo = self.span.lo;
             self.expect_and();
+            let mutability = if self.eat_keyword(keywords::Mut) {
+                ast::MutMutable
+            } else {
+                ast::MutImmutable
+            };
             let sub = self.parse_pat();
-            pat = PatRegion(sub);
+            pat = PatRegion(sub, mutability);
             hi = self.last_span.hi;
             return P(ast::Pat {
                 id: ast::DUMMY_NODE_ID,