Skip to content

Commit

Permalink
Move some const asserts to runtime asserts
Browse files Browse the repository at this point in the history
Helps to address [builds with `-Zrandomize-layout`][comment]

[comment]: bytecodealliance#1750 (comment)
  • Loading branch information
alexcrichton committed Nov 5, 2024
1 parent fce817c commit 58dec91
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
5 changes: 3 additions & 2 deletions crates/wasmparser/src/validator/component_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,10 @@ pub struct ComponentDefinedTypeId {
alias_id: u32,
}

const _: () = {
#[test]
fn assert_defined_type_small() {
assert!(core::mem::size_of::<ComponentDefinedTypeId>() <= 8);
};
}

impl TypeIdentifier for ComponentDefinedTypeId {
type Data = ComponentDefinedType;
Expand Down
5 changes: 3 additions & 2 deletions crates/wasmparser/src/validator/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@ enum MaybeType<T = ValType> {
// The validator is pretty performance-sensitive and `MaybeType` is the main
// unit of storage, so assert that it doesn't exceed 4 bytes which is the
// current expected size.
const _: () = {
#[test]
fn assert_maybe_type_small() {
assert!(core::mem::size_of::<MaybeType>() == 4);
};
}

impl core::fmt::Display for MaybeType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Expand Down
6 changes: 4 additions & 2 deletions crates/wasmparser/src/validator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ macro_rules! define_type_id {
}
}


// The size of type IDs was seen to have a large-ish impact in #844, so
// this assert ensures that it stays relatively small.
const _: () = {
Expand All @@ -106,9 +107,10 @@ pub struct CoreTypeId {
index: u32,
}

const _: () = {
#[test]
fn assert_core_type_id_small() {
assert!(core::mem::size_of::<CoreTypeId>() <= 4);
};
}

impl TypeIdentifier for CoreTypeId {
type Data = SubType;
Expand Down
5 changes: 3 additions & 2 deletions crates/wast/src/core/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,11 +1208,12 @@ instructions! {
// since big `*.wat` files will have a lot of these. This is a small ratchet to
// make sure that this enum doesn't become larger than it already is, although
// ideally it also wouldn't be as large as it is now.
const _: () = {
#[test]
fn assert_instruction_not_too_large() {
let size = std::mem::size_of::<Instruction<'_>>();
let pointer = std::mem::size_of::<u64>();
assert!(size <= pointer * 11);
};
}

impl<'a> Instruction<'a> {
pub(crate) fn needs_data_count(&self) -> bool {
Expand Down

0 comments on commit 58dec91

Please sign in to comment.