swc-project/swc · error
more than 1 unnamed member field are not allowed
Error message
more than 1 unnamed member field are not allowed
What it means
The Encode derive (swc_common::Encode from the ast_node crate) classifies every non-#[encoding(unknown)] enum variant by shape: named fields map to Struct, exactly one unnamed field to One, no fields to Unit. A tuple variant with two or more fields has no representation in the generated CBOR tag encoding, so macro expansion panics with 'more than 1 unnamed member field are not allowed'. This is a compile-time constraint on the type definition, not a runtime encoding failure.
Source
Thrown at crates/ast_node/src/encoding/encode.rs:62
-> Result<(), cbor4ii::core::error::EncodeError<W::Error>>
{
#head
#(#fields)*;
Ok(())
}
}
}
}
Data::Enum(data) => {
let enum_type = data.variants.iter().filter(|v| !is_unknown(&v.attrs)).fold(
None,
|mut sum, next| {
let ty = match &next.fields {
syn::Fields::Named(_) => EnumType::Struct,
syn::Fields::Unnamed(fields) if fields.unnamed.len() == 1 => EnumType::One,
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();
View on GitHub (pinned to 5176682b65)
Solutions
- Reshape the variant into a named struct variant: `Pair { lo: u32, hi: u32 }`.
- Or wrap the payloads in a single struct/newtype: `Pair(RangeFields)`.
- Leave two-field tuples to the dedicated #[encoding(unknown)] tag+value variant, the only 2-field tuple allowed.
Example fix
// before
#[derive(Encode, Decode)]
enum Bound {
Pair(u32, u32), // tuple with 2 fields
}
// after — named struct variant
#[derive(Encode, Decode)]
enum Bound {
Pair { lo: u32, hi: u32 },
} Defensive patterns
Strategy: validation
Prevention
- Model multi-value enum payloads as named struct variants before deriving swc CBOR codecs.
- cargo check the defining crate right after adding a variant to catch shape violations at authoring time.
- Remember the only allowed tuple shapes: single-field newtype variants, and the 2-field #[encoding(unknown)] variant.
When it happens
Trigger: Deriving Encode (or #[ast_node] with the encoding-impl feature) on an enum that contains a non-unknown variant with 2+ positional fields, such as `Pair(u32, u32)`.
Common situations: Porting serde-friendly enums into swc AST node types; adding a quick variant without reshaping it into a supported form.
Related errors
- enum member types must be consistent: {:?}
- more than 1 unnamed member field are not allowed
- enum member types must be consistent: {:?}
- unknown member must be a tag and a value
- named enum unsupported
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/389231927254a41c.
Report an issue: GitHub.