gleam-lang/gleam · error

invalid pattern size made it to code generation

Error message

invalid pattern size made it to code generation {x:?}

What it means

Panic in bit array read code generation (compiler-core/src/exhaustiveness.rs:4007). A segment's size must be expressed as a `Pattern::BitArraySize` (or be absent with `bits`/`bytes` options for a trailing catch-all). If any other pattern kind occupies the size position by the time code generation runs, earlier validation/simplification invariants were broken and the compiler panics.

Solutions

  1. Write segment sizes as bit array size expressions: `<<x:size(n)>>` with `n` an int variable/expression, not a binding or composite pattern.
  2. Make sure the module compiles without prior type errors; suppressed errors can let malformed patterns reach codegen.
  3. Upgrade the compiler; report a bug with the `{x:?}` dump and minimal pattern if it persists.
  4. Compiler contributors: normalize sizes into `Pattern::BitArraySize` before code generation.

Example fix

// before: non-BitArraySize pattern in size slot
let <<x:size(<<8>>)>> = bits
// after: use an integer size
let <<x:size(8)>> = bits
Defensive patterns

Strategy: type-guard

Validate before calling

match segment.size() {
    None | Some(ast::Pattern::BitArraySize(_)) => ok,
    Some(_) => return Err(Error::InvalidSegmentSize),
}

Type guard

fn has_valid_size(segment: &Segment) -> bool {
    matches!(segment.size(), None | Some(ast::Pattern::BitArraySize(_)))
}

Prevention

When it happens

Trigger: Compiling a bit array pattern whose segment size is an arbitrary pattern (e.g. a variable binding or constructor pattern in the size slot) that the type checker failed to rewrite/reject; reachable via compiler bugs, patched builds, or programs that bypass validation.

Common situations: Compiler contributors touching bit array size desugaring; users on experimental compiler builds writing patterns like `<<x:size(y)>>` where `y` was expected to become a `BitArraySize` but wasn't; rare on stable releases.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of gleam-lang/gleam@49f8762da5 (2026-09-14). Data as JSON: /api/errors/e12cf8b641254fae. Report an issue: GitHub.

Appendix: source

Thrown at compiler-core/src/exhaustiveness.rs:4111

}

fn segment_size(
    segment: &TypedPatternBitArraySegment,
    pattern_variables: &HashMap<EcoString, ReadAction>,
    // If we are compiling an assignment pattern, we still need access to the
    // `type_` and `options` fields of the `segment`, so we must still pass that
    // in above. However, we need to check the correct sub-pattern of the original
    // pattern, so if they are different we set this argument to `Some`.
    pattern: Option<&TypedPattern>,
) -> ReadSize {
    let pattern = pattern.unwrap_or(&segment.value);

    match segment.size() {
        // The size of a segment must be a `BitArraySize` pattern.
        Some(ast::Pattern::BitArraySize(size)) => {
            bit_array_size(segment.unit(), pattern_variables, size)
        }
        Some(x) => panic!("invalid pattern size made it to code generation {x:?}"),

        // If a segment has the `bits`/`bytes` option and has no size, that
        // means it's the final catch all segment: we'll have to read any number
        // of bits.
        _ if segment.has_bits_option() => ReadSize::RemainingBits,
        _ if segment.has_bytes_option() => ReadSize::RemainingBytes,

        // If there's no size option we go for a default: 8 bits for int
        // segments, and 64 for anything else.
        None if segment.type_.is_int() => ReadSize::ConstantBits(8.into()),
        None => match pattern {
            ast::Pattern::Assign { pattern, .. } => {
                segment_size(segment, pattern_variables, Some(pattern))
            }

            ast::Pattern::String { value, .. } if segment.has_utf16_option() => {
                ReadSize::ConstantBits(
                    // Each utf16 code unit is 16 bits

View on GitHub (pinned to 49f8762da5)