Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a few tests around raw pointers and interior mutability
- Loading branch information
Showing
5 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::cell::*; | ||
|
||
// not ok, because this would create a silent constant with interior mutability. | ||
// the rules could be relaxed in the future | ||
static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr()); | ||
//~^ ERROR cannot borrow a constant which may contain interior mutability | ||
|
||
static FOO3: Wrap<Cell<u32>> = Wrap(Cell::new(42)); | ||
// ok | ||
static FOO4: Wrap<*mut u32> = Wrap(FOO3.0.as_ptr()); | ||
|
||
// not ok, because the `as_ptr` call takes a reference to a type with interior mutability | ||
// which is not allowed in constants | ||
const FOO2: *mut u32 = Cell::new(42).as_ptr(); | ||
//~^ ERROR cannot borrow a constant which may contain interior mutability | ||
|
||
struct IMSafeTrustMe(UnsafeCell<u32>); | ||
unsafe impl Send for IMSafeTrustMe {} | ||
unsafe impl Sync for IMSafeTrustMe {} | ||
|
||
static BAR: IMSafeTrustMe = IMSafeTrustMe(UnsafeCell::new(5)); | ||
|
||
|
||
struct Wrap<T>(T); | ||
unsafe impl<T> Send for Wrap<T> {} | ||
unsafe impl<T> Sync for Wrap<T> {} | ||
|
||
static BAR_PTR: Wrap<*mut u32> = Wrap(BAR.0.get()); | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead | ||
--> $DIR/cell.rs:5:35 | ||
| | ||
LL | static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr()); | ||
| ^^^^^^^^^^^^^ | ||
|
||
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead | ||
--> $DIR/cell.rs:14:24 | ||
| | ||
LL | const FOO2: *mut u32 = Cell::new(42).as_ptr(); | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0492`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-pass | ||
|
||
static X: bool = 'a'.is_ascii(); | ||
static Y: bool = 'ä'.is_ascii(); | ||
|
||
fn main() { | ||
assert!(X); | ||
assert!(!Y); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-pass | ||
|
||
const I: std::iter::Empty<u32> = std::iter::empty(); | ||
|
||
fn main() { | ||
for i in I { | ||
panic!("magical value creation: {}", i); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// compile-pass | ||
|
||
struct Wrap<T>(T); | ||
unsafe impl<T> Send for Wrap<T> {} | ||
unsafe impl<T> Sync for Wrap<T> {} | ||
|
||
static FOO: Wrap<*const u32> = Wrap([42, 44, 46].as_ptr()); | ||
static BAR: Wrap<*const u8> = Wrap("hello".as_ptr()); | ||
|
||
fn main() {} |