-
-
Notifications
You must be signed in to change notification settings - Fork 932
Description
@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
Labels
Type
Fields
Give feedbackPriority