{"record":{"id":"91f26175634b0bd5","repo":"swc-project/swc","slug":"derive-fromvariant-requires-all-variants-to-be","errorCode":null,"errorMessage":"#[derive(FromVariant)] requires all variants to be tuple with exactly one field","messagePattern":"#\\[derive\\(FromVariant\\)\\] requires all variants to be tuple with exactly one field","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/from_variant/src/lib.rs","lineNumber":61,"sourceCode":"    }: DeriveInput,\n) -> Vec<ItemImpl> {\n    let variants = match data {\n        Data::Enum(DataEnum { variants, .. }) => variants,\n        _ => panic!(\"#[derive(FromVariant)] only works for an enum.\"),\n    };\n\n    let mut from_impls: Vec<ItemImpl> = Vec::new();\n\n    for v in variants {\n        if is_ignored(&v.attrs) {\n            continue;\n        }\n\n        let variant_name = v.ident;\n        match v.fields {\n            Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {\n                if unnamed.len() != 1 {\n                    panic!(\n                        \"#[derive(FromVariant)] requires all variants to be tuple with exactly \\\n                         one field\"\n                    )\n                }\n                let field = unnamed.into_iter().next().unwrap();\n\n                let variant_type = &field.ty;\n\n                let from_impl: ItemImpl = parse_quote!(\n                    impl From<#variant_type> for #ident {\n                        fn from(v: #variant_type) -> Self {\n                            #ident::#variant_name(v)\n                        }\n                    }\n                );\n\n                let from_impl = from_impl.with_generics(generics.clone());\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/from_variant/src/lib.rs#L43-L79","documentation":"Compile-time panic from `#[derive(FromVariant)]` (crates/from_variant). Each non-ignored variant must be a tuple variant with exactly one field, because the generated `From<T>` impl constructs `#ident::#variant_name(v)` with the single payload. A tuple variant whose unnamed field list has length != 1 panics at expansion.","triggerScenarios":"`enum E { A(Expr, Span) }` or `enum E { A() }` with the derive applied; `unnamed.len() != 1` triggers the panic.","commonSituations":"Growing a variant to carry extra metadata (span, context) alongside its main payload while using FromVariant.","solutions":["Reduce the variant to a single field: `A(Expr)`","Bundle extra data into one payload struct: `A(ExprWithSpan)`","Exclude the variant with `#[from_variant(ignore)]` if it should not get a From impl"],"exampleFix":"// before\n#[derive(FromVariant)]\nenum Node {\n    Expr(Expr, Span),\n}\n\n// after\n#[derive(FromVariant)]\nenum Node {\n    Expr(Expr),\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep every FromVariant variant a one-field tuple variant","Bundle extra data into a single payload struct type","Use `#[from_variant(ignore)]` for variants that must not convert"],"tags":["rust","proc-macro","swc","derive","enum-variant","compile-time"],"backgroundTag":"proc-macro-misuse","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"}