Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark core::alloc::Layout::from_size_align_unchecked const #60370

Merged
merged 3 commits into from
May 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcore/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Richard-W marked this conversation as resolved.
Show resolved Hide resolved
Layout { size_: size, align_: NonZeroUsize::new_unchecked(align) }
}

Expand Down
10 changes: 10 additions & 0 deletions src/libcore/tests/alloc.rs
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 2 additions & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/consts/std/alloc.rs
Original file line number Diff line number Diff line change
@@ -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() {}
11 changes: 11 additions & 0 deletions src/test/ui/consts/std/alloc.stderr
Original file line number Diff line number Diff line change
@@ -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`.