Hmbown/CodeWhale · error · ValueError

could not parse match block after {signature!r}

Error message

could not parse match block after {signature!r}

What it means

extract_match_block scans forward from the '{' after the first `match` keyword following a signature, counting brace depth; this error means depth never returned to zero before end-of-file. Practically, the scanned region opened more braces than it closed — most often a '{' or '}' inside a string literal of a parse arm, because the scanner does not understand Rust strings or comments. It is a parse-shape failure of the source the checker reads, not ordinary registry drift.

Source

Thrown at scripts/check-provider-registry.py:101

    return source[start:end]


def extract_match_block(
    source: str, signature: str, context: str, start: int = 0
) -> str:
    start = require_index(source, signature, context, start)
    match_start = require_index(source, "match", f"match block after {signature!r}", start)
    brace_start = require_index(source, "{", f"match block after {signature!r}", match_start)
    depth = 0
    for index in range(brace_start, len(source)):
        char = source[index]
        if char == "{":
            depth += 1
        elif char == "}":
            depth -= 1
            if depth == 0:
                return source[brace_start + 1 : index]
    raise ValueError(f"could not parse match block after {signature!r}")


def parse_aliases_for_variant(source: str, enum_name: str, variant: str, context: str) -> set[str]:
    # `ProviderKind`'s enum + identity impl (incl. `parse`) live in
    # provider_kind.rs after the config module split; read the impl from there
    # regardless of the file the caller passed for other lookups.
    if enum_name == "ProviderKind":
        source = read(PROVIDER_KIND_RS)
        context = "crates/config/src/provider_kind.rs"
    impl_start = require_index(source, f"impl {enum_name}", context)
    block = extract_match_block(
        source,
        "pub fn parse(value: &str) -> Option<Self>",
        context,
        impl_start,
    )
    match_arm = re.search(
        rf'((?:"[^"]+"\s*\|\s*)*"[^"]+")\s*=>\s*Some\(Self::{variant}\)',

View on GitHub (pinned to 8880682c63)

Solutions

  1. Inspect the text between the `pub fn parse...` signature and end-of-file for unbalanced braces, especially inside string literals
  2. Rewrite the offending literal to avoid brace characters
  3. If the parse function's shape changed, update the needles or logic in extract_match_block within scripts/check-provider-registry.py

Example fix

// before (provider_kind.rs, inside parse)
"curly{{id" => Some(Self::Custom),  // stray brace breaks the naive depth count
// after
"curly-id" => Some(Self::Custom),
Defensive patterns

Strategy: try-catch

Validate before calling

block = extract_match_block(source, 'pub fn parse(value: &str) -> Option<Self>', context)
assert block.count('{') == block.count('}'), 'unbalanced braces in match block'

Try / catch

try:
    aliases = parse_aliases_for_variant(source, enum, variant, context)
except ValueError as e:
    report_parse_failure(str(e)); raise

Prevention

When it happens

Trigger: An alias or id literal inside the enum's `parse` match contains an unbalanced brace; the file is truncated so the match block never closes; code between the signature and the real match introduces braces that desynchronize the naive depth count.

Common situations: Unusually-shaped provider alias strings; partial copies of provider files; macro-generated arms that confuse the scanner.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@8880682c63 (2026-08-16). Data as JSON: /api/errors/53565a8a8b0a52ac. Report an issue: GitHub.