Skip to content

Commit 0bf3ac7

Browse files
committed
reassign-ref-mut
1 parent 33a7363 commit 0bf3ac7

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
// Tests how we behave when the user attempts to mutate an immutable
2-
// binding that was introduced by either `ref` or `ref mut`
3-
// patterns.
4-
//
5-
// Such bindings cannot be made mutable via the mere addition of the
6-
// `mut` keyword, and thus we want to check that the compiler does not
7-
// suggest doing so.
1+
//! Tests how we behave when the user attempts to mutate an immutable
2+
//! binding that was introduced by either `ref` or `ref mut`
3+
//! patterns.
4+
//!
5+
//! Such bindings cannot be made mutable via the mere addition of the
6+
//! `mut` keyword, and thus we want to check that the compiler does not
7+
//! suggest doing so.
88
99
fn main() {
1010
let (mut one_two, mut three_four) = ((1, 2), (3, 4));
11+
12+
// Bind via pattern:
13+
// - `a` as immutable reference (`ref`)
14+
// - `b` as mutable reference (`ref mut`)
1115
let &mut (ref a, ref mut b) = &mut one_two;
16+
17+
// Attempt to reassign immutable `ref`-bound variable
1218
a = &three_four.0;
13-
//~^ ERROR cannot assign twice to immutable variable `a` [E0384]
19+
//~^ ERROR cannot assign twice to immutable variable `a`
20+
21+
// Attempt to reassign mutable `ref mut`-bound variable
1422
b = &mut three_four.1;
15-
//~^ ERROR cannot assign twice to immutable variable `b` [E0384]
23+
//~^ ERROR cannot assign twice to immutable variable `b`
1624
}

tests/ui/pattern/move-ref-patterns/pattern-ref-bindings-reassignment.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
error[E0384]: cannot assign twice to immutable variable `a`
2-
--> $DIR/reassign-ref-mut.rs:12:5
2+
--> $DIR/pattern-ref-bindings-reassignment.rs:18:5
33
|
44
LL | let &mut (ref a, ref mut b) = &mut one_two;
55
| ----- first assignment to `a`
6+
...
67
LL | a = &three_four.0;
78
| ^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
89

910
error[E0384]: cannot assign twice to immutable variable `b`
10-
--> $DIR/reassign-ref-mut.rs:14:5
11+
--> $DIR/pattern-ref-bindings-reassignment.rs:22:5
1112
|
1213
LL | let &mut (ref a, ref mut b) = &mut one_two;
1314
| --------- first assignment to `b`

0 commit comments

Comments
 (0)