{"record":{"id":"f1e909fa108b5efd","repo":"gleam-lang/gleam","slug":"unexpected-segment-value-pattern-pattern","errorCode":null,"errorMessage":"unexpected segment value pattern {pattern:?}","messagePattern":"unexpected segment value pattern (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/exhaustiveness.rs","lineNumber":4007,"sourceCode":"        }\n        ast::Pattern::String { value, .. } => BitArrayMatchedValue::LiteralString {\n            value: value.clone(),\n            encoding: StringEncoding::Utf8,\n            bytes: convert_string_escape_chars(value).as_bytes().into(),\n        },\n        ast::Pattern::Variable { name, .. } => BitArrayMatchedValue::Variable(name.clone()),\n        ast::Pattern::Discard { name, .. } => BitArrayMatchedValue::Discard(name.clone()),\n        ast::Pattern::Assign { name, pattern, .. } => BitArrayMatchedValue::Assign {\n            name: name.clone(),\n            value: Box::new(segment_matched_value(segment, Some(pattern), read_action)),\n        },\n        ast::Pattern::BitArraySize(_)\n        | ast::Pattern::List { .. }\n        | ast::Pattern::Constructor { .. }\n        | ast::Pattern::Tuple { .. }\n        | ast::Pattern::BitArray { .. }\n        | ast::Pattern::StringPrefix { .. }\n        | ast::Pattern::Invalid { .. } => panic!(\"unexpected segment value pattern {pattern:?}\"),\n    }\n}\n\nfn int_to_bits(\n    value: &BigInt,\n    read_size: &ReadSize,\n    endianness: Endianness,\n    signed: bool,\n) -> Result<BitVec<u8, Msb0>, IntToBitsError> {\n    let size = read_size\n        .constant_bits()\n        .ok_or(IntToBitsError::NonConstantSize)?\n        .to_u32()\n        .ok_or(IntToBitsError::ExceedsMaximumSize)?;\n\n    if !representable_with_bits(value, size, signed) {\n        return Err(IntToBitsError::Unrepresentable { size });\n    } else if size > BitArrayMatchedValue::MAX_BITS_INTERFERENCE {","sourceCodeStart":3989,"sourceCodeEnd":4025,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-core/src/exhaustiveness.rs#L3989-L4025","documentation":"Panic raised when computing the matched value of a bit array segment (compiler-core/src/exhaustiveness.rs:3903). Bit array sizes/segment values may only be literal-like patterns (ints, strings, variables, wildcards, bit array sizes); whole composite patterns such as `Constructor`, `List`, `Tuple`, `BitArray`, `StringPrefix`, `BitArraySize`-mismatch, or `Invalid` reaching this function violate the compiler's invariants, so it panics with the pattern dump.","triggerScenarios":"A bit array segment size or value position contains a pattern other than the allowed literal/variable forms after earlier phases — e.g. a constructor or list pattern in a size slot — usually because type checking failed to reject the program or a compiler change altered pattern classification.","commonSituations":"Compiler developers working on bit array size expressions; users of patched compilers or pre-release builds compiling patterns like `<<x:size(Some(n))>>`; stable-release users should never see this.","solutions":["Rewrite the pattern so segment sizes/values are plain literals, variables, or bit array size expressions (`size(n)`, `size(2 * n)`).","Ensure the program type-checks cleanly — an `Invalid` pattern reaching codegen means an earlier error was suppressed; fix the reported type errors first.","Upgrade the compiler; if reproducible on a supported release, file a bug with the `{pattern:?}` dump and minimal repro.","Compiler contributors: extend the match arms or reject the pattern in validation before this function runs."],"exampleFix":"// before: composite pattern in size slot\nlet <<x:size(maybe_size)>> = bits  // maybe_size: Result(Int, _)\n// after: resolve the size first\nassert Ok(n) = maybe_size\nlet <<x:size(n)>> = bits","handlingStrategy":"type-guard","validationCode":"// ensure segment values are literal/variable patterns before value computation\nif !matches!(pattern, ast::Pattern::Int(_) | ast::Pattern::String(_) | ast::Pattern::Variable(_) | ast::Pattern::Discard(_) | ast::Pattern::BitArraySize(_) | ast::Pattern::Float(_)) {\n    return Err(Error::UnsupportedSegmentPattern);\n}","typeGuard":"fn is_segment_value_pattern(p: &ast::Pattern) -> bool {\n    matches!(p, ast::Pattern::Int(_) | ast::Pattern::Float(_) | ast::Pattern::String(_) | ast::Pattern::Variable(_) | ast::Pattern::Discard(_) | ast::Pattern::BitArraySize(_))\n}","tryCatchPattern":null,"preventionTips":["Normalize segment values in an earlier pass so only allowed pattern kinds reach value computation","Return a diagnostic instead of panicking","Test composite patterns in segment value positions"],"tags":["panic","internal-invariant","bit-array","pattern-matching","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"}