swc-project/swc · error
union unsupported
Error message
union unsupported
What it means
The final match of the Decode derive only handles Data::Struct and Data::Enum; deriving it on a union hits `Data::Union(_) => panic!("union unsupported")`. Unions have no field order or tag semantics, so there is no CBOR mapping the macro could generate correctly. The sibling Encode derive has the same restriction.
Solutions
- Replace the union with a struct or enum that makes the alternative explicit.
- Or hand-write the cbor4ii Decode/Encode impls, encoding a manual tag for the active alternative.
Example fix
// before
#[derive(Encode, Decode)]
union Value {
u: u32,
f: f32,
}
// after — model the alternatives explicitly
#[derive(Encode, Decode)]
enum Value {
U(u32),
F(f32),
} Defensive patterns
Strategy: validation
Prevention
- Do not apply swc codec derives to unions; redesign as a struct or enum first.
- If a union is unavoidable (FFI), hand-write the cbor4ii Encode/Decode impls with an explicit tag.
When it happens
Trigger: #[derive(Decode)] (or swc_common::Decode via #[ast_node]) applied to a `union U { a: u32, b: f32 }`.
Common situations: Trying to give FFI/interop unions wire codecs; converting low-level memory-layout types into AST-node-like types.
Related errors
- Cannot use both #[encoding(with)] and #[encoding(ignore)]…
- enum member types must be consistent
- more than 1 unnamed member field are not allowed
- named enum unsupported
- union
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/b5a1ae8ed315c2ed.
Report an issue: GitHub.
Appendix: source
Thrown at crates/ast_node/src/encoding/decode.rs:282
};
syn::parse_quote! {
impl<'de> cbor4ii::core::dec::Decode<'de> for #ident {
#[inline]
fn decode<R: cbor4ii::core::dec::Read<'de>>(reader: &mut R)
-> Result<Self, cbor4ii::core::error::DecodeError<R::Error>>
{
#tag
let value = match tag {
#(#fields)*
#unknown_arm
};
Ok(value)
}
}
}
}
Data::Union(_) => panic!("union unsupported"),
}
}
View on GitHub (pinned to 5176682b65)