Skip to content

Commit 4684649

Browse files
committed
Add unit test for case that didn't seem to be covered in existing UI tests
(since I made this mistake at first but the tests didn't catch it): we should not suggest adding `mut` to a reassigned `ref` or `ref mut` binding. (The Rust language, since at least 1.0, does not have `mut ref mut` or `ref mut mut` etc.)
1 parent a32fca7 commit 4684649

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/test/ui/reassign-ref-mut.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests how we behave when the user attempts to mutate an immutable
12+
// binding that was introduced by either `ref` or `ref mut`
13+
// patterns.
14+
//
15+
// Such bindings cannot be made mutable via the mere addition of the
16+
// `mut` keyword, and thus we want to check that the compiler does not
17+
// suggest doing so.
18+
19+
fn main() {
20+
let (mut one_two, mut three_four) = ((1, 2), (3, 4));
21+
let &mut (ref a, ref mut b) = &mut one_two;
22+
a = &three_four.0;
23+
//~^ ERROR cannot assign twice to immutable variable `a` [E0384]
24+
b = &mut three_four.1;
25+
//~^ ERROR cannot assign twice to immutable variable `b` [E0384]
26+
}

src/test/ui/reassign-ref-mut.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0384]: cannot assign twice to immutable variable `a`
2+
--> $DIR/reassign-ref-mut.rs:22:5
3+
|
4+
LL | let &mut (ref a, ref mut b) = &mut one_two;
5+
| ----- first assignment to `a`
6+
LL | a = &three_four.0;
7+
| ^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
8+
9+
error[E0384]: cannot assign twice to immutable variable `b`
10+
--> $DIR/reassign-ref-mut.rs:24:5
11+
|
12+
LL | let &mut (ref a, ref mut b) = &mut one_two;
13+
| --------- first assignment to `b`
14+
...
15+
LL | b = &mut three_four.1;
16+
| ^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0384`.

0 commit comments

Comments
 (0)