swc-project/swc · error

derive(Merge) does not support a union

Error message

derive(Merge) does not support a union

What it means

A derive-macro validation abort in expand of swc_config_macro's Merge derive (merge.rs:23). Mirroring the enum case, when the annotated DeriveInput is a union there is no meaningful field-wise merge to generate, so the macro expansion panics with 'derive(Merge) does not support a union'. The union type annotated with #[derive(Merge)] is the input at fault.

Source

Thrown at crates/swc_config_macro/src/merge.rs:23

pub fn expand(input: DeriveInput) -> TokenStream {
    match &input.data {
        syn::Data::Struct(s) => {
            let body = call_merge_for_fields(&quote!(self), &s.fields);
            let body = join_stmts(&body);

            let ident = &input.ident;
            parse_quote!(
                #[automatically_derived]
                impl swc_config::merge::Merge for #ident {
                    fn merge(&mut self, _other: Self) {
                        #body
                    }
                }
            )
        }
        syn::Data::Enum(_) => unimplemented!("derive(Merge) does not support an enum"),
        syn::Data::Union(_) => unimplemented!("derive(Merge) does not support a union"),
    }
}

fn call_merge_for_fields(obj: &dyn ToTokens, fields: &Fields) -> Vec<Stmt> {
    fn call_merge(obj: &dyn ToTokens, idx: usize, f: &Field) -> Expr {
        let r = quote!(_other);

        let l = access_field(obj, idx, f);
        let r = access_field(&r, idx, f);
        parse_quote!(swc_config::merge::Merge::merge(&mut #l, #r))
    }

    match fields {
        Fields::Named(fs) => fs
            .named
            .iter()
            .enumerate()
            .map(|(idx, f)| call_merge(obj, idx, f))

View on GitHub (pinned to 5176682b65)

Solutions

  1. Convert the union to a struct if config merging is needed
  2. Write a manual Merge impl if union-specific semantics are desired
  3. Produce a spanned compile_error! identifying the union instead of a panic
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_config_macro/src/merge.rs:23 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/e08223e7eb5d096c. Report an issue: GitHub.