{"record":{"id":"13e0116d420dafb5","repo":"gleam-lang/gleam","slug":"bit-array-compilation-with-no-choice","errorCode":null,"errorMessage":"bit array compilation with no choice","messagePattern":"bit array compilation with no choice","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/exhaustiveness.rs","lineNumber":3281,"sourceCode":"                        BitArrayMatchedValue::LiteralFloat(_)\n                        | BitArrayMatchedValue::LiteralInt { .. }\n                        | BitArrayMatchedValue::LiteralString { .. }\n                        | BitArrayMatchedValue::Discard(_) => {}\n                    }\n                }\n\n                false\n            });\n\n            let variable = &pattern_check.var;\n            branch.add_check(variable.is(compiler.bit_array_pattern(tests)));\n\n            // We know that there's always going to be a single choice for the\n            // successful check, so we get that and add the branch to it.\n            let (_, if_true_branches) = self\n                .choices\n                .get_mut(0)\n                .expect(\"bit array compilation with no choice\");\n            if_true_branches.push_back(branch);\n        }\n\n        // Same goes for the if_false branch: knowing the pivot test has failed\n        // we only want to keep those branches with a pattern that still have a\n        // chance to match.\n        if !pattern_fails_if_test_fails {\n            // The main difference with the if true case is that we have no way\n            // of pruning the number of needed tests, so we add this check back\n            // exactly as it is.\n            branch.add_check(pattern_check);\n            self.fallback.push_back(branch);\n        }\n    }\n\n    fn save_index_of_new_choice(&mut self, kind: RuntimeCheckKind) {\n        let index = self.choices.len();\n        match kind {","sourceCodeStart":3263,"sourceCodeEnd":3299,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-core/src/exhaustiveness.rs#L3263-L3299","documentation":"Panic after compiling a bit-array pivot test: the code assumes `self.choices` has exactly the one choice pushed for the successful test, so `self.choices.get_mut(0)` must succeed. If choices is empty here, the pivot-test push above did not happen or choices were cleared.","triggerScenarios":"Compiling the if-true side of a bit-array decision node when `self.choices` is empty — e.g. the pivot test push was skipped, or a previous branch of the compiler removed choices before this point.","commonSituations":"Hit by Gleam compiler contributors while refactoring the bit-array path of the exhaustiveness compiler, or when early-return paths (like the `add_checked_bit_array_branch` fast path) leave `self.choices` empty.","solutions":["Verify every path that reaches this code pushed the `(RuntimeCheck::BitArray { .. }, ...)` choice first.","Check early returns above (e.g. already-compiled patterns) do not bypass the pivot push while later code still assumes choice 0 exists.","Use `let Some((_, branches)) = self.choices.get_mut(0)` with a descriptive panic to ease debugging."],"exampleFix":"// before\nlet (_, if_true_branches) = self.choices.get_mut(0).expect(\"bit array compilation with no choice\");\n// after\nlet Some((_, if_true_branches)) = self.choices.get_mut(0) else {\n    panic!(\"choices empty when adding bit array branch\")\n};","handlingStrategy":"validation","validationCode":"assert!(!self.choices.is_empty(), \"pivot test must be pushed before branch compilation\");","typeGuard":"fn first_choice(choices: &Choices) -> Option<&(RuntimeCheck, Branches)> { choices.first() }","tryCatchPattern":"std::panic::catch_unwind(|| add_bit_array_branch(branch)).map_err(|_| Bug::BitArrayNoChoice)","preventionTips":["Always push the (RuntimeCheck::BitArray, ...) choice before compiling branches.","Check early-return paths do not bypass the pivot push.","Assert choices.len() == 1 at bit-array node entry in debug builds."],"tags":["rust","compiler","panics","bit-array","exhaustiveness"],"backgroundTag":"index-out-of-bounds","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"}