Skip to content

Commit bb6e54d

Browse files
committed
Added and updated tests to enable/disable overflow checks.
1 parent 5cd4b4f commit bb6e54d

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/test/ui/issue-45697-1.rs

+35
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 -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+
}

src/test/ui/issue-45697-1.stderr

+18
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-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+

src/test/ui/issue-45697.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Test that assignments to an `&mut` pointer which is found in a
1212
// borrowed (but otherwise non-aliasable) location is illegal.
1313

14-
// compile-flags: -Z emit-end-regions -Z borrowck=compare
14+
// compile-flags: -Z emit-end-regions -Z borrowck=compare -C overflow-checks=off
1515

1616
struct S<'a> {
1717
pointer: &'a mut isize

0 commit comments

Comments
 (0)