{"record":{"id":"52c9311ad10064de","repo":"gleam-lang/gleam","slug":"invalid-bit-array-segment-type-reached-code-generation","errorCode":null,"errorMessage":"Invalid bit array segment type reached code generation: {:?}","messagePattern":"Invalid bit array segment type reached code generation: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/javascript/expression.rs","lineNumber":3718,"sourceCode":"            let encoding = if segment.has_utf16_option() {\n                StringEncoding::Utf16\n            } else if segment.has_utf32_option() {\n                StringEncoding::Utf32\n            } else {\n                StringEncoding::Utf8\n            };\n            BitArraySegmentType::String(encoding)\n        } else if segment.type_.is_utf_codepoint() {\n            let encoding = if segment.has_utf16_codepoint_option() {\n                StringEncoding::Utf16\n            } else if segment.has_utf32_codepoint_option() {\n                StringEncoding::Utf32\n            } else {\n                StringEncoding::Utf8\n            };\n            BitArraySegmentType::UtfCodepoint(encoding)\n        } else {\n            panic!(\n                \"Invalid bit array segment type reached code generation: {:?}\",\n                segment.type_\n            );\n        }\n    }\n}\n\npub fn string<'a, 'doc>(\n    arena: &'doc DocumentArena<'a, 'doc>,\n    value: &'a str,\n) -> Document<'a, 'doc> {\n    if value.contains('\\n') {\n        EcoString::from(value.replace('\\n', r\"\\n\"))\n            .to_doc(arena)\n            .surround(arena, DOUBLE_QUOTE_DOCUMENT, DOUBLE_QUOTE_DOCUMENT)\n    } else {\n        value\n            .to_doc(arena)","sourceCodeStart":3700,"sourceCodeEnd":3736,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-core/src/javascript/expression.rs#L3700-L3736","documentation":"When lowering a bit array segment to JavaScript, the compiler only recognizes a fixed set of segment types (Int, Float, Bytes/String with an encoding, UtfCodepoint, etc.). If `segment.type_` holds any other variant, this panic fires, indicating the type checker allowed a segment type the JavaScript backend cannot represent.","triggerScenarios":"A typed bit array segment whose `type_` is not one of the supported variants reaches `bit_array_segment` handling in expression.rs — e.g. a new segment type added to the core AST but not implemented in the JS backend, or internals misuse constructing segments directly.","commonSituations":"Compiler development where a new bit array option was added to the type checker but not the JavaScript code generator; passing hand-built ASTs into the generator from tooling.","solutions":["Check which segment type the debug output (`{:?}`) names and avoid using that option in bit arrays targeting JavaScript.","Restrict the bit array pattern/segment to supported types (int, float, bits, utf8/utf16/utf32, utf_codepoint).","If the type is a legitimately supported one that still panics, report a Gleam compiler bug including the debug value."],"exampleFix":"// before\nlet x = <<1.5:size(16)>> // unsupported option combination\n// after\nlet x = <<1.5:float>>","handlingStrategy":"validation","validationCode":"// only use segment types the JS backend supports\nlet ok = matches!(seg.type_,\n    BitArraySegmentType::Int | BitArraySegmentType::Float\n    | BitArraySegmentType::Bytes(_) | BitArraySegmentType::UtfCodepoint(_));\nif !ok { return Err(unsupported_segment_error(seg.location)); }","typeGuard":"fn js_supported_segment(t: &BitArraySegmentType) -> bool {\n    matches!(t, BitArraySegmentType::Int\n        | BitArraySegmentType::Float\n        | BitArraySegmentType::Bytes(_)\n        | BitArraySegmentType::UtfCodepoint(_))\n}","tryCatchPattern":"match std::panic::catch_unwind(|| segment_doc(seg)) {\n    Ok(doc) => doc,\n    Err(_) => emit_unsupported_segment_diagnostic(&seg.type_),\n}","preventionTips":["Restrict bit array segments to documented JS-supported types and options.","After upgrading Gleam, re-check bit array code against release notes.","Include the `{:?}` debug value from the panic message in any bug report."],"tags":["compiler","panic","bit-array","code-generation"],"backgroundTag":"unsupported-operation","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"}