diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
commit | 64d98f8ee037282c35007b64c2649055c56af1db (patch) | |
tree | 5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/consts/std | |
parent | Adding debian version 1.67.1+dfsg1-1. (diff) | |
download | rustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/consts/std')
-rw-r--r-- | tests/ui/consts/std/alloc.32bit.stderr | 25 | ||||
-rw-r--r-- | tests/ui/consts/std/alloc.64bit.stderr | 25 | ||||
-rw-r--r-- | tests/ui/consts/std/alloc.rs | 19 | ||||
-rw-r--r-- | tests/ui/consts/std/cell.rs | 46 | ||||
-rw-r--r-- | tests/ui/consts/std/cell.stderr | 26 | ||||
-rw-r--r-- | tests/ui/consts/std/iter.rs | 9 | ||||
-rw-r--r-- | tests/ui/consts/std/slice.rs | 10 |
7 files changed, 160 insertions, 0 deletions
diff --git a/tests/ui/consts/std/alloc.32bit.stderr b/tests/ui/consts/std/alloc.32bit.stderr new file mode 100644 index 000000000..8c83df53d --- /dev/null +++ b/tests/ui/consts/std/alloc.32bit.stderr @@ -0,0 +1,25 @@ +error[E0080]: it is undefined behavior to use this value + --> $DIR/alloc.rs:12:1 + | +LL | const LAYOUT_INVALID_ZERO: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x00) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .align.0.<enum-tag>: encountered 0x00000000, but expected a valid enum tag + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { + HEX_DUMP + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/alloc.rs:16:1 + | +LL | const LAYOUT_INVALID_THREE: Layout = unsafe { Layout::from_size_align_unchecked(9, 3) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .align.0.<enum-tag>: encountered 0x00000003, but expected a valid enum tag + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { + HEX_DUMP + } + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/consts/std/alloc.64bit.stderr b/tests/ui/consts/std/alloc.64bit.stderr new file mode 100644 index 000000000..addedad17 --- /dev/null +++ b/tests/ui/consts/std/alloc.64bit.stderr @@ -0,0 +1,25 @@ +error[E0080]: it is undefined behavior to use this value + --> $DIR/alloc.rs:12:1 + | +LL | const LAYOUT_INVALID_ZERO: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x00) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .align.0.<enum-tag>: encountered 0x0000000000000000, but expected a valid enum tag + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { + HEX_DUMP + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/alloc.rs:16:1 + | +LL | const LAYOUT_INVALID_THREE: Layout = unsafe { Layout::from_size_align_unchecked(9, 3) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .align.0.<enum-tag>: encountered 0x0000000000000003, but expected a valid enum tag + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { + HEX_DUMP + } + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/consts/std/alloc.rs b/tests/ui/consts/std/alloc.rs new file mode 100644 index 000000000..9abf35d63 --- /dev/null +++ b/tests/ui/consts/std/alloc.rs @@ -0,0 +1,19 @@ +// stderr-per-bitwidth +// ignore-debug (the debug assertions change the error) +// Strip out raw byte dumps to make comparison platform-independent: +// normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)" +// normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*a(lloc)?[0-9]+(\+[a-z0-9]+)?─*╼ )+ *│.*" -> "HEX_DUMP" +use std::alloc::Layout; + +// ok +const LAYOUT_VALID: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x08) }; + +// not ok, since alignment needs to be non-zero. +const LAYOUT_INVALID_ZERO: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x00) }; +//~^ ERROR it is undefined behavior to use this value + +// not ok, since alignment needs to be a power of two. +const LAYOUT_INVALID_THREE: Layout = unsafe { Layout::from_size_align_unchecked(9, 3) }; +//~^ ERROR it is undefined behavior to use this value + +fn main() {} diff --git a/tests/ui/consts/std/cell.rs b/tests/ui/consts/std/cell.rs new file mode 100644 index 000000000..f1ef54131 --- /dev/null +++ b/tests/ui/consts/std/cell.rs @@ -0,0 +1,46 @@ +#![feature(const_refs_to_cell)] + +use std::cell::*; + +// not ok, because this creates a dangling pointer, just like `let x = Cell::new(42).as_ptr()` would +static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr()); +//~^ ERROR encountered dangling pointer +const FOO_CONST: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr()); +//~^ ERROR encountered dangling pointer + +// Ok, these are just base values and it is the `Wrap` author's job to uphold `Send` and `Sync` +// invariants, since they used `unsafe impl`. +static FOO3: Wrap<Cell<u32>> = Wrap(Cell::new(42)); +const FOO3_CONST: Wrap<Cell<u32>> = Wrap(Cell::new(42)); + +// ok, we are referring to the memory of another static item. +static FOO4: Wrap<*mut u32> = Wrap(FOO3.0.as_ptr()); + +// not ok, the use of a constant here is equivalent to an inline declaration of the value, so +// its memory will get freed before the constant is finished evaluating, thus creating a dangling +// pointer. This would happen exactly the same at runtime. +const FOO4_CONST: Wrap<*mut u32> = Wrap(FOO3_CONST.0.as_ptr()); +//~^ ERROR encountered dangling pointer + +// not ok, because the `as_ptr` call takes a reference to a temporary that will get freed +// before the constant is finished evaluating. +const FOO2: *mut u32 = Cell::new(42).as_ptr(); +//~^ ERROR encountered dangling pointer + +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()); + +const fn fst_ref<T, U>(x: &(T, U)) -> &T { &x.0 } + +fn main() {} diff --git a/tests/ui/consts/std/cell.stderr b/tests/ui/consts/std/cell.stderr new file mode 100644 index 000000000..937fa7db0 --- /dev/null +++ b/tests/ui/consts/std/cell.stderr @@ -0,0 +1,26 @@ +error: encountered dangling pointer in final constant + --> $DIR/cell.rs:6:1 + | +LL | static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: encountered dangling pointer in final constant + --> $DIR/cell.rs:8:1 + | +LL | const FOO_CONST: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: encountered dangling pointer in final constant + --> $DIR/cell.rs:22:1 + | +LL | const FOO4_CONST: Wrap<*mut u32> = Wrap(FOO3_CONST.0.as_ptr()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: encountered dangling pointer in final constant + --> $DIR/cell.rs:27:1 + | +LL | const FOO2: *mut u32 = Cell::new(42).as_ptr(); + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 4 previous errors + diff --git a/tests/ui/consts/std/iter.rs b/tests/ui/consts/std/iter.rs new file mode 100644 index 000000000..e9af781eb --- /dev/null +++ b/tests/ui/consts/std/iter.rs @@ -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); + } +} diff --git a/tests/ui/consts/std/slice.rs b/tests/ui/consts/std/slice.rs new file mode 100644 index 000000000..f19defc64 --- /dev/null +++ b/tests/ui/consts/std/slice.rs @@ -0,0 +1,10 @@ +// build-pass (FIXME(62277): could be check-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() {} |