swc-project/swc · error

enum cannot be empty

Error message

enum cannot be empty

What it means

Compile-time panic in the ast_node encoding derive (decode side): the tagged-encoding expansion found an enum with no usable variants (empty enum), which the derive cannot generate a decoder for. It fires during macro expansion of #[ast_node]/encoding derives, not at runtime.

Source

Thrown at crates/ast_node/src/encoding/decode.rs:104

                        syn::Fields::Unit => EnumType::Unit,
                        syn::Fields::Unnamed(_) => {
                            panic!("more than 1 unnamed member field are not allowed")
                        }
                    };
                    match (*sum.get_or_insert(ty), ty) {
                        (EnumType::Struct, EnumType::Struct)
                        | (EnumType::Struct, EnumType::Unit)
                        | (EnumType::Unit, EnumType::Unit)
                        | (EnumType::One, EnumType::One) => (),
                        (EnumType::Unit, EnumType::One)
                        | (EnumType::One, EnumType::Unit)
                        | (_, EnumType::Struct) => sum = Some(EnumType::Struct),
                        _ => panic!("enum member types must be consistent: {:?}", (sum, ty)),
                    }
                    sum
                },
            );
            let enum_type = enum_type.expect("enum cannot be empty");
            let mut iter = data.variants.iter().peekable();

            let unknown_arm: Option<syn::Arm> = iter.next_if(|variant| is_unknown(&variant.attrs))
                .map(|unknown| {
                    let name = &unknown.ident;
                    assert!(
                        unknown.discriminant.is_none(),
                        "unknown member is not allowed custom discriminant"
                    );
                    assert!(
                        is_with(&unknown.attrs).is_none(),
                        "unknown member is not allowed with type"
                    );

                    match &unknown.fields {
                        syn::Fields::Unnamed(fields) => match fields.unnamed.len() {
                            1 => {
                                assert_eq!(enum_type, EnumType::Unit);

View on GitHub (pinned to 5176682b65)

Solutions

  1. Add at least one variant to the enum
  2. Exclude the empty enum from the encoding derive
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/ast_node/src/encoding/decode.rs:104 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/365145b1c5bad065. Report an issue: GitHub.