{"record":{"id":"726ee67e5b21aea4","repo":"denoland/deno","slug":"variants-with-fields-are-not-allowed-for-enum-conv","errorCode":null,"errorMessage":"variants with fields are not allowed for enum converters","messagePattern":"variants with fields are not allowed for enum converters","errorType":"validation","errorClass":"syn::Error","httpStatus":null,"severity":"error","filePath":"libs/ops/webidl/enum.rs","lineNumber":62,"sourceCode":"\n  let as_str = quote! {\n    impl #ident {\n      pub fn as_str(&self) -> &'static str {\n        match self {\n          #(Self::#idents => #names),*,\n        }\n      }\n    }\n  };\n\n  Ok((impl_body, as_str))\n}\n\nfn get_variant_name(value: Variant) -> Result<(String, Ident), Error> {\n  let mut rename: Option<String> = None;\n\n  if !value.fields.is_empty() {\n    return Err(Error::new(\n      value.fields.span(),\n      \"variants with fields are not allowed for enum converters\",\n    ));\n  }\n\n  for attr in value.attrs {\n    if attr.path().is_ident(\"webidl\") {\n      let list = attr.meta.require_list()?;\n      let args = list.parse_args_with(\n        Punctuated::<EnumVariantArgument, Token![,]>::parse_terminated,\n      )?;\n\n      for argument in args {\n        match argument {\n          EnumVariantArgument::Rename { value, .. } => {\n            rename = Some(value.value())\n          }\n        }","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/libs/ops/webidl/enum.rs#L44-L80","documentation":"WebIDL enums are string enumerations — each variant maps to a string value. `get_variant_name` in libs/ops/webidl/enum.rs therefore rejects any variant that carries fields (tuple or struct variants), with the error on the variant's field span.","triggerScenarios":"`#[derive(WebIDL)] #[webidl(enum)] enum Msg { Plain, Wrapped(u8), Named { x: u8 } }` — the `Wrapped` and `Named` variants trigger this error; only `Plain`-style unit variants are allowed.","commonSituations":"Trying to reuse a Rust sum type (data-carrying enum) as a WebIDL enum converter; adding a payload to a variant of an already-converted enum.","solutions":["Strip the fields so every variant is a plain unit variant; WebIDL enums are sets of string values only.","If you need data-carrying variants, model them as a dictionary with a discriminator field, or write the conversion manually instead of `#[webidl(enum)]`.","Use `#[webidl(rename = \"...\")]` on variants when the JS string must differ from the Rust name."],"exampleFix":"// before\n#[derive(WebIDL)]\n#[webidl(enum)]\nenum Shape { Circle, Rect { w: f64, h: f64 } }\n\n// after\n#[derive(WebIDL)]\n#[webidl(enum)]\nenum Shape { Circle, Rect }","handlingStrategy":"validation","validationCode":"// Compile-time: `cargo check`. Keep every variant of a #[webidl(enum)] enum\n// a unit variant; use #[webidl(rename = \"...\")] for custom JS strings.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Model data-carrying sum types as a dictionary plus a discriminator, not as a webidl enum.","When adding a variant to an existing webidl enum, add it without fields."],"tags":["webidl","proc-macro","derive-macro","enum","data-carrying-variant","compile-time"],"backgroundTag":"enum-variant-with-fields","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}