{"record":{"id":"e12cf8b641254fae","repo":"gleam-lang/gleam","slug":"invalid-pattern-size-made-it-to-code-generation-x","errorCode":null,"errorMessage":"invalid pattern size made it to code generation {x:?}","messagePattern":"invalid pattern size made it to code generation (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/exhaustiveness.rs","lineNumber":4111,"sourceCode":"}\n\nfn segment_size(\n    segment: &TypedPatternBitArraySegment,\n    pattern_variables: &HashMap<EcoString, ReadAction>,\n    // If we are compiling an assignment pattern, we still need access to the\n    // `type_` and `options` fields of the `segment`, so we must still pass that\n    // in above. However, we need to check the correct sub-pattern of the original\n    // pattern, so if they are different we set this argument to `Some`.\n    pattern: Option<&TypedPattern>,\n) -> ReadSize {\n    let pattern = pattern.unwrap_or(&segment.value);\n\n    match segment.size() {\n        // The size of a segment must be a `BitArraySize` pattern.\n        Some(ast::Pattern::BitArraySize(size)) => {\n            bit_array_size(segment.unit(), pattern_variables, size)\n        }\n        Some(x) => panic!(\"invalid pattern size made it to code generation {x:?}\"),\n\n        // If a segment has the `bits`/`bytes` option and has no size, that\n        // means it's the final catch all segment: we'll have to read any number\n        // of bits.\n        _ if segment.has_bits_option() => ReadSize::RemainingBits,\n        _ if segment.has_bytes_option() => ReadSize::RemainingBytes,\n\n        // If there's no size option we go for a default: 8 bits for int\n        // segments, and 64 for anything else.\n        None if segment.type_.is_int() => ReadSize::ConstantBits(8.into()),\n        None => match pattern {\n            ast::Pattern::Assign { pattern, .. } => {\n                segment_size(segment, pattern_variables, Some(pattern))\n            }\n\n            ast::Pattern::String { value, .. } if segment.has_utf16_option() => {\n                ReadSize::ConstantBits(\n                    // Each utf16 code unit is 16 bits","sourceCodeStart":4093,"sourceCodeEnd":4129,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-core/src/exhaustiveness.rs#L4093-L4129","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Write segment sizes as bit array size expressions: `<<x:size(n)>>` with `n` an int variable/expression, not a binding or composite pattern.","Make sure the module compiles without prior type errors; suppressed errors can let malformed patterns reach codegen.","Upgrade the compiler; report a bug with the `{x:?}` dump and minimal pattern if it persists.","Compiler contributors: normalize sizes into `Pattern::BitArraySize` before code generation."],"exampleFix":"// before: non-BitArraySize pattern in size slot\nlet <<x:size(<<8>>)>> = bits\n// after: use an integer size\nlet <<x:size(8)>> = bits","handlingStrategy":"type-guard","validationCode":"match segment.size() {\n    None | Some(ast::Pattern::BitArraySize(_)) => ok,\n    Some(_) => return Err(Error::InvalidSegmentSize),\n}","typeGuard":"fn has_valid_size(segment: &Segment) -> bool {\n    matches!(segment.size(), None | Some(ast::Pattern::BitArraySize(_)))\n}","tryCatchPattern":null,"preventionTips":["Desugar all segment sizes to Pattern::BitArraySize before codegen","Add a validation pass assert on segment sizes","Test bit array patterns with odd size expressions"],"tags":["panic","internal-invariant","bit-array","codegen","compiler"],"backgroundTag":"internal-invariant-violation","analyzedSha":"49f8762da595eb3c8f576656a4200a142ca188c9","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}