Skip to content

Commit dd6b005

Browse files
committed
Added test for #45697
1 parent 6272b60 commit dd6b005

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/test/ui/issue-45697.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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
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 assign to `*y.pointer` because it is borrowed (Mir) [E0506]
33+
*z.pointer += 1;
34+
}
35+
}

src/test/ui/issue-45697.stderr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0506]: cannot assign to `*y.pointer` because it is borrowed (Ast)
2+
--> $DIR/issue-45697.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[E0506]: cannot assign to `*y.pointer` because it is borrowed (Mir)
10+
--> $DIR/issue-45697.rs:30:9
11+
|
12+
29 | let z = copy_borrowed_ptr(&mut y);
13+
| ------ borrow of `*y.pointer` occurs here
14+
30 | *y.pointer += 1;
15+
| ^^^^^^^^^^^^^^^ assignment to borrowed `*y.pointer` occurs here
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)