{"record":{"id":"4acca02c8d15369d","repo":"swc-project/swc","slug":"derive-spanned-cannot-determine-span-field-to","errorCode":null,"errorMessage":"#[derive(Spanned)]: cannot determine span field to use for {}","messagePattern":"#\\[derive\\(Spanned\\)\\]: cannot determine span field to use for (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ast_node/src/spanned.rs","lineNumber":183,"sourceCode":"    // If all fields do not have `#[span(..)]`, check for field named `span`.\n    let has_any_span_attr = bindings.iter().any(|b| {\n        b.field()\n            .attrs\n            .iter()\n            .any(|attr| is_attr_name(attr, \"span\"))\n    });\n    if !has_any_span_attr {\n        let span_field = bindings\n            .iter()\n            .find(|b| {\n                b.field()\n                    .ident\n                    .as_ref()\n                    .map(|ident| ident == \"span\")\n                    .unwrap_or(false)\n            })\n            .unwrap_or_else(|| {\n                panic!(\n                    \"#[derive(Spanned)]: cannot determine span field to use for {}\",\n                    v.qual_path().into_token_stream()\n                )\n            });\n\n        return simple_field(span_field);\n    }\n\n    let fields: Vec<_> = bindings\n        .iter()\n        .map(|b| (b, MyField::from_field(b.field())))\n        .collect();\n\n    // TODO: Only one field should be `#[span(lo)]`.\n    let lo = fields.iter().find(|&(_, f)| f.lo);\n    let hi = fields.iter().find(|&(_, f)| f.hi);\n\n    match (lo, hi) {","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/ast_node/src/spanned.rs#L165-L201","documentation":"Compile-time panic from `#[derive(Spanned)]` (crates/ast_node/src/spanned.rs). When no field carries any `#[span]`/`#[span(..)]` attribute, the macro falls back to using a field literally named `span`; if the struct has multiple fields and none is named `span`, it cannot pick one and panics, naming the offending type in the message.","triggerScenarios":"`#[derive(Spanned)] struct Bin { left: Expr, right: Expr }` — multiple fields, no `#[span]` attribute, no `span` field.","commonSituations":"Defining custom AST node structs with several fields and forgetting the span field or marker attribute, unlike swc_ecma_ast nodes which all carry `span: Span` first.","solutions":["Add a `pub span: Span` field (the swc AST convention)","Or mark exactly one field with a bare `#[span]` attribute to select the span source"],"exampleFix":"// before\n#[derive(Spanned)]\nstruct Bin {\n    left: Expr,\n    right: Expr,\n}\n\n// after\n#[derive(Spanned)]\nstruct Bin {\n    left: Expr,\n    #[span]\n    right: Expr,\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Follow the swc AST convention: first field is `pub span: Span`","For multi-field nodes without a span field, mark exactly one field `#[span]`"],"tags":["rust","proc-macro","swc","span","derive","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"}