{"record":{"id":"d732cbe2f5b8a4a5","repo":"swc-project/swc","slug":"cannot-use-both-encoding-with-and-encoding-i","errorCode":null,"errorMessage":"Cannot use both #[encoding(with)] and #[encoding(ignore)] attributes on the same field","messagePattern":"Cannot use both #\\[encoding\\(with\\)\\] and #\\[encoding\\(ignore\\)\\] attributes on the same field","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ast_node/src/encoding/decode.rs","lineNumber":30,"sourceCode":"                .enumerate()\n                .map(|(idx, field)| match field.ident.as_ref() {\n                    Some(name) => name.clone(),\n                    None => {\n                        let name = format!(\"unit{idx}\");\n                        syn::Ident::new(&name, field.span())\n                    }\n                })\n                .collect::<Vec<_>>();\n\n            let fields = data.fields.iter()\n                .zip(names.iter())\n                .map(|(field, field_name)| -> syn::Stmt {\n                    let ty = &field.ty;\n                    let value: syn::Expr = match (is_with(&field.attrs), is_ignore(&field.attrs)) {\n                        (Some(with_type), false) => syn::parse_quote!(<#with_type<#ty> as cbor4ii::core::dec::Decode<'_>>::decode(reader)?.0),\n                        (None, false) => syn::parse_quote!(<#ty as cbor4ii::core::dec::Decode<'_>>::decode(reader)?),\n                        (None, true) => syn::parse_quote!(<#ty as Default>::default()),\n                        (Some(_), true) => panic!(\"Cannot use both #[encoding(with)] and #[encoding(ignore)] attributes on the same field\")\n                    };\n\n                    syn::parse_quote!{\n                        let #field_name = #value;\n                    }\n                });\n            let build_struct: syn::Expr = if is_named {\n                syn::parse_quote! { #ident { #(#names),* } }\n            } else {\n                syn::parse_quote! { #ident ( #(#names),* ) }\n            };\n\n            let count = data\n                .fields\n                .iter()\n                .filter(|field| !is_ignore(&field.attrs))\n                .count();\n            let head: Option<syn::Stmt> = (count != 1).then(|| {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/ast_node/src/encoding/decode.rs#L12-L48","documentation":"The Decode derive from swc's ast_node crate (re-exported as swc_common::Decode and applied by #[ast_node] under the encoding-impl feature) generates CBOR decoding via cbor4ii. While expanding a struct it maps each field's #[encoding(...)] attributes: `with = \"Path\"` delegates decoding to a wrapper type, and `ignore` substitutes Default::default(). The two are mutually exclusive per field; carrying both makes the attribute match hit the (Some(_), true) arm and panic during macro expansion, failing the build.","triggerScenarios":"Deriving Decode (directly or via #[ast_node] with the encoding-impl feature) on a struct where one field has both #[encoding(with = \"SomeCodec\")] and #[encoding(ignore)] — whether combined in one attribute (#[encoding(with = \"SomeCodec\", ignore)]) or split across two #[encoding(...)] attributes on that field.","commonSituations":"Copy-pasting AST node definitions while toggling codec behavior; merging upstream swc_ecma_ast changes where a field switched from encoded to ignored and both attributes survived the merge.","solutions":["Pick one behavior per field: keep #[encoding(ignore)] to skip the field (decoded as Default), or #[encoding(with = \"...\")] to use the custom codec, and delete the other.","If the field should sometimes default, implement that inside the with-type's Decode impl instead of stacking attributes."],"exampleFix":"// before\n#[derive(Encode, Decode)]\npub struct Node {\n    #[encoding(with = \"IdAsText\")]\n    #[encoding(ignore)]\n    pub id: u32,\n}\n\n// after — drop the conflicting codec, keep ignore\n#[derive(Encode, Decode)]\npub struct Node {\n    #[encoding(ignore)]\n    pub id: u32,\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep at most one #[encoding(...)] behavior per field: `with` or `ignore`, never both.","Run cargo check immediately after editing AST-node attributes; these panics are compile-time and cheap to catch.","When merging upstream swc AST changes, diff the attributes per field so removed codecs do not resurrect alongside new ones."],"tags":["rust","proc-macro","derive","decode","cbor","attributes"],"backgroundTag":"derive-macro-attribute-conflict","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"}