Skip to content

Commit 6dcc789

Browse files
committed
Add more tests for raw_ref_op
1 parent 1593194 commit 6dcc789

31 files changed

+590
-168
lines changed

src/librustc_error_codes/error_codes/E0745.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn temp_address() {
1111

1212
To avoid the error, first bind the temporary to a named local variable.
1313

14-
```ignore (not yet implemented)
14+
```
1515
# #![feature(raw_ref_op)]
1616
fn temp_address() {
1717
let val = 2;

src/test/pretty/raw-address-of.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// pp-exact
2+
#![feature(raw_ref_op)]
3+
4+
const C_PTR: () = { let a = 1; &raw const a; };
5+
static S_PTR: () = { let b = false; &raw const b; };
6+
7+
fn main() {
8+
let x = 123;
9+
let mut y = 345;
10+
let c_p = &raw const x;
11+
let parens = unsafe { *(&raw mut (y)) };
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(raw_ref_op)]
2+
3+
fn address_of_shared() {
4+
let mut x = 0;
5+
let y = &x;
6+
7+
let q = &raw mut x; //~ ERROR cannot borrow
8+
9+
drop(y);
10+
}
11+
12+
fn address_of_mutably_borrowed() {
13+
let mut x = 0;
14+
let y = &mut x;
15+
16+
let p = &raw const x; //~ ERROR cannot borrow
17+
let q = &raw mut x; //~ ERROR cannot borrow
18+
19+
drop(y);
20+
}
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
2+
--> $DIR/borrow-raw-address-of-borrowed.rs:7:13
3+
|
4+
LL | let y = &x;
5+
| -- immutable borrow occurs here
6+
LL |
7+
LL | let q = &raw mut x;
8+
| ^^^^^^^^^^ mutable borrow occurs here
9+
LL |
10+
LL | drop(y);
11+
| - immutable borrow later used here
12+
13+
error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
14+
--> $DIR/borrow-raw-address-of-borrowed.rs:16:13
15+
|
16+
LL | let y = &mut x;
17+
| ------ mutable borrow occurs here
18+
LL |
19+
LL | let p = &raw const x;
20+
| ^^^^^^^^^^^^ immutable borrow occurs here
21+
...
22+
LL | drop(y);
23+
| - mutable borrow later used here
24+
25+
error[E0499]: cannot borrow `x` as mutable more than once at a time
26+
--> $DIR/borrow-raw-address-of-borrowed.rs:17:13
27+
|
28+
LL | let y = &mut x;
29+
| ------ first mutable borrow occurs here
30+
...
31+
LL | let q = &raw mut x;
32+
| ^^^^^^^^^^ second mutable borrow occurs here
33+
LL |
34+
LL | drop(y);
35+
| - first borrow later used here
36+
37+
error: aborting due to 3 previous errors
38+
39+
Some errors have detailed explanations: E0499, E0502.
40+
For more information about an error, try `rustc --explain E0499`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// check-pass
2+
3+
#![feature(raw_ref_op)]
4+
5+
fn raw_reborrow() {
6+
let x = &0;
7+
let y = &mut 0;
8+
9+
let p = &raw const *x;
10+
let r = &raw const *y;
11+
let s = &raw mut *y;
12+
}
13+
14+
unsafe fn raw_reborrow_of_raw() {
15+
let x = &0 as *const i32;
16+
let y = &mut 0 as *mut i32;
17+
18+
let p = &raw const *x;
19+
let r = &raw const *y;
20+
let s = &raw mut *y;
21+
}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Check that `&raw mut` cannot be used to turn a `&T` into a `*mut T`.
2+
3+
#![feature(raw_ref_op)]
4+
5+
fn raw_reborrow() {
6+
let x = &0;
7+
8+
let q = &raw mut *x; //~ ERROR cannot borrow
9+
}
10+
11+
unsafe fn raw_reborrow_of_raw() {
12+
let x = &0 as *const i32;
13+
14+
let q = &raw mut *x; //~ ERROR cannot borrow
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
2+
--> $DIR/borrow-raw-address-of-deref-mutability.rs:8:13
3+
|
4+
LL | let x = &0;
5+
| -- help: consider changing this to be a mutable reference: `&mut 0`
6+
LL |
7+
LL | let q = &raw mut *x;
8+
| ^^^^^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
9+
10+
error[E0596]: cannot borrow `*x` as mutable, as it is behind a `*const` pointer
11+
--> $DIR/borrow-raw-address-of-deref-mutability.rs:14:13
12+
|
13+
LL | let x = &0 as *const i32;
14+
| -- help: consider changing this to be a mutable pointer: `&mut 0`
15+
LL |
16+
LL | let q = &raw mut *x;
17+
| ^^^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0596`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// check-pass
2+
3+
#![feature(raw_ref_op)]
4+
5+
fn mutable_address_of() {
6+
let mut x = 0;
7+
let y = &raw mut x;
8+
}
9+
10+
fn mutable_address_of_closure() {
11+
let mut x = 0;
12+
let mut f = || {
13+
let y = &raw mut x;
14+
};
15+
f();
16+
}
17+
18+
fn const_address_of_closure() {
19+
let x = 0;
20+
let f = || {
21+
let y = &raw const x;
22+
};
23+
f();
24+
}
25+
26+
fn make_fn<F: Fn()>(f: F) -> F { f }
27+
28+
fn const_address_of_fn_closure() {
29+
let x = 0;
30+
let f = make_fn(|| {
31+
let y = &raw const x;
32+
});
33+
f();
34+
}
35+
36+
fn const_address_of_fn_closure_move() {
37+
let x = 0;
38+
let f = make_fn(move || {
39+
let y = &raw const x;
40+
});
41+
f();
42+
}
43+
44+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![feature(raw_ref_op)]
2+
3+
fn mutable_address_of() {
4+
let x = 0;
5+
let y = &raw mut x; //~ ERROR cannot borrow
6+
}
7+
8+
fn mutable_address_of_closure() {
9+
let x = 0;
10+
let mut f = || {
11+
let y = &raw mut x; //~ ERROR cannot borrow
12+
};
13+
f();
14+
}
15+
16+
fn mutable_address_of_imm_closure() {
17+
let mut x = 0;
18+
let f = || {
19+
let y = &raw mut x;
20+
};
21+
f(); //~ ERROR cannot borrow
22+
}
23+
24+
fn make_fn<F: Fn()>(f: F) -> F { f }
25+
26+
fn mutable_address_of_fn_closure() {
27+
let mut x = 0;
28+
let f = make_fn(|| {
29+
let y = &raw mut x; //~ ERROR cannot borrow
30+
});
31+
f();
32+
}
33+
34+
fn mutable_address_of_fn_closure_move() {
35+
let mut x = 0;
36+
let f = make_fn(move || {
37+
let y = &raw mut x; //~ ERROR cannot borrow
38+
});
39+
f();
40+
}
41+
42+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
2+
--> $DIR/borrow-raw-address-of-mutability.rs:5:13
3+
|
4+
LL | let x = 0;
5+
| - help: consider changing this to be mutable: `mut x`
6+
LL | let y = &raw mut x;
7+
| ^^^^^^^^^^ cannot borrow as mutable
8+
9+
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
10+
--> $DIR/borrow-raw-address-of-mutability.rs:11:17
11+
|
12+
LL | let x = 0;
13+
| - help: consider changing this to be mutable: `mut x`
14+
LL | let mut f = || {
15+
LL | let y = &raw mut x;
16+
| ^^^^^^^^^^ cannot borrow as mutable
17+
18+
error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
19+
--> $DIR/borrow-raw-address-of-mutability.rs:21:5
20+
|
21+
LL | let f = || {
22+
| - help: consider changing this to be mutable: `mut f`
23+
...
24+
LL | f();
25+
| ^ cannot borrow as mutable
26+
27+
error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
28+
--> $DIR/borrow-raw-address-of-mutability.rs:29:17
29+
|
30+
LL | let y = &raw mut x;
31+
| ^^^^^^^^^^ cannot borrow as mutable
32+
|
33+
help: consider changing this to accept closures that implement `FnMut`
34+
--> $DIR/borrow-raw-address-of-mutability.rs:28:21
35+
|
36+
LL | let f = make_fn(|| {
37+
| _____________________^
38+
LL | | let y = &raw mut x;
39+
LL | | });
40+
| |_____^
41+
42+
error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
43+
--> $DIR/borrow-raw-address-of-mutability.rs:37:17
44+
|
45+
LL | let y = &raw mut x;
46+
| ^^^^^^^^^^ cannot borrow as mutable
47+
|
48+
help: consider changing this to accept closures that implement `FnMut`
49+
--> $DIR/borrow-raw-address-of-mutability.rs:36:21
50+
|
51+
LL | let f = make_fn(move || {
52+
| _____________________^
53+
LL | | let y = &raw mut x;
54+
LL | | });
55+
| |_____^
56+
57+
error: aborting due to 5 previous errors
58+
59+
For more information about this error, try `rustc --explain E0596`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(raw_ref_op)]
2+
3+
use std::cell::Cell;
4+
5+
const A: () = { let x = Cell::new(2); &raw const x; }; //~ ERROR interior mutability
6+
7+
static B: () = { let x = Cell::new(2); &raw const x; }; //~ ERROR interior mutability
8+
9+
static mut C: () = { let x = Cell::new(2); &raw const x; }; //~ ERROR interior mutability
10+
11+
const fn foo() {
12+
let x = Cell::new(0);
13+
let y = &raw const x; //~ ERROR interior mutability
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2+
--> $DIR/const-address-of-interior-mut.rs:5:39
3+
|
4+
LL | const A: () = { let x = Cell::new(2); &raw const x; };
5+
| ^^^^^^^^^^^^
6+
7+
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
8+
--> $DIR/const-address-of-interior-mut.rs:7:40
9+
|
10+
LL | static B: () = { let x = Cell::new(2); &raw const x; };
11+
| ^^^^^^^^^^^^
12+
13+
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
14+
--> $DIR/const-address-of-interior-mut.rs:9:44
15+
|
16+
LL | static mut C: () = { let x = Cell::new(2); &raw const x; };
17+
| ^^^^^^^^^^^^
18+
19+
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
20+
--> $DIR/const-address-of-interior-mut.rs:13:13
21+
|
22+
LL | let y = &raw const x;
23+
| ^^^^^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0492`.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(raw_ref_op)]
2+
3+
const A: () = { let mut x = 2; &raw mut x; }; //~ ERROR `&raw mut` is not allowed
4+
5+
static B: () = { let mut x = 2; &raw mut x; }; //~ ERROR `&raw mut` is not allowed
6+
7+
static mut C: () = { let mut x = 2; &raw mut x; }; //~ ERROR `&raw mut` is not allowed
8+
9+
const fn foo() {
10+
let mut x = 0;
11+
let y = &raw mut x; //~ ERROR `&raw mut` is not allowed
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0658]: `&raw mut` is not allowed in constants
2+
--> $DIR/const-address-of-mut.rs:3:32
3+
|
4+
LL | const A: () = { let mut x = 2; &raw mut x; };
5+
| ^^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
8+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
9+
10+
error[E0658]: `&raw mut` is not allowed in statics
11+
--> $DIR/const-address-of-mut.rs:5:33
12+
|
13+
LL | static B: () = { let mut x = 2; &raw mut x; };
14+
| ^^^^^^^^^^
15+
|
16+
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
17+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
18+
19+
error[E0658]: `&raw mut` is not allowed in statics
20+
--> $DIR/const-address-of-mut.rs:7:37
21+
|
22+
LL | static mut C: () = { let mut x = 2; &raw mut x; };
23+
| ^^^^^^^^^^
24+
|
25+
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
26+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
27+
28+
error[E0658]: `&raw mut` is not allowed in constant functions
29+
--> $DIR/const-address-of-mut.rs:11:13
30+
|
31+
LL | let y = &raw mut x;
32+
| ^^^^^^^^^^
33+
|
34+
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
35+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
36+
37+
error: aborting due to 4 previous errors
38+
39+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)