Skip to content

refactor(ast): add a container macro to AST nodes #2641

@Boshen

Description

@Boshen

@overlookmotel originally said on #2640 (splitting into separate issue for clarity):

We could add an Arena trait to all AST types to prevent allocation-owning types getting into the arena and resulting in memory leaks.

The most ergonomic implementation would be a terse attribute macro #[arena].

We could also add to that macro a bunch of the boilerplate attrs which are crowding oxc_ast. It could also allow #[serde] attrs to be used without #[cfg_attr].

I know that macros can obscure meaning, and explicit code with no magic is a virtue, but personally I find the level of attributes in oxc_ast is now so high that it makes it hard to see the wood for the trees. I think this:

#[arena]
#[serde(tag = "type")]
pub struct Program<'a> {
  #[serde(flatten)]
  pub span: Span,
  // ...
}

is much easier to grok than:

#[derive(Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type", rename_all = "camelCase"))]
#[cfg_attr(all(feature = "serde", feature = "wasm"), derive(tsify::Tsify))]
pub struct Program<'a> {
  #[cfg_attr(feature = "serde", serde(flatten))]
  pub span: Span,
  // ...
}

If the macro's implementation is simple enough, and we added a comment to the top of each file saying // See 'arena' macro in 'oxc_macros' crate for attributes which are added to AST types, then that'd make it easier for people new to the codebase to understand it swiftly.

I do have one further motivation: A macro of this sort would also be very useful for AST transfer, which needs to mutate enums extensively (see current impl here).

Of course the big argument against an #[arena] macro is if it'd have a noticeable impact on compile times. If it's going to hurt that measure considerably, then probably a bad idea. Do you think that'd be the case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions