swc-project/swc · error
#[derive(Spanned)] requires a field to get span from
Error message
#[derive(Spanned)] requires a field to get span from
What it means
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.
Source
Thrown at crates/ast_node/src/spanned.rs:146
item.with_generics(input.generics)
}
fn make_body_for_variant(v: &VariantBinder<'_>, bindings: Vec<BindedField<'_>>) -> Box<Expr> {
/// `swc_common::Spanned::span(#field)`
fn simple_field(field: &dyn ToTokens) -> Box<Expr> {
Box::new(parse_quote_spanned! (def_site() => {
swc_common::Spanned::span(#field)
}))
}
if is_unknown(v.attrs()) {
return Box::new(parse_quote_spanned! { v.data().span() => {
swc_common::DUMMY_SP
}});
}
if bindings.is_empty() {
panic!("#[derive(Spanned)] requires a field to get span from")
}
if bindings.len() == 1 {
if let Fields::Unnamed(..) = *v.data() {
// Call self.0.span()
return simple_field(&bindings[0]);
}
}
// Handle #[span] attribute.
if let Some(f) = bindings
.iter()
.find(|b| has_empty_span_attr(&b.field().attrs))
{
//TODO: Verify that there's no more #[span]
return simple_field(f);
}
View on GitHub (pinned to 5176682b65)
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
Example fix
// before
#[derive(Spanned)]
struct Empty;
// after
struct Empty;
impl swc_common::Spanned for Empty {
fn span(&self) -> swc_common::Span {
swc_common::DUMMY_SP
}
} Defensive patterns
Strategy: validation
Prevention
- 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
When it happens
Trigger: Deriving Spanned on `struct Marker;` or any empty struct.
Common situations: Adding Spanned to marker/error sentinel types while building custom AST nodes on top of swc_common.
Related errors
- Unknown span attribute: {kind:?}
- Unknown span attribute
- #[derive(Spanned)]: cannot determine span field to use for {
- #[derive(Spanned)]: #[span(lo)] and #[span(hi)] is required
- An element descriptor's .kind property must be either "metho
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/713176e69ce22820.
Report an issue: GitHub.