swc-project/swc · error

#[ast_serde] on enum does not accept any argument

Error message

#[ast_serde] on enum does not accept any argument

What it means

Compile-time panic from the `ast_serde` proc-macro attribute in swc's ast_node crate. When `#[ast_serde]` is applied to an enum, the macro only generates `#[derive(Serialize, DeserializeEnum)] #[serde(untagged)]` and rejects any attribute arguments outright; arguments are only parsed (as `ast_node_macro::Args`) for structs. Passing anything on an enum aborts macro expansion.

Source

Thrown at crates/ast_node/src/lib.rs:106

///
/// so the deserializer can decide which variant to use.
///
///
/// `#[tag]` also supports wildcard like `#[tag("*")]`. You can use this if
/// there are two many variants.
#[proc_macro_attribute]
pub fn ast_serde(
    args: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let input: DeriveInput = parse(input).expect("failed to parse input as a DeriveInput");

    // we should use call_site
    let mut item = TokenStream::new();
    match input.data {
        Data::Enum(..) => {
            if !args.is_empty() {
                panic!("#[ast_serde] on enum does not accept any argument")
            }

            item.extend(quote!(
                #[derive(::serde::Serialize, ::swc_common::DeserializeEnum)]
                #[serde(untagged)]
                #input
            ));
        }
        _ => {
            let args: Option<ast_node_macro::Args> = if args.is_empty() {
                None
            } else {
                Some(parse(args).expect("failed to parse args of #[ast_serde]"))
            };

            let serde_tag = match input.data {
                Data::Struct(DataStruct {
                    fields: Fields::Named(..),

View on GitHub (pinned to 5176682b65)

Solutions

  1. Remove all arguments and use a bare `#[ast_serde]` on the enum
  2. If you need custom serde behavior for the enum, write manual Serialize/Deserialize impls instead of the macro
  3. Keep struct-only arguments (e.g. field tags) on struct applications only

Example fix

// before
#[ast_serde(tag = "type")]
pub enum Expr {
    Num(f64),
}

// after
#[ast_serde]
pub enum Expr {
    Num(f64),
}
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Writing `#[ast_serde(some_arg)]` or any parenthesized/list argument on an enum item; the enum branch checks `!args.is_empty()` and panics before generating code.

Common situations: Copying a `#[ast_serde(...)]` invocation that was written for a struct onto a new enum; assuming enums and structs share the same argument grammar; upgrading swc_ast_node versions where accepted syntax shifted.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/d74077d7bd3ca772. Report an issue: GitHub.