Skip to content

Commit

Permalink
Add more tests for raw_ref_op
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Dec 15, 2019
1 parent 5eb17c3 commit 68fed49
Show file tree
Hide file tree
Showing 31 changed files with 590 additions and 168 deletions.
2 changes: 1 addition & 1 deletion src/librustc_error_codes/error_codes/E0745.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn temp_address() {

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

```ignore (not yet implemented)
```
# #![feature(raw_ref_op)]
fn temp_address() {
let val = 2;
Expand Down
12 changes: 12 additions & 0 deletions src/test/pretty/raw-address-of.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// pp-exact
#![feature(raw_ref_op)]

const C_PTR: () = { let a = 1; &raw const a; };
static S_PTR: () = { let b = false; &raw const b; };

fn main() {
let x = 123;
let mut y = 345;
let c_p = &raw const x;
let parens = unsafe { *(&raw mut (y)) };
}
22 changes: 22 additions & 0 deletions src/test/ui/borrowck/borrow-raw-address-of-borrowed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![feature(raw_ref_op)]

fn address_of_shared() {
let mut x = 0;
let y = &x;

let q = &raw mut x; //~ ERROR cannot borrow

drop(y);
}

fn address_of_mutably_borrowed() {
let mut x = 0;
let y = &mut x;

let p = &raw const x; //~ ERROR cannot borrow
let q = &raw mut x; //~ ERROR cannot borrow

drop(y);
}

fn main() {}
40 changes: 40 additions & 0 deletions src/test/ui/borrowck/borrow-raw-address-of-borrowed.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable
--> $DIR/borrow-raw-address-of-borrowed.rs:7:13
|
LL | let y = &x;
| -- immutable borrow occurs here
LL |
LL | let q = &raw mut x;
| ^^^^^^^^^^ mutable borrow occurs here
LL |
LL | drop(y);
| - immutable borrow later used here

error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
--> $DIR/borrow-raw-address-of-borrowed.rs:16:13
|
LL | let y = &mut x;
| ------ mutable borrow occurs here
LL |
LL | let p = &raw const x;
| ^^^^^^^^^^^^ immutable borrow occurs here
...
LL | drop(y);
| - mutable borrow later used here

error[E0499]: cannot borrow `x` as mutable more than once at a time
--> $DIR/borrow-raw-address-of-borrowed.rs:17:13
|
LL | let y = &mut x;
| ------ first mutable borrow occurs here
...
LL | let q = &raw mut x;
| ^^^^^^^^^^ second mutable borrow occurs here
LL |
LL | drop(y);
| - first borrow later used here

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0499, E0502.
For more information about an error, try `rustc --explain E0499`.
23 changes: 23 additions & 0 deletions src/test/ui/borrowck/borrow-raw-address-of-deref-mutability-ok.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// check-pass

#![feature(raw_ref_op)]

fn raw_reborrow() {
let x = &0;
let y = &mut 0;

let p = &raw const *x;
let r = &raw const *y;
let s = &raw mut *y;
}

unsafe fn raw_reborrow_of_raw() {
let x = &0 as *const i32;
let y = &mut 0 as *mut i32;

let p = &raw const *x;
let r = &raw const *y;
let s = &raw mut *y;
}

fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/borrowck/borrow-raw-address-of-deref-mutability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Check that `&raw mut` cannot be used to turn a `&T` into a `*mut T`.

#![feature(raw_ref_op)]

fn raw_reborrow() {
let x = &0;

let q = &raw mut *x; //~ ERROR cannot borrow
}

unsafe fn raw_reborrow_of_raw() {
let x = &0 as *const i32;

let q = &raw mut *x; //~ ERROR cannot borrow
}

fn main() {}
21 changes: 21 additions & 0 deletions src/test/ui/borrowck/borrow-raw-address-of-deref-mutability.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0596]: cannot borrow `*x` as mutable, as it is behind a `&` reference
--> $DIR/borrow-raw-address-of-deref-mutability.rs:8:13
|
LL | let x = &0;
| -- help: consider changing this to be a mutable reference: `&mut 0`
LL |
LL | let q = &raw mut *x;
| ^^^^^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be borrowed as mutable

error[E0596]: cannot borrow `*x` as mutable, as it is behind a `*const` pointer
--> $DIR/borrow-raw-address-of-deref-mutability.rs:14:13
|
LL | let x = &0 as *const i32;
| -- help: consider changing this to be a mutable pointer: `&mut 0`
LL |
LL | let q = &raw mut *x;
| ^^^^^^^^^^^ `x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0596`.
44 changes: 44 additions & 0 deletions src/test/ui/borrowck/borrow-raw-address-of-mutability-ok.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// check-pass

#![feature(raw_ref_op)]

fn mutable_address_of() {
let mut x = 0;
let y = &raw mut x;
}

fn mutable_address_of_closure() {
let mut x = 0;
let mut f = || {
let y = &raw mut x;
};
f();
}

fn const_address_of_closure() {
let x = 0;
let f = || {
let y = &raw const x;
};
f();
}

fn make_fn<F: Fn()>(f: F) -> F { f }

fn const_address_of_fn_closure() {
let x = 0;
let f = make_fn(|| {
let y = &raw const x;
});
f();
}

fn const_address_of_fn_closure_move() {
let x = 0;
let f = make_fn(move || {
let y = &raw const x;
});
f();
}

fn main() {}
42 changes: 42 additions & 0 deletions src/test/ui/borrowck/borrow-raw-address-of-mutability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![feature(raw_ref_op)]

fn mutable_address_of() {
let x = 0;
let y = &raw mut x; //~ ERROR cannot borrow
}

fn mutable_address_of_closure() {
let x = 0;
let mut f = || {
let y = &raw mut x; //~ ERROR cannot borrow
};
f();
}

fn mutable_address_of_imm_closure() {
let mut x = 0;
let f = || {
let y = &raw mut x;
};
f(); //~ ERROR cannot borrow
}

fn make_fn<F: Fn()>(f: F) -> F { f }

fn mutable_address_of_fn_closure() {
let mut x = 0;
let f = make_fn(|| {
let y = &raw mut x; //~ ERROR cannot borrow
});
f();
}

fn mutable_address_of_fn_closure_move() {
let mut x = 0;
let f = make_fn(move || {
let y = &raw mut x; //~ ERROR cannot borrow
});
f();
}

fn main() {}
59 changes: 59 additions & 0 deletions src/test/ui/borrowck/borrow-raw-address-of-mutability.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/borrow-raw-address-of-mutability.rs:5:13
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
LL | let y = &raw mut x;
| ^^^^^^^^^^ cannot borrow as mutable

error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> $DIR/borrow-raw-address-of-mutability.rs:11:17
|
LL | let x = 0;
| - help: consider changing this to be mutable: `mut x`
LL | let mut f = || {
LL | let y = &raw mut x;
| ^^^^^^^^^^ cannot borrow as mutable

error[E0596]: cannot borrow `f` as mutable, as it is not declared as mutable
--> $DIR/borrow-raw-address-of-mutability.rs:21:5
|
LL | let f = || {
| - help: consider changing this to be mutable: `mut f`
...
LL | f();
| ^ cannot borrow as mutable

error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-raw-address-of-mutability.rs:29:17
|
LL | let y = &raw mut x;
| ^^^^^^^^^^ cannot borrow as mutable
|
help: consider changing this to accept closures that implement `FnMut`
--> $DIR/borrow-raw-address-of-mutability.rs:28:21
|
LL | let f = make_fn(|| {
| _____________________^
LL | | let y = &raw mut x;
LL | | });
| |_____^

error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
--> $DIR/borrow-raw-address-of-mutability.rs:37:17
|
LL | let y = &raw mut x;
| ^^^^^^^^^^ cannot borrow as mutable
|
help: consider changing this to accept closures that implement `FnMut`
--> $DIR/borrow-raw-address-of-mutability.rs:36:21
|
LL | let f = make_fn(move || {
| _____________________^
LL | | let y = &raw mut x;
LL | | });
| |_____^

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0596`.
16 changes: 16 additions & 0 deletions src/test/ui/consts/const-address-of-interior-mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(raw_ref_op)]

use std::cell::Cell;

const A: () = { let x = Cell::new(2); &raw const x; }; //~ ERROR interior mutability

static B: () = { let x = Cell::new(2); &raw const x; }; //~ ERROR interior mutability

static mut C: () = { let x = Cell::new(2); &raw const x; }; //~ ERROR interior mutability

const fn foo() {
let x = Cell::new(0);
let y = &raw const x; //~ ERROR interior mutability
}

fn main() {}
27 changes: 27 additions & 0 deletions src/test/ui/consts/const-address-of-interior-mut.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/const-address-of-interior-mut.rs:5:39
|
LL | const A: () = { let x = Cell::new(2); &raw const x; };
| ^^^^^^^^^^^^

error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/const-address-of-interior-mut.rs:7:40
|
LL | static B: () = { let x = Cell::new(2); &raw const x; };
| ^^^^^^^^^^^^

error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/const-address-of-interior-mut.rs:9:44
|
LL | static mut C: () = { let x = Cell::new(2); &raw const x; };
| ^^^^^^^^^^^^

error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/const-address-of-interior-mut.rs:13:13
|
LL | let y = &raw const x;
| ^^^^^^^^^^^^

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0492`.
14 changes: 14 additions & 0 deletions src/test/ui/consts/const-address-of-mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![feature(raw_ref_op)]

const A: () = { let mut x = 2; &raw mut x; }; //~ ERROR `&raw mut` is not allowed

static B: () = { let mut x = 2; &raw mut x; }; //~ ERROR `&raw mut` is not allowed

static mut C: () = { let mut x = 2; &raw mut x; }; //~ ERROR `&raw mut` is not allowed

const fn foo() {
let mut x = 0;
let y = &raw mut x; //~ ERROR `&raw mut` is not allowed
}

fn main() {}
39 changes: 39 additions & 0 deletions src/test/ui/consts/const-address-of-mut.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
error[E0658]: `&raw mut` is not allowed in constants
--> $DIR/const-address-of-mut.rs:3:32
|
LL | const A: () = { let mut x = 2; &raw mut x; };
| ^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable

error[E0658]: `&raw mut` is not allowed in statics
--> $DIR/const-address-of-mut.rs:5:33
|
LL | static B: () = { let mut x = 2; &raw mut x; };
| ^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable

error[E0658]: `&raw mut` is not allowed in statics
--> $DIR/const-address-of-mut.rs:7:37
|
LL | static mut C: () = { let mut x = 2; &raw mut x; };
| ^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable

error[E0658]: `&raw mut` is not allowed in constant functions
--> $DIR/const-address-of-mut.rs:11:13
|
LL | let y = &raw mut x;
| ^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0658`.
Loading

0 comments on commit 68fed49

Please sign in to comment.