From a7a05203f18eac663ebf2751b77621c453dc280c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Wiedenh=C3=B6ft?= Date: Mon, 29 Apr 2019 15:49:43 +0200 Subject: [PATCH 1/3] Mark core::alloc::Layout::from_size_align_unchecked const --- src/libcore/alloc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index c124457118cb9..302a9d89e5854 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -99,7 +99,7 @@ impl Layout { /// [`Layout::from_size_align`](#method.from_size_align). #[stable(feature = "alloc_layout", since = "1.28.0")] #[inline] - pub unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self { + pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self { Layout { size_: size, align_: NonZeroUsize::new_unchecked(align) } } From 07e8d844795628ce90415c1dec3536f3e75cc71c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Wiedenh=C3=B6ft?= Date: Tue, 30 Apr 2019 08:23:14 +0200 Subject: [PATCH 2/3] Add const_unchecked_layout test to libcore/tests --- src/libcore/tests/alloc.rs | 10 ++++++++++ src/libcore/tests/lib.rs | 2 ++ 2 files changed, 12 insertions(+) create mode 100644 src/libcore/tests/alloc.rs diff --git a/src/libcore/tests/alloc.rs b/src/libcore/tests/alloc.rs new file mode 100644 index 0000000000000..63537ba23d84d --- /dev/null +++ b/src/libcore/tests/alloc.rs @@ -0,0 +1,10 @@ +use core::alloc::Layout; + +#[test] +fn const_unchecked_layout() { + const SIZE: usize = 0x2000; + const ALIGN: usize = 0x1000; + const LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(SIZE, ALIGN) }; + assert_eq!(LAYOUT.size(), SIZE); + assert_eq!(LAYOUT.align(), ALIGN); +} diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index b8075ef2942e0..c617596aba801 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -31,10 +31,12 @@ #![feature(slice_partition_dedup)] #![feature(copy_within)] #![feature(int_error_matching)] +#![feature(const_fn)] #![warn(rust_2018_idioms)] extern crate test; +mod alloc; mod any; mod array; mod ascii; From c0b6d3c975915e740548f0ec7bcf5963e7a3b218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Wiedenh=C3=B6ft?= Date: Tue, 30 Apr 2019 12:56:38 +0200 Subject: [PATCH 3/3] Add ui test for const Layout::from_size_align_unchecked --- src/test/ui/consts/std/alloc.rs | 10 ++++++++++ src/test/ui/consts/std/alloc.stderr | 11 +++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/test/ui/consts/std/alloc.rs create mode 100644 src/test/ui/consts/std/alloc.stderr diff --git a/src/test/ui/consts/std/alloc.rs b/src/test/ui/consts/std/alloc.rs new file mode 100644 index 0000000000000..65ac7e44926d0 --- /dev/null +++ b/src/test/ui/consts/std/alloc.rs @@ -0,0 +1,10 @@ +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: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x00) }; +//~^ ERROR it is undefined behavior to use this value + +fn main() {} diff --git a/src/test/ui/consts/std/alloc.stderr b/src/test/ui/consts/std/alloc.stderr new file mode 100644 index 0000000000000..74a8f3daf6aaa --- /dev/null +++ b/src/test/ui/consts/std/alloc.stderr @@ -0,0 +1,11 @@ +error[E0080]: it is undefined behavior to use this value + --> $DIR/alloc.rs:7:1 + | +LL | const LAYOUT_INVALID: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x00) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0 at .align_, but expected something greater or equal to 1 + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0080`.