{"record":{"id":"713176e69ce22820","repo":"swc-project/swc","slug":"derive-spanned-requires-a-field-to-get-span-fr","errorCode":null,"errorMessage":"#[derive(Spanned)] requires a field to get span from","messagePattern":"#\\[derive\\(Spanned\\)\\] requires a field to get span from","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ast_node/src/spanned.rs","lineNumber":146,"sourceCode":"    item.with_generics(input.generics)\n}\n\nfn make_body_for_variant(v: &VariantBinder<'_>, bindings: Vec<BindedField<'_>>) -> Box<Expr> {\n    /// `swc_common::Spanned::span(#field)`\n    fn simple_field(field: &dyn ToTokens) -> Box<Expr> {\n        Box::new(parse_quote_spanned! (def_site() => {\n            swc_common::Spanned::span(#field)\n        }))\n    }\n\n    if is_unknown(v.attrs()) {\n        return Box::new(parse_quote_spanned! { v.data().span() => {\n            swc_common::DUMMY_SP\n        }});\n    }\n\n    if bindings.is_empty() {\n        panic!(\"#[derive(Spanned)] requires a field to get span from\")\n    }\n\n    if bindings.len() == 1 {\n        if let Fields::Unnamed(..) = *v.data() {\n            // Call self.0.span()\n            return simple_field(&bindings[0]);\n        }\n    }\n\n    //  Handle #[span] attribute.\n    if let Some(f) = bindings\n        .iter()\n        .find(|b| has_empty_span_attr(&b.field().attrs))\n    {\n        //TODO: Verify that there's no more #[span]\n        return simple_field(f);\n    }\n","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/ast_node/src/spanned.rs#L128-L164","documentation":"Compile-time panic from `#[derive(Spanned)]` (crates/ast_node/src/spanned.rs). The derive computes a span from a struct field; if the type has zero bindings (a unit struct `struct X;`, or `struct X {}` / `struct X()` with no fields) there is nothing to take a span from and the macro panics. Types flagged as `swc_ast_unknown` return DUMMY_SP before this check.","triggerScenarios":"Deriving Spanned on `struct Marker;` or any empty struct.","commonSituations":"Adding Spanned to marker/error sentinel types while building custom AST nodes on top of swc_common.","solutions":["Give the struct a field to derive the span from (e.g. a `span: Span` field)","Implement `swc_common::Spanned` manually and return `DUMMY_SP` if the type genuinely has no location"],"exampleFix":"// before\n#[derive(Spanned)]\nstruct Empty;\n\n// after\nstruct Empty;\n\nimpl swc_common::Spanned for Empty {\n    fn span(&self) -> swc_common::Span {\n        swc_common::DUMMY_SP\n    }\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never derive Spanned on empty/unit structs; give them a span field or a manual impl","For location-less types, implement swc_common::Spanned returning DUMMY_SP"],"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"}