{"record":{"id":"e6f5a9238c186f41","repo":"swc-project/swc","slug":"union","errorCode":null,"errorMessage":"union","messagePattern":"union","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_eq_ignore_macros/src/lib.rs","lineNumber":98,"sourceCode":"                let arm = self.make_arm_from_fields(parse_quote!(Self), &s.fields);\n\n                parse_quote!(match (self, other) { #arm })\n            }\n            Data::Enum(e) => {\n                //\n                let mut arms = Punctuated::<_, Token![,]>::default();\n                for v in &e.variants {\n                    let vi = &v.ident;\n                    let arm = self.make_arm_from_fields(parse_quote!(Self::#vi), &v.fields);\n\n                    arms.push(arm);\n                }\n\n                arms.push(parse_quote!(_ => false));\n\n                parse_quote!(match (self, other) { #arms })\n            }\n            Data::Union(_) => unimplemented!(\"union\"),\n        }\n    }\n\n    fn make_arm_from_fields(&self, pat_path: Path, fields: &Fields) -> Arm {\n        let mut l_pat_fields = Punctuated::<_, Token![,]>::default();\n        let mut r_pat_fields = Punctuated::<_, Token![,]>::default();\n        let mut exprs = Vec::new();\n\n        for (i, field) in fields\n            .iter()\n            .enumerate()\n            .filter(|(_, f)| !(self.ignore_field)(f))\n        {\n            let method_name =\n                if field.attrs.iter().any(|attr| {\n                    attr.path().is_ident(\"not_spanned\") || attr.path().is_ident(\"use_eq\")\n                }) {\n                    Ident::new(\"eq\", Span::call_site())","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_eq_ignore_macros/src/lib.rs#L80-L116","documentation":"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\")`.","triggerScenarios":"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).","commonSituations":"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.","solutions":["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)","Model the data as a struct or enum instead of a union so the derive works","If only equality is needed, derive PartialEq/Eq separately where sound and skip EqIgnoreSpan for the union"],"exampleFix":"// before\n#[derive(EqIgnoreSpan)]\nunion U { a: u32, b: f32 }\n\n// after\nunion U { a: u32, b: f32 }\n\nimpl EqIgnoreSpan for U {\n    fn eq_ignore_span(&self, other: &Self) -> bool {\n        // SAFETY: unions must be compared through their active field;\n        // compare via the discriminant/active variant your code maintains.\n        unsafe { self.a.to_bits() == other.a.to_bits() }\n    }\n}","handlingStrategy":"validation","validationCode":"// Grep-level guard in CI for repos using swc derives:\n// rg -U '#[derive\\([^)]*EqIgnoreSpan[^)]*)][\\s\\S]{0,80}\\bunion\\b' src/\n// Any match must be reviewed and converted.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never apply EqIgnoreSpan to unions; implement it manually where needed","Prefer enum modeling over union for types that need span-ignoring equality","Run cargo check in CI with -D warnings so derive panics fail the build immediately"],"tags":["rust","proc-macro","derive","union","compile-time"],"backgroundTag":"derive-macro-unsupported-type","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}