Skip to content

Commit 7885dfc

Browse files
authored
Rollup merge of rust-lang#60370 - Richard-W:const-layout-construction, r=sfackler
Mark core::alloc::Layout::from_size_align_unchecked const Makes it possible (pending stabilization of rust-lang#57563 (`const_fn`)) to rewrite code like ```rust const BUFFER_SIZE: usize = 0x2000; const BUFFER_ALIGN: usize = 0x1000; fn foo() { let layout = std::alloc::Layout::from_size_align(BUFFER_SIZE, BUFFER_ALIGN) .unwrap(); let buffer = std::alloc::alloc(layout); } ``` to ```rust const BUFFER_LAYOUT: std::alloc::Layout = unsafe { std::alloc::Layout::from_size_align_unchecked(0x2000, 0x1000) }; fn foo() { let buffer = std::alloc::alloc(BUFFER_LAYOUT); } ``` which (although `unsafe` is used) looks somewhat cleaner and is easier to read.
2 parents 963184b + c0b6d3c commit 7885dfc

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

src/libcore/alloc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl Layout {
9999
/// [`Layout::from_size_align`](#method.from_size_align).
100100
#[stable(feature = "alloc_layout", since = "1.28.0")]
101101
#[inline]
102-
pub unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
102+
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
103103
Layout { size_: size, align_: NonZeroUsize::new_unchecked(align) }
104104
}
105105

src/libcore/tests/alloc.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use core::alloc::Layout;
2+
3+
#[test]
4+
fn const_unchecked_layout() {
5+
const SIZE: usize = 0x2000;
6+
const ALIGN: usize = 0x1000;
7+
const LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(SIZE, ALIGN) };
8+
assert_eq!(LAYOUT.size(), SIZE);
9+
assert_eq!(LAYOUT.align(), ALIGN);
10+
}

src/libcore/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
#![feature(slice_partition_dedup)]
3232
#![feature(copy_within)]
3333
#![feature(int_error_matching)]
34+
#![feature(const_fn)]
3435
#![warn(rust_2018_idioms)]
3536

3637
extern crate test;
3738

39+
mod alloc;
3840
mod any;
3941
mod array;
4042
mod ascii;

src/test/ui/consts/std/alloc.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::alloc::Layout;
2+
3+
// ok
4+
const LAYOUT_VALID: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x08) };
5+
6+
// not ok, since alignment needs to be non-zero.
7+
const LAYOUT_INVALID: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x00) };
8+
//~^ ERROR it is undefined behavior to use this value
9+
10+
fn main() {}

src/test/ui/consts/std/alloc.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/alloc.rs:7:1
3+
|
4+
LL | const LAYOUT_INVALID: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x00) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0 at .align_, but expected something greater or equal to 1
6+
|
7+
= 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
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)