{"record":{"id":"d42d210f396743bb","repo":"swc-project/swc","slug":"unsupported-discriminant-type","errorCode":null,"errorMessage":"unsupported discriminant type","messagePattern":"unsupported discriminant type","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ast_node/src/encoding/decode.rs","lineNumber":160,"sourceCode":"                        _ => panic!(\"named enum unsupported\"),\n                    }\n                });\n\n            if matches!(enum_type, EnumType::Struct) {\n                assert!(\n                    unknown_arm.is_none(),\n                    \"struct enum does not allow unknown variants\"\n                );\n            }\n\n            let mut discriminant: u32 = 0;\n            let fields = iter\n                .map(|field| -> syn::Arm {\n                    match field.discriminant.as_ref() {\n                        Some((_, syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }))) => {\n                            discriminant = lit.base10_parse::<u32>().unwrap();\n                        },\n                        Some(_) => panic!(\"unsupported discriminant type\"),\n                        None => (),\n                    };\n                    discriminant += 1;\n                    let idx = discriminant as u64;\n                    let name = &field.ident;\n\n                    assert!(!is_unknown(&field.attrs), \"unknown member must be first\");\n\n                    match enum_type {\n                        EnumType::Unit => {\n                            assert!(is_with(&field.attrs).is_none(), \"unit member is not allowed with type\");\n                            syn::parse_quote!{\n                                #idx => #ident::#name,\n                            }\n                        },\n                        EnumType::One => {\n                            let val_ty = &field.fields.iter().next().unwrap().ty;\n                            let value: syn::Expr = match is_with(&field.attrs) {","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/ast_node/src/encoding/decode.rs#L142-L178","documentation":"The generated CBOR codec indexes each variant with a running u32 counter; an explicit discriminant is honored only when it is an integer literal (syn::Lit::Int) that base10_parse can read as u32 — the macro does no const evaluation. `A = SOME_CONST`, `A = -1`, or any other expression hits `Some(_) => panic!(\"unsupported discriminant type\")`. Values that overflow u32 additionally fail inside the following unwrap().","triggerScenarios":"derive(Decode) on an enum where a variant discriminant is a path/constant, a negative number, or any non-literal expression instead of a plain u32 integer literal.","commonSituations":"Sharing discriminant constants between the wire protocol and Rust code; refactoring magic numbers into consts and forgetting the macro only accepts literals.","solutions":["Inline the value as an integer literal: `Num = 3`.","Or remove explicit discriminants and let the macro assign sequential indices.","Keep external consumers in sync by asserting the encoded tag value in a test instead of sharing a const into the enum definition."],"exampleFix":"// before\nconst KIND_NUM: u32 = 3;\nenum Tagged {\n    Str = 1,\n    Num = KIND_NUM,\n}\n\n// after — plain u32 literals\nenum Tagged {\n    Str = 1,\n    Num = 3,\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Write enum discriminants as plain u32 integer literals in the enum definition.","Share wire values via tests that assert the encoded tag, not via consts spliced into the enum.","Avoid negative, float, or const-path discriminants on codec-derived enums."],"tags":["rust","proc-macro","derive","decode","enum","discriminant"],"backgroundTag":"enum-discriminant-not-literal","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}