Skip to content

Commit fa5a3c3

Browse files
committed
Don't parse mut a @ b as mut a @ mut b
1 parent 64ea639 commit fa5a3c3

6 files changed

+82
-4
lines changed

src/librustc_parse/parser/pat.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,14 @@ impl<'a> Parser<'a> {
542542
}
543543

544544
fn visit_pat(&mut self, pat: &mut P<Pat>) {
545-
if let PatKind::Ident(BindingMode::ByValue(ref mut m @ Mutability::Not), ..) =
546-
pat.kind
547-
{
548-
*m = Mutability::Mut;
545+
if let PatKind::Ident(ref mut bm, ..) = pat.kind {
546+
if let BindingMode::ByValue(ref mut m @ Mutability::Not) = bm {
547+
*m = Mutability::Mut;
548+
}
549549
self.0 = true;
550+
// Don't recurse into the subpattern, mut on the outer
551+
// binding doesn't affect the inner bindings.
552+
return;
550553
}
551554
noop_visit_pat(pat, self);
552555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// check-pass
2+
3+
#![feature(bindings_after_at)]
4+
#![deny(unused_mut)]
5+
6+
fn main() {
7+
let mut is_mut @ not_mut = 42;
8+
&mut is_mut;
9+
&not_mut;
10+
let not_mut @ mut is_mut = 42;
11+
&mut is_mut;
12+
&not_mut;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(bindings_after_at)]
2+
3+
fn main() {
4+
let mut is_mut @ not_mut = 42;
5+
&mut is_mut;
6+
&mut not_mut;
7+
//~^ ERROR cannot borrow
8+
9+
let not_mut @ mut is_mut = 42;
10+
&mut is_mut;
11+
&mut not_mut;
12+
//~^ ERROR cannot borrow
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0596]: cannot borrow `not_mut` as mutable, as it is not declared as mutable
2+
--> $DIR/nested-binding-modes-mut.rs:6:5
3+
|
4+
LL | let mut is_mut @ not_mut = 42;
5+
| ------- help: consider changing this to be mutable: `mut not_mut`
6+
LL | &mut is_mut;
7+
LL | &mut not_mut;
8+
| ^^^^^^^^^^^^ cannot borrow as mutable
9+
10+
error[E0596]: cannot borrow `not_mut` as mutable, as it is not declared as mutable
11+
--> $DIR/nested-binding-modes-mut.rs:11:5
12+
|
13+
LL | let not_mut @ mut is_mut = 42;
14+
| -------------------- help: consider changing this to be mutable: `mut not_mut`
15+
LL | &mut is_mut;
16+
LL | &mut not_mut;
17+
| ^^^^^^^^^^^^ cannot borrow as mutable
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0596`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(bindings_after_at)]
2+
3+
fn main() {
4+
let ref is_ref @ is_val = 42;
5+
*is_ref;
6+
*is_val;
7+
//~^ ERROR cannot be dereferenced
8+
9+
let is_val @ ref is_ref = 42;
10+
*is_ref;
11+
*is_val;
12+
//~^ ERROR cannot be dereferenced
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0614]: type `{integer}` cannot be dereferenced
2+
--> $DIR/nested-binding-modes-ref.rs:6:5
3+
|
4+
LL | *is_val;
5+
| ^^^^^^^
6+
7+
error[E0614]: type `{integer}` cannot be dereferenced
8+
--> $DIR/nested-binding-modes-ref.rs:11:5
9+
|
10+
LL | *is_val;
11+
| ^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0614`.

0 commit comments

Comments
 (0)