{"record":{"id":"ac9e140541fb3492","repo":"gleam-lang/gleam","slug":"empty-bit-array-test","errorCode":null,"errorMessage":"empty bit array test","messagePattern":"empty bit array test","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/exhaustiveness.rs","lineNumber":3213,"sourceCode":"    /// One path is going to be for the branches that can match if the test is\n    /// successful, and the other one is going to be the usual fallback to go\n    /// down to if it's not successful.\n    ///\n    /// > \"And now for the tricky bit...\"\n    /// > <https://youtu.be/lKXe3HUG2l4?si=i1thYB-kjfMU8NSe&t=645>\n    ///\n    fn add_checked_bit_array_branch(\n        &mut self,\n        pattern_check: PatternCheck,\n        mut tests: VecDeque<BitArrayTest>,\n        mut branch: Branch,\n        compiler: &mut Compiler<'_>,\n    ) {\n        // If we haven't found a test yet we just use the first one we find\n        // as the pivot to split all the branches.\n        let pivot_test = match self.choices.as_slice() {\n            [] => {\n                let test = tests.front().expect(\"empty bit array test\").clone();\n                self.choices.push((\n                    RuntimeCheck::BitArray { test: test.clone() },\n                    VecDeque::new(),\n                ));\n                test\n            }\n            [(RuntimeCheck::BitArray { test }, _)] => test.clone(),\n            _ => unreachable!(\"non bit array check when splitting bit array patterns\"),\n        };\n\n        let pattern_fails_if_test_succeeds = tests\n            .iter()\n            .any(|test| test.fails_if_succeeding(&pivot_test).is_certain());\n\n        let pattern_fails_if_test_fails = tests\n            .iter()\n            .any(|test| test.fails_if_failing(&pivot_test).is_certain());\n","sourceCodeStart":3195,"sourceCodeEnd":3231,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-core/src/exhaustiveness.rs#L3195-L3231","documentation":"Panic when starting a bit-array decision node with no existing choices: the pivot test is taken from the front of the pending `tests` deque, and this `expect` fires when that deque is empty. The invariant is that any branch reaching bit-array compilation has at least one pending bit-array segment test.","triggerScenarios":"Compiling a case subject with bit-array patterns where all pending tests were already consumed (e.g. empty segment list, or every branch's first segment produced no test), so `tests.front()` is None at the pivot-selection point.","commonSituations":"Gleam compiler contributors hit this with bit-array patterns containing only size/width segments without value tests, or after changes to how bit-array segment tests are generated (`is_bit_array_test`/segment lowering).","solutions":["Ensure at least one value test is generated per bit-array pattern; if a pattern has no tests, treat it as a catch-all instead of entering the pivot path.","Fix segment lowering so trivially-true segments do not push empty test lists while still entering this compiler path.","Short-circuit: if `tests` is empty, add the branch as unconditional rather than selecting a pivot."],"exampleFix":"// before\nlet test = tests.front().expect(\"empty bit array test\").clone();\n// after\nlet Some(test) = tests.front() else {\n    panic!(\"bit array pattern produced no tests; cannot choose pivot\")\n};\nlet test = test.clone();","handlingStrategy":"validation","validationCode":"if tests.is_empty() {\n    // no pivot possible: treat pattern as unconditional/catch-all and skip pivot selection\n}","typeGuard":"fn has_pivot_test(tests: &VecDeque<BitArrayTest>) -> bool { !tests.is_empty() }","tryCatchPattern":"std::panic::catch_unwind(|| compile_bit_array_node(node)).map_err(|_| Bug::EmptyBitArrayTest)","preventionTips":["Guarantee at least one value test per bit-array pattern segment lowering.","Route test-less bit-array patterns through the catch-all path, not the pivot path.","Unit-test bit arrays with only size/flags segments."],"tags":["rust","compiler","panics","bit-array","exhaustiveness"],"backgroundTag":"empty-required-field","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"}