Skip to content
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
4 changes: 4 additions & 0 deletions .github/generated/ast_changes_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ src:
- 'crates/oxc_ast/src/generated/ast_kind.rs'
- 'crates/oxc_ast/src/generated/derive_clone_in.rs'
- 'crates/oxc_ast/src/generated/derive_content_eq.rs'
- 'crates/oxc_ast/src/generated/derive_dummy.rs'
- 'crates/oxc_ast/src/generated/derive_estree.rs'
- 'crates/oxc_ast/src/generated/derive_get_address.rs'
- 'crates/oxc_ast/src/generated/derive_get_span.rs'
- 'crates/oxc_ast/src/generated/derive_get_span_mut.rs'
- 'crates/oxc_ast/src/generated/derive_take_in.rs'
- 'crates/oxc_ast/src/generated/get_id.rs'
- 'crates/oxc_ast/src/serialize.rs'
- 'crates/oxc_ast_macros/src/generated/mod.rs'
Expand All @@ -31,12 +33,14 @@ src:
- 'crates/oxc_regular_expression/src/generated/derive_estree.rs'
- 'crates/oxc_regular_expression/src/generated/derive_get_address.rs'
- 'crates/oxc_span/src/generated/assert_layouts.rs'
- 'crates/oxc_span/src/generated/derive_dummy.rs'
- 'crates/oxc_span/src/generated/derive_estree.rs'
- 'crates/oxc_span/src/source_type/mod.rs'
- 'crates/oxc_span/src/span.rs'
- 'crates/oxc_syntax/src/generated/assert_layouts.rs'
- 'crates/oxc_syntax/src/generated/derive_clone_in.rs'
- 'crates/oxc_syntax/src/generated/derive_content_eq.rs'
- 'crates/oxc_syntax/src/generated/derive_dummy.rs'
- 'crates/oxc_syntax/src/generated/derive_estree.rs'
- 'crates/oxc_syntax/src/lib.rs'
- 'crates/oxc_syntax/src/module_record.rs'
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod convert;
mod from_raw_parts;
pub mod hash_map;
pub mod string;
mod take_in;
mod vec;
mod vec2;

Expand All @@ -34,4 +35,5 @@ pub use clone_in::CloneIn;
pub use convert::{FromIn, IntoIn};
pub use hash_map::HashMap;
pub use string::String;
pub use take_in::{Dummy, TakeIn};
pub use vec::Vec;
138 changes: 138 additions & 0 deletions crates/oxc_allocator/src/take_in.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use std::{cell::Cell, mem, num};

use crate::{Allocator, Box, Vec};

/// A trait to replace an existing AST node with a dummy.
pub trait TakeIn<'a>: Dummy<'a> {
/// Replace node with a dummy.
#[must_use]
fn take_in(&mut self, allocator: &'a Allocator) -> Self {
let dummy = Dummy::dummy(allocator);
mem::replace(self, dummy)
}
}

impl<'a, T> TakeIn<'a> for Vec<'a, T> {}

/// A trait to create a dummy AST node.
pub trait Dummy<'a>: Sized {
/// Create a dummy node.
fn dummy(allocator: &'a Allocator) -> Self;
}

impl<'a, T> Dummy<'a> for Option<T> {
/// Create a dummy [`Option`].
#[expect(clippy::inline_always)]
#[inline(always)]
fn dummy(_allocator: &'a Allocator) -> Self {
None
}
}

impl<'a, T: Dummy<'a>> Dummy<'a> for Box<'a, T> {
/// Create a dummy [`Box`].
#[inline]
fn dummy(allocator: &'a Allocator) -> Self {
Box::new_in(Dummy::dummy(allocator), allocator)
}
}

impl<'a, T> Dummy<'a> for Vec<'a, T> {
/// Create a dummy [`Vec`].
#[inline]
fn dummy(allocator: &'a Allocator) -> Self {
Vec::new_in(allocator)
}
}

impl<'a, T: Dummy<'a>> Dummy<'a> for Cell<T> {
/// Create a dummy [`Cell`].
#[expect(clippy::inline_always)]
#[inline(always)]
fn dummy(allocator: &'a Allocator) -> Self {
Cell::new(Dummy::dummy(allocator))
}
}

impl<'a> Dummy<'a> for () {
#[inline(always)]
fn dummy(_allocator: &'a Allocator) {}
}

impl<'a> Dummy<'a> for bool {
#[expect(clippy::inline_always)]
#[inline(always)]
fn dummy(_allocator: &'a Allocator) -> Self {
false
}
}

impl<'a> Dummy<'a> for &'a str {
#[expect(clippy::inline_always)]
#[inline(always)]
fn dummy(_allocator: &'a Allocator) -> Self {
""
}
}

macro_rules! dummy_impl_int {
($ty:ident) => {
impl<'a> Dummy<'a> for $ty {
#[inline(always)]
fn dummy(_allocator: &'a Allocator) -> Self {
0
}
}
};
}

dummy_impl_int!(u8);
dummy_impl_int!(u16);
dummy_impl_int!(u32);
dummy_impl_int!(u64);
dummy_impl_int!(u128);
dummy_impl_int!(usize);
dummy_impl_int!(i8);
dummy_impl_int!(i16);
dummy_impl_int!(i32);
dummy_impl_int!(i64);
dummy_impl_int!(i128);
dummy_impl_int!(isize);

macro_rules! dummy_impl_float {
($ty:ident) => {
impl<'a> Dummy<'a> for $ty {
#[inline(always)]
fn dummy(_allocator: &'a Allocator) -> Self {
0.0
}
}
};
}

dummy_impl_float!(f32);
dummy_impl_float!(f64);

macro_rules! dummy_impl_non_zero {
($ty:ident) => {
impl<'a> Dummy<'a> for num::$ty {
#[inline(always)]
fn dummy(_allocator: &'a Allocator) -> Self {
Self::MIN
}
}
};
}

dummy_impl_non_zero!(NonZeroU8);
dummy_impl_non_zero!(NonZeroU16);
dummy_impl_non_zero!(NonZeroU32);
dummy_impl_non_zero!(NonZeroU64);
dummy_impl_non_zero!(NonZeroU128);
dummy_impl_non_zero!(NonZeroUsize);
dummy_impl_non_zero!(NonZeroI8);
dummy_impl_non_zero!(NonZeroI16);
dummy_impl_non_zero!(NonZeroI32);
dummy_impl_non_zero!(NonZeroI64);
dummy_impl_non_zero!(NonZeroI128);
dummy_impl_non_zero!(NonZeroIsize);
Loading