feat(oxc_index): add static_assert_size for compile time evaluation (#343)

This commit is contained in:
Boshen 2023-05-08 22:20:54 +08:00 committed by GitHub
parent c5c88a965d
commit 1b83e6c16e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 23 deletions

2
Cargo.lock generated
View file

@ -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",

View file

@ -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 }

View file

@ -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::<Statement>(), 16);
assert_eq!(size_of::<Expression>(), 16);
assert_eq!(size_of::<Declaration>(), 16);
assert_eq!(size_of::<BindingPatternKind>(), 16);
assert_eq!(size_of::<ModuleDeclaration>(), 16);
assert_eq!(size_of::<ClassElement>(), 16);
assert_eq!(size_of::<ExportDefaultDeclarationKind>(), 16);
assert_eq!(size_of::<AssignmentTargetPattern>(), 16);
assert_eq!(size_of::<AssignmentTargetMaybeDefault>(), 24);
assert_eq!(size_of::<AssignmentTargetProperty>(), 16);
assert_eq!(size_of::<TSLiteral>(), 16);
assert_eq!(size_of::<TSType>(), 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);
}

View file

@ -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.
///
/// <https://github.com/rust-lang/rust/blob/c86e7fb60f5343041fd0c27d4affaf3261115666/compiler/rustc_index/src/lib.rs#L30-L36>
#[macro_export]
macro_rules! static_assert_size {
($ty:ty, $size:expr) => {
const _: [(); $size] = [(); std::mem::size_of::<$ty>()];
};
}

View file

@ -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 }

View file

@ -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::<Token>(), 48);
mod size_asserts {
use oxc_index::static_assert_size;
use super::Token;
static_assert_size!(Token, 48);
}
impl<'a> Token<'a> {

View file

@ -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::<Symbol>(), 96);
mod size_asserts {
use oxc_index::static_assert_size;
use super::Symbol;
static_assert_size!(Symbol, 96);
}
bitflags! {