{"record":{"id":"dcf4e2d58b4f6fa2","repo":"denoland/deno","slug":"cannot-use-serde-on-unit-variant","errorCode":null,"errorMessage":"Cannot use serde on unit variant","messagePattern":"Cannot use serde on unit variant","errorType":"validation","errorClass":"syn::Error","httpStatus":null,"severity":"error","filePath":"libs/ops/conversion/to_v8/enum.rs","lineNumber":219,"sourceCode":"          .unnamed\n          .into_iter()\n          .enumerate()\n          .map(|(i, field)| format_ident!(\"__{}\", i, span = field.span()))\n          .collect();\n        let indices: Vec<_> = (0..len as u32).collect();\n\n        Ok(quote! {\n          let __arr = ::deno_core::v8::Array::new(__scope, #len as i32);\n          #(\n            let __val = ::deno_core::serde_v8::to_v8(__scope, #field_idents)\n              .map_err(::deno_error::JsErrorBox::from_err)?;\n            __arr.set_index(__scope, #indices, __val);\n          )*\n          Ok::<_, ::deno_error::JsErrorBox>(__arr.into())\n        })\n      }\n    }\n    Fields::Unit => Err(Error::new(span, \"Cannot use serde on unit variant\")),\n  }\n}\n\n#[derive(Default)]\nenum EnumMode {\n  #[default]\n  ExternallyTagged,\n  InternallyTagged {\n    tag: TokenStream,\n  },\n  AdjacentlyTagged {\n    tag: TokenStream,\n    content: TokenStream,\n  },\n  Untagged,\n}\n\nimpl EnumMode {","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/libs/ops/conversion/to_v8/enum.rs#L201-L237","documentation":"In #[derive(ToV8)] on enums, #[to_v8(serde)] on a variant delegates that variant's conversion to serde_v8. A unit variant has no field value to hand to serde (it serializes as just its tag name), so the derive rejects serde on Fields::Unit with this compile error instead of generating code that would not compile or would double-encode the tag.","triggerScenarios":"Annotating a unit variant with #[to_v8(serde)], e.g. #[to_v8(serde)] None, inside a #[derive(ToV8)] enum.","commonSituations":"Applying #[to_v8(serde)] at the variant level to 'keep serialization consistent' across all variants; copy-pasting the attribute from a data-carrying variant to a unit one; IDE auto-completing the attribute onto every variant.","solutions":["Remove #[to_v8(serde)] from the unit variant — unit variants are already serialized correctly as their (optionally renamed) tag string.","If the variant must carry serde-encoded data, change it into a newtype variant (e.g. None(Option<u32>)) and keep serde on that.","Use #[to_v8(rename = \"...\")] if the goal was only to control the emitted tag name."],"exampleFix":"// before\n#[derive(ToV8)]\nenum Result {\n  #[to_v8(serde)]\n  Ok,          // error: Cannot use serde on unit variant\n  Error(String),\n}\n\n// after\n#[derive(ToV8)]\nenum Result {\n  Ok,\n  Error(String),\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Apply #[to_v8(serde)] only to variants that carry data; unit variants already emit their tag name correctly.","Use #[to_v8(rename = \"...\")] when the goal is just a different tag string."],"tags":["proc-macro","derive","to-v8","serde","enum","compile-time","deno-core"],"backgroundTag":"derive-macro-unsupported-shape","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}