{"record":{"id":"b5a1ae8ed315c2ed","repo":"swc-project/swc","slug":"union-unsupported","errorCode":null,"errorMessage":"union unsupported","messagePattern":"union unsupported","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ast_node/src/encoding/decode.rs","lineNumber":282,"sourceCode":"            };\n\n            syn::parse_quote! {\n                impl<'de> cbor4ii::core::dec::Decode<'de> for #ident {\n                    #[inline]\n                    fn decode<R: cbor4ii::core::dec::Read<'de>>(reader: &mut R)\n                        -> Result<Self, cbor4ii::core::error::DecodeError<R::Error>>\n                    {\n                        #tag\n                        let value = match tag {\n                            #(#fields)*\n                            #unknown_arm\n                        };\n                        Ok(value)\n                    }\n                }\n            }\n        }\n        Data::Union(_) => panic!(\"union unsupported\"),\n    }\n}\n","sourceCodeStart":264,"sourceCodeEnd":285,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/ast_node/src/encoding/decode.rs#L264-L285","documentation":"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.","triggerScenarios":"#[derive(Decode)] (or swc_common::Decode via #[ast_node]) applied to a `union U { a: u32, b: f32 }`.","commonSituations":"Trying to give FFI/interop unions wire codecs; converting low-level memory-layout types into AST-node-like types.","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."],"exampleFix":"// before\n#[derive(Encode, Decode)]\nunion Value {\n    u: u32,\n    f: f32,\n}\n\n// after — model the alternatives explicitly\n#[derive(Encode, Decode)]\nenum Value {\n    U(u32),\n    F(f32),\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["rust","proc-macro","derive","decode","union"],"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-14T00:17:10.932Z"}