swc-project/swc · error

#[ast_node] enum cannot contain named fields or unit variant

Error message

#[ast_node] enum cannot contain named fields or unit variant

What it means

A derive-macro validation panic in the #[ast_node] deserializer expansion (enum_deserialize.rs:52). While generating tag-match arms for the enum's variants, the macro asserts each variant is a single unnamed field (a newtype variant); this panic fires for a variant that has named fields or is a unit variant, shapes the internally-tagged deserialization code generator cannot handle. The offending enum definition in the user's source is the input at fault.

Source

Thrown at crates/ast_node/src/enum_deserialize.rs:52

    let deserialize = {
        let mut all_tags: Punctuated<_, token::Comma> = Default::default();
        let tag_match_arms = data
            .variants
            .iter()
            .filter(|v| !crate::encoding::is_unknown(&v.attrs))
            .map(|variant| {
                let field_type = match variant.fields {
                    Fields::Unnamed(ref fields) => {
                        assert_eq!(
                            fields.unnamed.len(),
                            1,
                            "#[ast_node] enum cannot contain variant with multiple fields"
                        );

                        fields.unnamed.last().unwrap().ty.clone()
                    }
                    _ => {
                        unreachable!("#[ast_node] enum cannot contain named fields or unit variant")
                    }
                };
                let tags = variant
                    .attrs
                    .iter()
                    .filter_map(|attr| -> Option<VariantAttr> {
                        if !is_attr_name(attr, "tag") {
                            return None;
                        }
                        let tokens = match &attr.meta {
                            Meta::List(meta) => meta.tokens.clone(),
                            _ => {
                                panic!("#[tag] attribute must be in form of #[tag(..)]")
                            }
                        };
                        let tags = parse2(tokens).expect("failed to parse #[tag] attribute");

                        Some(tags)

View on GitHub (pinned to 5176682b65)

Solutions

  1. Refactor the #[ast_node] enum so every variant is a newtype variant with exactly one field
  2. Split data-carrying payloads into separate structs wrapped by newtype variants
  3. Emit a spanned compile_error! naming the offending variant instead of a bare panic
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/ast_node/src/enum_deserialize.rs:52 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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