swc-project/swc · error

union

Error message

union

What it means

The #[derive(EqIgnoreSpan)] macro from swc_eq_ignore_macros generates span-ignoring structural equality for structs and enums by building a match over each variant's fields. Rust unions have no safe field access pattern the generator can emit, so deriving on a union aborts compilation with `unimplemented!("union")`.

Solutions

  1. Remove the derive from the union type and implement EqIgnoreSpan manually (compare via a raw-memory/active-field strategy or convert the union to an enum)
  2. Model the data as a struct or enum instead of a union so the derive works
  3. If only equality is needed, derive PartialEq/Eq separately where sound and skip EqIgnoreSpan for the union

Example fix

// before
#[derive(EqIgnoreSpan)]
union U { a: u32, b: f32 }

// after
union U { a: u32, b: f32 }

impl EqIgnoreSpan for U {
    fn eq_ignore_span(&self, other: &Self) -> bool {
        // SAFETY: unions must be compared through their active field;
        // compare via the discriminant/active variant your code maintains.
        unsafe { self.a.to_bits() == other.a.to_bits() }
    }
}
Defensive patterns

Strategy: validation

Validate before calling

// Grep-level guard in CI for repos using swc derives:
// rg -U '#[derive\([^)]*EqIgnoreSpan[^)]*)][\s\S]{0,80}\bunion\b' src/
// Any match must be reviewed and converted.

Prevention

When it happens

Trigger: Placing `#[derive(EqIgnoreSpan)]` on a `union` type definition in a crate that depends on swc_eq_ignore_macros (directly or via swc_ecma_ast-adjacent macros).

Common situations: Adding the derive wholesale to a module of AST types that happens to contain a union; machine-generated code that decorates every type with all swc derives.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/e6f5a9238c186f41. Report an issue: GitHub.

Appendix: source

Thrown at crates/swc_eq_ignore_macros/src/lib.rs:98

                let arm = self.make_arm_from_fields(parse_quote!(Self), &s.fields);

                parse_quote!(match (self, other) { #arm })
            }
            Data::Enum(e) => {
                //
                let mut arms = Punctuated::<_, Token![,]>::default();
                for v in &e.variants {
                    let vi = &v.ident;
                    let arm = self.make_arm_from_fields(parse_quote!(Self::#vi), &v.fields);

                    arms.push(arm);
                }

                arms.push(parse_quote!(_ => false));

                parse_quote!(match (self, other) { #arms })
            }
            Data::Union(_) => unimplemented!("union"),
        }
    }

    fn make_arm_from_fields(&self, pat_path: Path, fields: &Fields) -> Arm {
        let mut l_pat_fields = Punctuated::<_, Token![,]>::default();
        let mut r_pat_fields = Punctuated::<_, Token![,]>::default();
        let mut exprs = Vec::new();

        for (i, field) in fields
            .iter()
            .enumerate()
            .filter(|(_, f)| !(self.ignore_field)(f))
        {
            let method_name =
                if field.attrs.iter().any(|attr| {
                    attr.path().is_ident("not_spanned") || attr.path().is_ident("use_eq")
                }) {
                    Ident::new("eq", Span::call_site())

View on GitHub (pinned to 5176682b65)