Use custom macros for static assertions

This commit is contained in:
Brian Carroll 2022-02-09 17:04:28 +00:00
parent d8b76b317b
commit c61a18a200
8 changed files with 82 additions and 26 deletions

3
Cargo.lock generated
View file

@ -3592,6 +3592,7 @@ dependencies = [
"bumpalo",
"lazy_static",
"roc_collections",
"roc_error_macros",
"roc_ident",
"roc_region",
"snafu",
@ -3608,6 +3609,7 @@ dependencies = [
"roc_builtins",
"roc_can",
"roc_collections",
"roc_error_macros",
"roc_module",
"roc_problem",
"roc_region",
@ -3792,6 +3794,7 @@ version = "0.1.0"
dependencies = [
"bumpalo",
"roc_collections",
"roc_error_macros",
"roc_module",
"roc_region",
"static_assertions",

View file

@ -9,6 +9,7 @@ license = "UPL-1.0"
roc_region = { path = "../region" }
roc_ident = { path = "../ident" }
roc_collections = { path = "../collections" }
roc_error_macros = {path = "../../error_macros"}
bumpalo = { version = "3.8.0", features = ["collections"] }
lazy_static = "1.4.0"
static_assertions = "1.1.0"

View file

@ -59,7 +59,9 @@ pub enum TagName {
Closure(Symbol),
}
static_assertions::assert_eq_size!((usize, usize, u64), TagName);
roc_error_macros::assert_sizeof_aarch64!(TagName, 24);
roc_error_macros::assert_sizeof_wasm!(TagName, 16);
roc_error_macros::assert_sizeof_default!(TagName, 24);
impl TagName {
pub fn as_ident_str(&self, interns: &Interns, home: ModuleId) -> IdentStr {

View file

@ -17,6 +17,7 @@ roc_std = { path = "../../roc_std" }
roc_problem = { path = "../problem" }
roc_builtins = { path = "../builtins" }
roc_target = { path = "../roc_target" }
roc_error_macros = {path="../../error_macros"}
ven_pretty = { path = "../../vendor/pretty" }
morphic_lib = { path = "../../vendor/morphic_lib" }
bumpalo = { version = "3.8.0", features = ["collections"] }

View file

@ -34,18 +34,26 @@ pub fn pretty_print_ir_symbols() -> bool {
// if it went up, maybe check that the change is really required
// i128 alignment is different on arm
#[cfg(target_arch = "aarch64")]
static_assertions::assert_eq_size!([u64; 4], Literal);
#[cfg(not(target_arch = "aarch64"))]
static_assertions::assert_eq_size!([u64; 3], Literal);
static_assertions::assert_eq_size!(([u64; 4], [usize; 6]), Expr);
#[cfg(not(target_arch = "aarch64"))]
static_assertions::assert_eq_size!(([u64; 5], [usize; 14]), Stmt);
#[cfg(target_arch = "aarch64")]
static_assertions::assert_eq_size!([u64; 20], Stmt);
static_assertions::assert_eq_size!([usize; 6], ProcLayout);
static_assertions::assert_eq_size!(([u64; 3], [usize; 4]), Call);
static_assertions::assert_eq_size!(([u64; 3], [usize; 2]), CallType);
roc_error_macros::assert_sizeof_aarch64!(Literal, 4 * 8);
roc_error_macros::assert_sizeof_aarch64!(Expr, 10 * 8);
roc_error_macros::assert_sizeof_aarch64!(Stmt, 20 * 8);
roc_error_macros::assert_sizeof_aarch64!(ProcLayout, 6 * 8);
roc_error_macros::assert_sizeof_aarch64!(Call, 7 * 8);
roc_error_macros::assert_sizeof_aarch64!(CallType, 5 * 8);
roc_error_macros::assert_sizeof_wasm!(Literal, 24);
roc_error_macros::assert_sizeof_wasm!(Expr, 56);
roc_error_macros::assert_sizeof_wasm!(Stmt, 96);
roc_error_macros::assert_sizeof_wasm!(ProcLayout, 24);
roc_error_macros::assert_sizeof_wasm!(Call, 40);
roc_error_macros::assert_sizeof_wasm!(CallType, 32);
roc_error_macros::assert_sizeof_default!(Literal, 3 * 8);
roc_error_macros::assert_sizeof_default!(Expr, 10 * 8);
roc_error_macros::assert_sizeof_default!(Stmt, 19 * 8);
roc_error_macros::assert_sizeof_default!(ProcLayout, 6 * 8);
roc_error_macros::assert_sizeof_default!(Call, 7 * 8);
roc_error_macros::assert_sizeof_default!(CallType, 5 * 8);
macro_rules! return_on_layout_error {
($env:expr, $layout_result:expr) => {

View file

@ -9,6 +9,7 @@ edition = "2018"
roc_collections = { path = "../collections" }
roc_region = { path = "../region" }
roc_module = { path = "../module" }
roc_error_macros = {path="../../error_macros"}
ven_ena = { path = "../../vendor/ena" }
bumpalo = { version = "3.8.0", features = ["collections"] }
static_assertions = "1.1.0"

View file

@ -9,12 +9,15 @@ use ven_ena::unify::{InPlace, Snapshot, UnificationTable, UnifyKey};
// if your changes cause this number to go down, great!
// please change it to the lower number.
// if it went up, maybe check that the change is really required
static_assertions::assert_eq_size!([u64; 6], Descriptor);
static_assertions::assert_eq_size!([u64; 4], Content);
static_assertions::assert_eq_size!([u64; 3], FlatType);
static_assertions::assert_eq_size!(([usize; 4], [u64; 2]), Problem);
static_assertions::assert_eq_size!([u8; 12], UnionTags);
static_assertions::assert_eq_size!([u64; 2], RecordFields);
roc_error_macros::assert_sizeof_all!(Descriptor, 6 * 8);
roc_error_macros::assert_sizeof_all!(Content, 4 * 8);
roc_error_macros::assert_sizeof_all!(FlatType, 3 * 8);
roc_error_macros::assert_sizeof_all!(UnionTags, 12);
roc_error_macros::assert_sizeof_all!(RecordFields, 2 * 8);
roc_error_macros::assert_sizeof_aarch64!(Problem, 6 * 8);
roc_error_macros::assert_sizeof_wasm!(Problem, 32);
roc_error_macros::assert_sizeof_default!(Problem, 6 * 8);
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct Mark(i32);
@ -1623,11 +1626,14 @@ impl From<Content> for Descriptor {
}
}
static_assertions::assert_eq_size!([u64; 4], Content);
static_assertions::assert_eq_size!([usize; 4], (Variable, Option<Lowercase>));
static_assertions::assert_eq_size!([u64; 3], (Symbol, AliasVariables, Variable));
static_assertions::assert_eq_size!([u8; 8], AliasVariables);
static_assertions::assert_eq_size!([u64; 3], FlatType);
roc_error_macros::assert_sizeof_all!(Content, 4 * 8);
roc_error_macros::assert_sizeof_all!((Symbol, AliasVariables, Variable), 3 * 8);
roc_error_macros::assert_sizeof_all!(AliasVariables, 8);
roc_error_macros::assert_sizeof_all!(FlatType, 3 * 8);
roc_error_macros::assert_sizeof_aarch64!((Variable, Option<Lowercase>), 4 * 8);
roc_error_macros::assert_sizeof_wasm!((Variable, Option<Lowercase>), 4 * 4);
roc_error_macros::assert_sizeof_all!((Variable, Option<Lowercase>), 4 * 8);
#[derive(Clone, Debug)]
pub enum Content {
@ -1765,8 +1771,6 @@ impl Content {
}
}
static_assertions::assert_eq_size!([u64; 3], FlatType);
#[derive(Clone, Debug)]
pub enum FlatType {
Apply(Symbol, VariableSubsSlice),

View file

@ -29,3 +29,39 @@ macro_rules! user_error {
std::process::exit(1);
})
}
/// Assert that a type has the expected size on ARM
#[macro_export]
macro_rules! assert_sizeof_aarch64 {
($t: ty, $expected_size: expr) => {
#[cfg(target_arch = "aarch64")]
static_assertions::assert_eq_size!($t, [u8; $expected_size]);
};
}
/// Assert that a type has the expected size in Wasm
#[macro_export]
macro_rules! assert_sizeof_wasm {
($t: ty, $expected_size: expr) => {
#[cfg(target_family = "wasm")]
static_assertions::assert_eq_size!($t, [u8; $expected_size]);
};
}
/// Assert that a type has the expected size on any target not covered above
/// In practice we use this for x86_64, and add specific macros for other platforms
#[macro_export]
macro_rules! assert_sizeof_default {
($t: ty, $expected_size: expr) => {
#[cfg(not(any(target_family = "wasm", target_arch = "aarch64")))]
static_assertions::assert_eq_size!($t, [u8; $expected_size]);
};
}
/// Assert that a type has the expected size on all targets
#[macro_export]
macro_rules! assert_sizeof_all {
($t: ty, $expected_size: expr) => {
static_assertions::assert_eq_size!($t, [u8; $expected_size]);
};
}