denoland/deno · error · syn::Error
Unions are not supported
Error message
Unions are not supported
What it means
#[derive(ToV8)] routes Data::Struct and Data::Enum inputs to generated ToV8 impls, but Data::Union is rejected with this error on the item's span. A union's active-field semantics cannot be represented when converting to a V8 value, so the derive fails fast rather than guessing a layout.
Source
Thrown at libs/ops/conversion/to_v8/mod.rs:41
let out = match input.data {
Data::Struct(data) => {
let fields = destruct_fields(&data.fields)?;
let body = r#struct::get_body(span, data)?;
create_impl(
ident,
&generics,
quote! {
let Self #fields = self;
#body
},
)
}
Data::Enum(data) => {
create_impl(ident, &generics, r#enum::get_body(span, input.attrs, data)?)
}
Data::Union(_) => return Err(Error::new(span, "Unions are not supported")),
};
Ok(out)
}
fn destruct_fields(fields: &Fields) -> Result<TokenStream, Error> {
match fields {
Fields::Named { 0: named } => {
let fields = named
.named
.iter()
.map(|field| field.ident.as_ref().unwrap());
Ok(quote! {
{
#(#fields),*
}
})View on GitHub (pinned to 9ad36f7a2c)
Solutions
- Model the 'one of several layouts' as an enum with a variant per alternative, which serializes naturally to V8.
- Keep unions behind manual conversion: write an op that reads the union and returns a normal struct/array to JS.
Example fix
// before
#[derive(ToV8)]
union Raw {
a: u32,
b: f32,
} // error: Unions are not supported
// after
#[derive(ToV8)]
enum Raw {
A(u32),
B(f32),
} Defensive patterns
Strategy: validation
Prevention
- Unions cannot be exposed to JS via conversion derives — convert them to enums or wrap them behind a hand-written op.
- Review any blanket derive lists in preludes when FFI unions live in the same module.
When it happens
Trigger: Annotating a union with #[derive(ToV8)], e.g. #[derive(ToV8)] union Raw { a: u32, b: f32 } returned or accepted by an op.
Common situations: Shared derive preludes applied to every type in a module including FFI unions; refactoring a struct into a union while keeping its derives.
Related errors
- Unions are not supported
- Cannot use serde on unit variant
- Cannot combine `untagged` with `tag` or `content`
- `content` requires `tag` to be specified
- Unit structs cannot be destructured
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/e46ffb66d8da00c6.
Report an issue: GitHub.