diff --git a/.github/.generated_ast_watch_list.yml b/.github/.generated_ast_watch_list.yml index eeec4030b096b..0dae445532eb5 100644 --- a/.github/.generated_ast_watch_list.yml +++ b/.github/.generated_ast_watch_list.yml @@ -32,7 +32,11 @@ src: - 'crates/oxc_syntax/src/generated/derive_clone_in.rs' - 'crates/oxc_syntax/src/generated/derive_content_eq.rs' - 'crates/oxc_syntax/src/generated/derive_estree.rs' + - 'crates/oxc_syntax/src/lib.rs' - 'crates/oxc_syntax/src/number.rs' - 'crates/oxc_syntax/src/operator.rs' + - 'crates/oxc_syntax/src/reference.rs' + - 'crates/oxc_syntax/src/scope.rs' + - 'crates/oxc_syntax/src/symbol.rs' - 'npm/oxc-types/types.d.ts' - 'tasks/ast_tools/src/**' diff --git a/Cargo.lock b/Cargo.lock index e0dc95aa5aaac..0e8ae50dfdfae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1580,6 +1580,7 @@ version = "0.48.2" dependencies = [ "bitflags 2.7.0", "cow-utils", + "nonmax", "num-bigint", "num-traits", "oxc_allocator", diff --git a/crates/oxc_ast/Cargo.toml b/crates/oxc_ast/Cargo.toml index d0690fd8839cd..2a354fa731837 100644 --- a/crates/oxc_ast/Cargo.toml +++ b/crates/oxc_ast/Cargo.toml @@ -28,6 +28,7 @@ oxc_syntax = { workspace = true } bitflags = { workspace = true } cow-utils = { workspace = true } +nonmax = { workspace = true } num-bigint = { workspace = true } num-traits = { workspace = true } diff --git a/crates/oxc_ast/src/generated/assert_layouts.rs b/crates/oxc_ast/src/generated/assert_layouts.rs index 38f4f32df7e8f..de6d4d0ca4043 100644 --- a/crates/oxc_ast/src/generated/assert_layouts.rs +++ b/crates/oxc_ast/src/generated/assert_layouts.rs @@ -3,7 +3,10 @@ use std::mem::{align_of, offset_of, size_of}; +use nonmax::NonMaxU32; + use oxc_regular_expression::ast::*; +use oxc_syntax::{reference::ReferenceId, scope::ScopeId, symbol::SymbolId}; use crate::ast::*; @@ -1393,6 +1396,9 @@ const _: () = { assert!(offset_of!(Comment, preceded_by_newline) == 14); assert!(offset_of!(Comment, followed_by_newline) == 15); + assert!(size_of::() == 4); + assert!(align_of::() == 4); + assert!(size_of::() == 1); assert!(align_of::() == 1); @@ -1414,6 +1420,15 @@ const _: () = { assert!(size_of::() == 1); assert!(align_of::() == 1); + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); + assert!(size_of::() == 8); assert!(align_of::() == 8); assert!(offset_of!(Span, start) == 0); @@ -2959,6 +2974,9 @@ const _: () = { assert!(offset_of!(Comment, preceded_by_newline) == 14); assert!(offset_of!(Comment, followed_by_newline) == 15); + assert!(size_of::() == 4); + assert!(align_of::() == 4); + assert!(size_of::() == 1); assert!(align_of::() == 1); @@ -2980,6 +2998,15 @@ const _: () = { assert!(size_of::() == 1); assert!(align_of::() == 1); + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); + assert!(size_of::() == 8); assert!(align_of::() == 4); assert!(offset_of!(Span, start) == 0); diff --git a/crates/oxc_syntax/src/lib.rs b/crates/oxc_syntax/src/lib.rs index c6c03195aa44e..c4ae9230e6041 100644 --- a/crates/oxc_syntax/src/lib.rs +++ b/crates/oxc_syntax/src/lib.rs @@ -1,5 +1,10 @@ //! Common code for JavaScript Syntax #![warn(missing_docs)] + +use std::num::NonZeroU32; + +use oxc_ast_macros::ast; + pub mod class; pub mod es_target; pub mod identifier; @@ -19,3 +24,8 @@ mod generated { #[cfg(feature = "serialize")] mod derive_estree; } + +/// Dummy type to communicate the content of `nonmax::NonMaxU32` to `oxc_ast_tools`. +#[ast(foreign = NonMaxU32)] +#[expect(dead_code)] +struct NonMaxU32Alias(NonZeroU32); diff --git a/crates/oxc_syntax/src/reference.rs b/crates/oxc_syntax/src/reference.rs index 3c2407009459d..5903dc4b718ce 100644 --- a/crates/oxc_syntax/src/reference.rs +++ b/crates/oxc_syntax/src/reference.rs @@ -8,6 +8,9 @@ use serde::{Serialize, Serializer}; use crate::{node::NodeId, symbol::SymbolId}; +use oxc_ast_macros::ast; + +#[ast] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct ReferenceId(NonMaxU32); diff --git a/crates/oxc_syntax/src/scope.rs b/crates/oxc_syntax/src/scope.rs index 442f26011b3e5..fedb1e2179b46 100644 --- a/crates/oxc_syntax/src/scope.rs +++ b/crates/oxc_syntax/src/scope.rs @@ -5,6 +5,9 @@ use oxc_index::Idx; #[cfg(feature = "serialize")] use serde::{Serialize, Serializer}; +use oxc_ast_macros::ast; + +#[ast] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct ScopeId(NonMaxU32); diff --git a/crates/oxc_syntax/src/symbol.rs b/crates/oxc_syntax/src/symbol.rs index 0003f4d3d6a56..c6ec65330559f 100644 --- a/crates/oxc_syntax/src/symbol.rs +++ b/crates/oxc_syntax/src/symbol.rs @@ -5,6 +5,9 @@ use oxc_index::Idx; #[cfg(feature = "serialize")] use serde::{Serialize, Serializer}; +use oxc_ast_macros::ast; + +#[ast] #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct SymbolId(NonMaxU32); diff --git a/tasks/ast_tools/src/generators/assert_layouts.rs b/tasks/ast_tools/src/generators/assert_layouts.rs index 4c30a844e45b4..6e204f2d5bcb5 100644 --- a/tasks/ast_tools/src/generators/assert_layouts.rs +++ b/tasks/ast_tools/src/generators/assert_layouts.rs @@ -46,8 +46,12 @@ impl Generator for AssertLayouts { let output = quote! { use std::mem::{align_of, offset_of, size_of}; + ///@@line_break + use nonmax::NonMaxU32; + ///@@line_break use oxc_regular_expression::ast::*; + use oxc_syntax::{reference::ReferenceId, scope::ScopeId, symbol::SymbolId}; ///@@line_break use crate::ast::*; @@ -337,8 +341,6 @@ fn calculate_layout_for_cell(type_id: TypeId, schema: &mut Schema) -> Layout { /// /// Primitives have varying layouts. Some have niches, most don't. fn calculate_layout_for_primitive(primitive_def: &PrimitiveDef) -> Layout { - // `ScopeId`, `SymbolId` and `ReferenceId` are a `NonZeroU32`, with a niche for 0 - let semantic_id_layout = Layout::from_size_align_niche(4, 4, Niche::new(0, 4, true, 1)); // `&str` and `Atom` are a `NonNull` pointer + `usize` pair. Niche for 0 on the pointer field let str_layout = Layout { layout_64: PlatformLayout::from_size_align_niche(16, 8, Niche::new(0, 8, true, 1)), @@ -394,9 +396,6 @@ fn calculate_layout_for_primitive(primitive_def: &PrimitiveDef) -> Layout { panic!("Cannot calculate alignment for `NonZeroI128`. It differs depending on Rust version.") } "NonZeroIsize" => non_zero_usize_layout, - "ScopeId" => semantic_id_layout.clone(), - "SymbolId" => semantic_id_layout.clone(), - "ReferenceId" => semantic_id_layout, "PointerAlign" => Layout { layout_64: PlatformLayout::from_size_align(0, 8), layout_32: PlatformLayout::from_size_align(0, 4), diff --git a/tasks/ast_tools/src/main.rs b/tasks/ast_tools/src/main.rs index 51cdce141a77e..43d9b6210329a 100644 --- a/tasks/ast_tools/src/main.rs +++ b/tasks/ast_tools/src/main.rs @@ -198,8 +198,12 @@ static SOURCE_PATHS: &[&str] = &[ "crates/oxc_ast/src/ast/jsx.rs", "crates/oxc_ast/src/ast/ts.rs", "crates/oxc_ast/src/ast/comment.rs", + "crates/oxc_syntax/src/lib.rs", "crates/oxc_syntax/src/number.rs", "crates/oxc_syntax/src/operator.rs", + "crates/oxc_syntax/src/scope.rs", + "crates/oxc_syntax/src/symbol.rs", + "crates/oxc_syntax/src/reference.rs", "crates/oxc_span/src/span/types.rs", "crates/oxc_span/src/source_type/mod.rs", "crates/oxc_regular_expression/src/ast.rs", diff --git a/tasks/ast_tools/src/parse/parse.rs b/tasks/ast_tools/src/parse/parse.rs index ffe14e7ed89e7..decc0a924619c 100644 --- a/tasks/ast_tools/src/parse/parse.rs +++ b/tasks/ast_tools/src/parse/parse.rs @@ -135,9 +135,6 @@ impl<'c> Parser<'c> { "NonZeroIsize" => primitive("NonZeroIsize"), "&str" => primitive("&str"), "Atom" => primitive("Atom"), - "ScopeId" => primitive("ScopeId"), - "SymbolId" => primitive("SymbolId"), - "ReferenceId" => primitive("ReferenceId"), // TODO: Remove the need for this by adding // `#[cfg_attr(target_pointer_width = "64", repr(align(8)))]` to all AST types "PointerAlign" => primitive("PointerAlign"),