From 56768235e18a8c02ace37489c4e033bde6118efb Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Thu, 25 Oct 2018 20:53:05 +0200 Subject: [PATCH] Add a few tests around raw pointers and interior mutability --- src/test/ui/consts/std/cell.rs | 30 ++++++++++++++++++++++++++++++ src/test/ui/consts/std/cell.stderr | 15 +++++++++++++++ src/test/ui/consts/std/char.rs | 9 +++++++++ src/test/ui/consts/std/iter.rs | 9 +++++++++ src/test/ui/consts/std/slice.rs | 10 ++++++++++ 5 files changed, 73 insertions(+) create mode 100644 src/test/ui/consts/std/cell.rs create mode 100644 src/test/ui/consts/std/cell.stderr create mode 100644 src/test/ui/consts/std/char.rs create mode 100644 src/test/ui/consts/std/iter.rs create mode 100644 src/test/ui/consts/std/slice.rs diff --git a/src/test/ui/consts/std/cell.rs b/src/test/ui/consts/std/cell.rs new file mode 100644 index 00000000000..db0bcbe2c44 --- /dev/null +++ b/src/test/ui/consts/std/cell.rs @@ -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> = 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); +unsafe impl Send for IMSafeTrustMe {} +unsafe impl Sync for IMSafeTrustMe {} + +static BAR: IMSafeTrustMe = IMSafeTrustMe(UnsafeCell::new(5)); + + +struct Wrap(T); +unsafe impl Send for Wrap {} +unsafe impl Sync for Wrap {} + +static BAR_PTR: Wrap<*mut u32> = Wrap(BAR.0.get()); + +fn main() {} \ No newline at end of file diff --git a/src/test/ui/consts/std/cell.stderr b/src/test/ui/consts/std/cell.stderr new file mode 100644 index 00000000000..f75aadff6d5 --- /dev/null +++ b/src/test/ui/consts/std/cell.stderr @@ -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`. diff --git a/src/test/ui/consts/std/char.rs b/src/test/ui/consts/std/char.rs new file mode 100644 index 00000000000..ce43f66a30c --- /dev/null +++ b/src/test/ui/consts/std/char.rs @@ -0,0 +1,9 @@ +// run-pass + +static X: bool = 'a'.is_ascii(); +static Y: bool = 'รค'.is_ascii(); + +fn main() { + assert!(X); + assert!(!Y); +} \ No newline at end of file diff --git a/src/test/ui/consts/std/iter.rs b/src/test/ui/consts/std/iter.rs new file mode 100644 index 00000000000..e36d5cccd5b --- /dev/null +++ b/src/test/ui/consts/std/iter.rs @@ -0,0 +1,9 @@ +// run-pass + +const I: std::iter::Empty = std::iter::empty(); + +fn main() { + for i in I { + panic!("magical value creation: {}", i); + } +} \ No newline at end of file diff --git a/src/test/ui/consts/std/slice.rs b/src/test/ui/consts/std/slice.rs new file mode 100644 index 00000000000..7598a20fc28 --- /dev/null +++ b/src/test/ui/consts/std/slice.rs @@ -0,0 +1,10 @@ +// compile-pass + +struct Wrap(T); +unsafe impl Send for Wrap {} +unsafe impl Sync for Wrap {} + +static FOO: Wrap<*const u32> = Wrap([42, 44, 46].as_ptr()); +static BAR: Wrap<*const u8> = Wrap("hello".as_ptr()); + +fn main() {} \ No newline at end of file