gleam-lang/gleam · error
invalid size in pattern size segment
Error message
invalid size in pattern size segment
What it means
An internal compiler assertion in bit-array pattern size-segment codegen: a segment's size node is not a TypedPattern::BitArraySize. Sizes of bit-array pattern segments must be represented as bit-array-size patterns after type checking; any other pattern shape here breaks the invariant.
Solutions
- Fix any errors the compiler reported earlier for the pattern.
- Update gleam to the latest version.
- File a minimal reproducing module at github.com/gleam-lang/gleam if no prior error appeared.
Defensive patterns
Strategy: validation
Validate before calling
// Confirm segment sizes are plain integer expressions before building gleam check || exit 1
Type guard
match size { TypedPattern::BitArraySize(s) => Ok(s), _ => Err("size must be a bit-array size expression") } Try / catch
let status = Command::new("gleam").args(["build"]).status()?;
if !status.success() { /* handle failure */ } Prevention
- Write segment sizes as integer literals or Int-typed size variables.
- Clear earlier pattern errors before rebuilding.
- Report panics without prior diagnostics to the gleam repo.
When it happens
Trigger: Generating code for a `<<..:size(n)>>` pattern segment whose size expression was not normalized to a BitArraySize node by earlier compiler phases.
Common situations: Compiling bit-array patterns after an earlier phase (parsing/type checking) produced a malformed or error-recovery AST, usually alongside other diagnostics; otherwise a compiler bug.
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
- invalid constants should not reach code generation
- invalid pattern inside aliased bit array pattern segment
- invalid pattern size made it to code generation
- invalid patterns should not reach code generation
- invalid segment
AI-assisted analysis of gleam-lang/gleam@15b07c7830 (2026-09-14).
Data as JSON: /api/errors/9fb36de77319afac.
Report an issue: GitHub.
Appendix: source
Thrown at compiler-core/src/erlang/pattern.rs:437
};
let _ = self
.variables_to_add_later
.insert(name.clone(), aliased_value);
self.pattern(builder, pattern);
}
fn bit_array_pattern_segment_size<Output>(
&mut self,
builder: &mut impl ErlangBuilder<Output>,
segment: &'a TypedPatternBitArraySegment,
) {
let Some(size) = segment.size() else {
builder.bit_array_segment_default_size();
return;
};
let TypedPattern::BitArraySize(size) = size else {
panic!("invalid size in pattern size segment")
};
self.bit_array_size(builder, size);
}
fn bit_array_size_divide<Output>(
&mut self,
location: &'a SrcSpan,
builder: &mut impl ErlangBuilder<Output>,
left: &'a TypedBitArraySize,
right: &'a TypedBitArraySize,
operator: &'static str,
) {
if right.non_zero_compile_time_number() {
builder.binary_operator(*location, operator);
self.bit_array_size(builder, left);
self.bit_array_size(builder, right);
} else {
let case = builder.start_case(*location);View on GitHub (pinned to 15b07c7830)