swc-project/swc · error
Invalid type: {:?}
Error message
Invalid type: {:?} What it means
In SWC's quote! macro, interpolated variables are typed by annotation, and only Ident, Expr, Pat, Str, and AssignTarget are accepted single-segment type names. A simple type path with any other name (e.g. `as Stmt`, `as Module`) panics the proc macro at compile time with 'Invalid type: {:?}'.
Source
Thrown at crates/swc_ecma_quote_macros/src/ctxt.rs:110
let ident_str = ident.to_string();
let pos = match var.ty {
Some(syn::Type::Path(syn::TypePath {
qself: None,
path:
syn::Path {
leading_colon: None,
segments,
},
})) => {
let segment = segments.first().unwrap();
match segment.ident.to_string().as_str() {
"Ident" => VarPos::Ident,
"Expr" => VarPos::Expr,
"Pat" => VarPos::Pat,
"Str" => VarPos::Str,
"AssignTarget" => VarPos::AssignTarget,
_ => panic!("Invalid type: {:?}", segment.ident),
}
}
None => VarPos::Ident,
_ => {
panic!(
"Var type should be one of: Ident, Expr, Pat; got {:?}",
var.ty
)
}
};
let var_ident = syn::Ident::new(&format!("quote_var_{ident}"), ident.span());
let old = init_map.entry(pos).or_default().insert(
ident_str.clone(),
VarData {
pos,
is_counting: true,View on GitHub (pinned to 5176682b65)
Solutions
- Change the annotation to one of Ident, Expr, Pat, Str, AssignTarget
- For non-expression nodes (statements, declarations), restructure the macro to build them from expressions or use the builder API instead of interpolation
- Check the macro docs/tests for the supported list before adding new interpolations
Example fix
// before quote!(stmt_var as Stmt; console.log(stmt_var);); // after quote!(expr_var as Expr; console.log(expr_var););
Defensive patterns
Strategy: validation
Prevention
- Only annotate quote! interpolations with Ident, Expr, Pat, Str, or AssignTarget
- Build non-expression nodes with builder methods instead of interpolating them
- Compile the proc-macro crate early in CI so macro input errors surface fast
When it happens
Trigger: Writing quote! { x as Stmt; ... } or any interpolation annotated with a bare type name outside the five supported ones while authoring macros that use swc_ecma_quote_macros.
Common situations: Contributing to SWC or writing custom codegen with the quote macro, assuming any AST node type name can annotate an interpolation, porting examples that used older macro versions.
Related errors
- Var type should be one of: Ident, Expr, Pat; got {:?}
- Duplicate variable name: {ident_str}
- An element descriptor's .kind property must be either "metho
- An element descriptor's .placement property must be one of "
- A class descriptor's .kind property must be "class", but a d
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/5e2bc8514732fdea.
Report an issue: GitHub.