File tree 3 files changed +54
-1
lines changed
3 files changed +54
-1
lines changed Original file line number Diff line number Diff line change
1
+ // Copyright 2012 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
+ // Test that assignments to an `&mut` pointer which is found in a
12
+ // borrowed (but otherwise non-aliasable) location is illegal.
13
+
14
+ // compile-flags: -Z emit-end-regions -Z borrowck=compare -C overflow-checks=on
15
+
16
+ struct S < ' a > {
17
+ pointer : & ' a mut isize
18
+ }
19
+
20
+ fn copy_borrowed_ptr < ' a > ( p : & ' a mut S < ' a > ) -> S < ' a > {
21
+ S { pointer : & mut * p. pointer }
22
+ }
23
+
24
+ fn main ( ) {
25
+ let mut x = 1 ;
26
+
27
+ {
28
+ let mut y = S { pointer : & mut x } ;
29
+ let z = copy_borrowed_ptr ( & mut y) ;
30
+ * y. pointer += 1 ;
31
+ //~^ ERROR cannot assign to `*y.pointer` because it is borrowed (Ast) [E0506]
32
+ //~| ERROR cannot use `*y.pointer` because it was mutably borrowed (Mir) [E0503]
33
+ * z. pointer += 1 ;
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ error[E0506]: cannot assign to `*y.pointer` because it is borrowed (Ast)
2
+ --> $DIR/issue-45697-1.rs:30:9
3
+ |
4
+ 29 | let z = copy_borrowed_ptr(&mut y);
5
+ | - borrow of `*y.pointer` occurs here
6
+ 30 | *y.pointer += 1;
7
+ | ^^^^^^^^^^^^^^^ assignment to borrowed `*y.pointer` occurs here
8
+
9
+ error[E0503]: cannot use `*y.pointer` because it was mutably borrowed (Mir)
10
+ --> $DIR/issue-45697-1.rs:30:9
11
+ |
12
+ 29 | let z = copy_borrowed_ptr(&mut y);
13
+ | ------ borrow of `y` occurs here
14
+ 30 | *y.pointer += 1;
15
+ | ^^^^^^^^^^^^^^^ use of borrowed `y`
16
+
17
+ error: aborting due to 2 previous errors
18
+
Original file line number Diff line number Diff line change 11
11
// Test that assignments to an `&mut` pointer which is found in a
12
12
// borrowed (but otherwise non-aliasable) location is illegal.
13
13
14
- // compile-flags: -Z emit-end-regions -Z borrowck=compare
14
+ // compile-flags: -Z emit-end-regions -Z borrowck=compare -C overflow-checks=off
15
15
16
16
struct S < ' a > {
17
17
pointer : & ' a mut isize
You can’t perform that action at this time.
0 commit comments