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

  1. Change the annotation to one of Ident, Expr, Pat, Str, AssignTarget
  2. For non-expression nodes (statements, declarations), restructure the macro to build them from expressions or use the builder API instead of interpolation
  3. 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

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


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/5e2bc8514732fdea. Report an issue: GitHub.