swc-project/swc · error

StringEnum: Cannot determine string value of this variant

Error message

StringEnum: Cannot determine string value of this variant

What it means

Compile-time panic from `#[derive(StringEnum)]` (crates/string_enum). The macro derives the wire string for each variant by scanning its doc comments for a line that, after trimming, starts and ends with a backtick (e.g. `/// \`use strict\``); the value is the backticked content. If no doc line matches, the variant has no string value and the macro panics.

Source

Thrown at crates/string_enum/src/lib.rs:113

    );

    item.with_generics(i.generics.clone())
}

fn get_str_value(attrs: &[Attribute]) -> String {
    // TODO: Accept multiline string
    let docs: Vec<_> = attrs.iter().filter_map(doc_str).collect();
    for raw_line in docs {
        let line = raw_line.trim();
        if line.starts_with('`') && line.ends_with('`') {
            let mut s: String = line.split_at(1).1.into();
            let new_len = s.len() - 1;
            s.truncate(new_len);
            return s;
        }
    }

    panic!("StringEnum: Cannot determine string value of this variant")
}

fn make_from_str(i: &DeriveInput) -> ItemImpl {
    let arms = Binder::new_from(i)
        .variants()
        .into_iter()
        .map(|v| {
            // Qualified path of variant.
            let qual_name = v.qual_path();

            let str_value = get_str_value(v.attrs());

            let mut pat: Pat = Pat::Lit(ExprLit {
                attrs: Default::default(),
                lit: Lit::Str(LitStr::new(&str_value, Span::call_site())),
            });

            // Handle `string_enum(alias("foo"))`

View on GitHub (pinned to 5176682b65)

Solutions

  1. Add a doc line that is exactly the backticked value: `/// \"value\"` wrapped in backticks
  2. Ensure the trimmed line both starts and ends with a backtick and stays on one line (multiline values are unsupported per the TODO)

Example fix

// before
/// indicates script
Script,

// after
/// `script`
Script,
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: A variant with no doc comment at all; a doc comment without backticks (`/// use strict`); prose docs where the backticked token is only part of the line (`/// indicates \`strict\` mode`).

Common situations: Adding new variants to string-mapped enums (e.g. source types, module kinds) and writing normal prose docs instead of the exact backtick convention; doc-formatters (rustfmt/doc comment style) altering the line.

Related errors


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