swc-project/swc · error
#[derive(Spanned)]: cannot determine span field to use for {
Error message
#[derive(Spanned)]: cannot determine span field to use for {} What it means
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.
Source
Thrown at crates/ast_node/src/spanned.rs:183
// If all fields do not have `#[span(..)]`, check for field named `span`.
let has_any_span_attr = bindings.iter().any(|b| {
b.field()
.attrs
.iter()
.any(|attr| is_attr_name(attr, "span"))
});
if !has_any_span_attr {
let span_field = bindings
.iter()
.find(|b| {
b.field()
.ident
.as_ref()
.map(|ident| ident == "span")
.unwrap_or(false)
})
.unwrap_or_else(|| {
panic!(
"#[derive(Spanned)]: cannot determine span field to use for {}",
v.qual_path().into_token_stream()
)
});
return simple_field(span_field);
}
let fields: Vec<_> = bindings
.iter()
.map(|b| (b, MyField::from_field(b.field())))
.collect();
// TODO: Only one field should be `#[span(lo)]`.
let lo = fields.iter().find(|&(_, f)| f.lo);
let hi = fields.iter().find(|&(_, f)| f.hi);
match (lo, hi) {View on GitHub (pinned to 5176682b65)
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
Example fix
// before
#[derive(Spanned)]
struct Bin {
left: Expr,
right: Expr,
}
// after
#[derive(Spanned)]
struct Bin {
left: Expr,
#[span]
right: Expr,
} Defensive patterns
Strategy: validation
Prevention
- Follow the swc AST convention: first field is `pub span: Span`
- For multi-field nodes without a span field, mark exactly one field `#[span]`
When it happens
Trigger: `#[derive(Spanned)] struct Bin { left: Expr, right: Expr }` — multiple fields, no `#[span]` attribute, no `span` field.
Common situations: 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.
Related errors
- Unknown span attribute: {kind:?}
- Unknown span attribute
- #[derive(Spanned)] requires a field to get span from
- #[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/4acca02c8d15369d.
Report an issue: GitHub.