Skip to content

Commit 5676823

Browse files
oli-obkCentril
authored andcommitted
Add a few tests around raw pointers and interior mutability
1 parent 38a9040 commit 5676823

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

src/test/ui/consts/std/cell.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::cell::*;
2+
3+
// not ok, because this would create a silent constant with interior mutability.
4+
// the rules could be relaxed in the future
5+
static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
6+
//~^ ERROR cannot borrow a constant which may contain interior mutability
7+
8+
static FOO3: Wrap<Cell<u32>> = Wrap(Cell::new(42));
9+
// ok
10+
static FOO4: Wrap<*mut u32> = Wrap(FOO3.0.as_ptr());
11+
12+
// not ok, because the `as_ptr` call takes a reference to a type with interior mutability
13+
// which is not allowed in constants
14+
const FOO2: *mut u32 = Cell::new(42).as_ptr();
15+
//~^ ERROR cannot borrow a constant which may contain interior mutability
16+
17+
struct IMSafeTrustMe(UnsafeCell<u32>);
18+
unsafe impl Send for IMSafeTrustMe {}
19+
unsafe impl Sync for IMSafeTrustMe {}
20+
21+
static BAR: IMSafeTrustMe = IMSafeTrustMe(UnsafeCell::new(5));
22+
23+
24+
struct Wrap<T>(T);
25+
unsafe impl<T> Send for Wrap<T> {}
26+
unsafe impl<T> Sync for Wrap<T> {}
27+
28+
static BAR_PTR: Wrap<*mut u32> = Wrap(BAR.0.get());
29+
30+
fn main() {}

src/test/ui/consts/std/cell.stderr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2+
--> $DIR/cell.rs:5:35
3+
|
4+
LL | static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
5+
| ^^^^^^^^^^^^^
6+
7+
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
8+
--> $DIR/cell.rs:14:24
9+
|
10+
LL | const FOO2: *mut u32 = Cell::new(42).as_ptr();
11+
| ^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0492`.

src/test/ui/consts/std/char.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-pass
2+
3+
static X: bool = 'a'.is_ascii();
4+
static Y: bool = 'ä'.is_ascii();
5+
6+
fn main() {
7+
assert!(X);
8+
assert!(!Y);
9+
}

src/test/ui/consts/std/iter.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-pass
2+
3+
const I: std::iter::Empty<u32> = std::iter::empty();
4+
5+
fn main() {
6+
for i in I {
7+
panic!("magical value creation: {}", i);
8+
}
9+
}

src/test/ui/consts/std/slice.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// compile-pass
2+
3+
struct Wrap<T>(T);
4+
unsafe impl<T> Send for Wrap<T> {}
5+
unsafe impl<T> Sync for Wrap<T> {}
6+
7+
static FOO: Wrap<*const u32> = Wrap([42, 44, 46].as_ptr());
8+
static BAR: Wrap<*const u8> = Wrap("hello".as_ptr());
9+
10+
fn main() {}

0 commit comments

Comments
 (0)