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

Move some const asserts to runtime asserts #1894

Merged
merged 1 commit into from
Nov 5, 2024
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
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