summaryrefslogtreecommitdiffstats
path: root/tests/ui/consts/std
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/consts/std')
-rw-r--r--tests/ui/consts/std/alloc.32bit.stderr25
-rw-r--r--tests/ui/consts/std/alloc.64bit.stderr25
-rw-r--r--tests/ui/consts/std/alloc.rs19
-rw-r--r--tests/ui/consts/std/cell.rs46
-rw-r--r--tests/ui/consts/std/cell.stderr26
-rw-r--r--tests/ui/consts/std/iter.rs9
-rw-r--r--tests/ui/consts/std/slice.rs10
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() {}