forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#74930 - ecstatic-morse:const-size-align-of-va…
…l, r=oli-obk Make `mem::size_of_val` and `mem::align_of_val` unstably const Implements rust-lang#46571 but does not stabilize it. I wanted this while working on something today. The only reason not to immediately stabilize are concerns around [custom DSTs](rust-lang#46571 (comment)). That proposal has made zero progress in the last two years and const eval is rich enough to support pretty much any user-defined `len` function as long as nightly features are allowed (`raw_ptr_deref`). Currently, this raises a `const_err` lint when passed an `extern type`. r? @oli-obk cc @rust-lang/wg-const-eval
- Loading branch information
Showing
7 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/test/ui/consts/const-size_of_val-align_of_val-extern-type.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![feature(extern_types)] | ||
#![feature(core_intrinsics)] | ||
#![feature(const_size_of_val, const_align_of_val)] | ||
|
||
use std::intrinsics::{size_of_val, min_align_of_val}; | ||
|
||
extern { | ||
type Opaque; | ||
} | ||
|
||
const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) }; //~ ERROR | ||
const _ALIGN: usize = unsafe { min_align_of_val(&4 as *const i32 as *const Opaque) }; //~ ERROR | ||
|
||
fn main() {} |
20 changes: 20 additions & 0 deletions
20
src/test/ui/consts/const-size_of_val-align_of_val-extern-type.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: any use of this value will cause an error | ||
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:11:31 | ||
| | ||
LL | const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) }; | ||
| ------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- | ||
| | | ||
| `extern type` does not have known layout | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
|
||
error: any use of this value will cause an error | ||
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:12:32 | ||
| | ||
LL | const _ALIGN: usize = unsafe { min_align_of_val(&4 as *const i32 as *const Opaque) }; | ||
| -------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- | ||
| | | ||
| `extern type` does not have known layout | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// run-pass | ||
|
||
#![feature(const_size_of_val, const_align_of_val)] | ||
|
||
use std::mem; | ||
|
||
struct Foo(u32); | ||
|
||
#[derive(Clone, Copy)] | ||
struct Bar { | ||
_x: u8, | ||
_y: u16, | ||
_z: u8, | ||
} | ||
|
||
union Ugh { | ||
_a: [u8; 3], | ||
_b: Bar, | ||
} | ||
|
||
const FOO: Foo = Foo(4); | ||
const BAR: Bar = Bar { _x: 4, _y: 1, _z: 2 }; | ||
const UGH: Ugh = Ugh { _a: [0; 3] }; | ||
|
||
const SIZE_OF_FOO: usize = mem::size_of_val(&FOO); | ||
const SIZE_OF_BAR: usize = mem::size_of_val(&BAR); | ||
const SIZE_OF_UGH: usize = mem::size_of_val(&UGH); | ||
|
||
const ALIGN_OF_FOO: usize = mem::align_of_val(&FOO); | ||
const ALIGN_OF_BAR: usize = mem::align_of_val(&BAR); | ||
const ALIGN_OF_UGH: usize = mem::align_of_val(&UGH); | ||
|
||
const SIZE_OF_SLICE: usize = mem::size_of_val("foobar".as_bytes()); | ||
|
||
fn main() { | ||
assert_eq!(SIZE_OF_FOO, mem::size_of::<Foo>()); | ||
assert_eq!(SIZE_OF_BAR, mem::size_of::<Bar>()); | ||
assert_eq!(SIZE_OF_UGH, mem::size_of::<Ugh>()); | ||
|
||
assert_eq!(ALIGN_OF_FOO, mem::align_of::<Foo>()); | ||
assert_eq!(ALIGN_OF_BAR, mem::align_of::<Bar>()); | ||
assert_eq!(ALIGN_OF_UGH, mem::align_of::<Ugh>()); | ||
|
||
assert_eq!(SIZE_OF_SLICE, "foobar".len()); | ||
} |