From 1b83e6c16e87f306dbf79dab3eb99e2aa0a367d6 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 8 May 2023 22:20:54 +0800 Subject: [PATCH] feat(oxc_index): add static_assert_size for compile time evaluation (#343) --- Cargo.lock | 2 ++ crates/oxc_ast/Cargo.toml | 1 + crates/oxc_ast/src/lib.rs | 30 +++++++++++++-------------- crates/oxc_index/src/lib.rs | 10 +++++++++ crates/oxc_parser/Cargo.toml | 1 + crates/oxc_parser/src/lexer/token.rs | 10 +++++---- crates/oxc_semantic/src/symbol/mod.rs | 10 +++++---- 7 files changed, 41 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d633c736..89f2c68ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -970,6 +970,7 @@ dependencies = [ "num-bigint", "ordered-float", "oxc_allocator", + "oxc_index", "oxc_span", "oxc_syntax", "rustc-hash", @@ -1190,6 +1191,7 @@ dependencies = [ "oxc_allocator", "oxc_ast", "oxc_diagnostics", + "oxc_index", "oxc_span", "oxc_syntax", "rustc-hash", diff --git a/crates/oxc_ast/Cargo.toml b/crates/oxc_ast/Cargo.toml index 179673fc5..46f06e507 100644 --- a/crates/oxc_ast/Cargo.toml +++ b/crates/oxc_ast/Cargo.toml @@ -13,6 +13,7 @@ repository.workspace = true oxc_allocator = { workspace = true } oxc_span = { workspace = true } oxc_syntax = { workspace = true } +oxc_index = { workspace = true } bitflags = { workspace = true } rustc-hash = { workspace = true } diff --git a/crates/oxc_ast/src/lib.rs b/crates/oxc_ast/src/lib.rs index afb375121..2830ad9bd 100644 --- a/crates/oxc_ast/src/lib.rs +++ b/crates/oxc_ast/src/lib.rs @@ -55,22 +55,22 @@ pub use crate::visit_mut::VisitMut; // The following test make sure all enum variants are boxed, resulting 16 bytes for each enum. // Read `https://nnethercote.github.io/perf-book/type-sizes.html` for more details. #[cfg(target_pointer_width = "64")] -#[test] -fn no_bloat_enum_sizes() { - use std::mem::size_of; +mod size_asserts { + use oxc_index::static_assert_size; #[allow(clippy::wildcard_imports)] use crate::ast::*; - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 24); - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 16); - assert_eq!(size_of::(), 16); + + static_assert_size!(Statement, 16); + static_assert_size!(Expression, 16); + static_assert_size!(Declaration, 16); + static_assert_size!(BindingPatternKind, 16); + static_assert_size!(ModuleDeclaration, 16); + static_assert_size!(ClassElement, 16); + static_assert_size!(ExportDefaultDeclarationKind, 16); + static_assert_size!(AssignmentTargetPattern, 16); + static_assert_size!(AssignmentTargetMaybeDefault, 24); + static_assert_size!(AssignmentTargetProperty, 16); + static_assert_size!(TSLiteral, 16); + static_assert_size!(TSType, 16); } diff --git a/crates/oxc_index/src/lib.rs b/crates/oxc_index/src/lib.rs index 17d4f55a7..15712c469 100644 --- a/crates/oxc_index/src/lib.rs +++ b/crates/oxc_index/src/lib.rs @@ -5,3 +5,13 @@ mod idx; pub use idx::Idx; + +/// Type size assertion. The first argument is a type and the second argument is its expected size. +/// +/// +#[macro_export] +macro_rules! static_assert_size { + ($ty:ty, $size:expr) => { + const _: [(); $size] = [(); std::mem::size_of::<$ty>()]; + }; +} diff --git a/crates/oxc_parser/Cargo.toml b/crates/oxc_parser/Cargo.toml index a6aa4dde0..032bf7a03 100644 --- a/crates/oxc_parser/Cargo.toml +++ b/crates/oxc_parser/Cargo.toml @@ -19,6 +19,7 @@ oxc_span = { workspace = true } oxc_ast = { workspace = true } oxc_syntax = { workspace = true } oxc_diagnostics = { workspace = true } +oxc_index = { workspace = true } bitflags = { workspace = true } rustc-hash = { workspace = true } diff --git a/crates/oxc_parser/src/lexer/token.rs b/crates/oxc_parser/src/lexer/token.rs index 292c52300..5d51e2de0 100644 --- a/crates/oxc_parser/src/lexer/token.rs +++ b/crates/oxc_parser/src/lexer/token.rs @@ -27,10 +27,12 @@ pub struct Token<'a> { } #[cfg(target_pointer_width = "64")] -#[test] -fn no_bloat_token() { - use std::mem::size_of; - assert_eq!(size_of::(), 48); +mod size_asserts { + use oxc_index::static_assert_size; + + use super::Token; + + static_assert_size!(Token, 48); } impl<'a> Token<'a> { diff --git a/crates/oxc_semantic/src/symbol/mod.rs b/crates/oxc_semantic/src/symbol/mod.rs index 63652634d..57ea7881e 100644 --- a/crates/oxc_semantic/src/symbol/mod.rs +++ b/crates/oxc_semantic/src/symbol/mod.rs @@ -34,10 +34,12 @@ pub struct Symbol { } #[cfg(target_pointer_width = "64")] -#[test] -fn symbol_size() { - use std::mem::size_of; - assert_eq!(size_of::(), 96); +mod size_asserts { + use oxc_index::static_assert_size; + + use super::Symbol; + + static_assert_size!(Symbol, 96); } bitflags! {