gleam-lang/gleam · error

invalid patterns should not reach code generation

Error message

invalid patterns should not reach code generation

What it means

An internal compiler assertion: a Pattern::Invalid node reached the Erlang pattern code generator. Invalid patterns are error-recovery placeholders created by the parser/type checker after reporting a syntax or type error; they must be filtered out before code generation.

Solutions

  1. Fix the pattern errors the compiler reported earlier in the file.
  2. Update gleam to the latest version (error-recovery bug may be fixed).
  3. File a minimal reproducer at github.com/gleam-lang/gleam if the panic occurs without a prior diagnostic.

Example fix

// before
let assert Ok(x) = result
  | _ -> .. // malformed clause
// after
let assert Ok(x) = result
Defensive patterns

Strategy: validation

Validate before calling

// Validate patterns parse/type-check before building
gleam check || exit 1

Type guard

match pattern { Pattern::Invalid { .. } => Err("invalid pattern"), _ => Ok(()) }

Try / catch

let status = Command::new("gleam").args(["build"]).status()?;
if !status.success() { /* read compiler diagnostics */ }

Prevention

When it happens

Trigger: Generating code for a function/clause whose patterns include an invalid pattern (one that already produced an error diagnostic) instead of compilation aborting earlier.

Common situations: Compiling a module that contains a malformed pattern the compiler already flagged; the panic typically appears together with the real error message, or alone when error recovery is buggy.

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@15b07c7830 (2026-09-14). Data as JSON: /api/errors/076f851da0747609. Report an issue: GitHub.

Appendix: source

Thrown at compiler-core/src/erlang/pattern.rs:290

            Pattern::BitArray {
                segments, location, ..
            } => {
                let bit_array = builder.start_bit_array_pattern(*location);
                for segment in segments {
                    builder.bit_array_segment(segment.location);
                    self.bit_array_pattern_segment_value(builder, segment);
                    self.bit_array_pattern_segment_size(builder, segment);
                    self.generator
                        .bit_array_segment_specifiers(builder, segment);
                }
                builder.end_bit_array_pattern(bit_array);
            }

            Pattern::BitArraySize(size) => self.bit_array_size(builder, size),

            Pattern::Invalid { .. } => {
                panic!("invalid patterns should not reach code generation")
            }
        }
    }

    fn bit_array_size<Output>(
        &mut self,
        builder: &mut impl ErlangBuilder<Output>,
        size: &'a TypedBitArraySize,
    ) {
        match size {
            BitArraySize::Int {
                int_value,
                location,
                ..
            } => builder.int_expression(*location, int_value.clone()),
            BitArraySize::Block { inner, .. } => self.bit_array_size(builder, inner),

            BitArraySize::Variable {

View on GitHub (pinned to 15b07c7830)