swc-project/swc · error

Var type should be one of: Ident, Expr, Pat; got {:?}

Error message

Var type should be one of: Ident, Expr, Pat; got {:?}

What it means

The quote! macro type annotation must be a bare single-segment type path. If the annotation is a reference (&Expr), generic (Vec<Expr>), tuple, fully-qualified path, or any non-path type, the VarPos extraction fails and panics with 'Var type should be one of: Ident, Expr, Pat; got {:?}' showing the full syn::Type.

Source

Thrown at crates/swc_ecma_quote_macros/src/ctxt.rs:115

                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,
                clone: Default::default(),
                ident: var_ident.clone(),
            },
        );

View on GitHub (pinned to 5176682b65)

Solutions

  1. Use a bare path: Expr, Ident, Pat, Str, or AssignTarget without &, Option<>, or module prefixes
  2. Pass owned values; the macro clones/counts interpolations itself
  3. If you need a String interpolation, use the Str annotation instead of &str

Example fix

// before
quote!(x as &Expr; x;);

// after
quote!(x as Expr; x;);
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Writing quote!(x as &Expr), quote!(x as Option<Expr>), or quote!(x as syn::Ident) inside a macro built on swc_ecma_quote_macros.

Common situations: Rust developers naturally writing reference or generic types when hand-rolling code generation, porting from other quote implementations (syn's quote! accepts arbitrary expressions), IDE autocompleting qualified paths.

Related errors


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