{"record":{"id":"865273a57ff2c46e","repo":"gleam-lang/gleam","slug":"unknown-variant-index","errorCode":null,"errorMessage":"unknown variant index","messagePattern":"unknown variant index","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/exhaustiveness.rs","lineNumber":2697,"sourceCode":"        &mut self,\n        kind: RuntimeCheckKind,\n        branch_mode: &BranchMode,\n    ) -> RuntimeCheck {\n        match (kind, branch_mode) {\n            (RuntimeCheckKind::Int { int_value }, _) => RuntimeCheck::Int { int_value },\n            (RuntimeCheckKind::Float { float_value }, _) => RuntimeCheck::Float { float_value },\n            (RuntimeCheckKind::String { value }, _) => RuntimeCheck::String { value },\n            (RuntimeCheckKind::StringPrefix { prefix }, _) => RuntimeCheck::StringPrefix {\n                prefix,\n                rest: self.fresh_variable(string()),\n            },\n            (RuntimeCheckKind::Tuple { .. }, BranchMode::Tuple { elements }) => {\n                self.is_tuple_check(elements)\n            }\n            (RuntimeCheckKind::Variant { index }, BranchMode::NamedType { constructors, .. }) => {\n                self.is_variant_check(\n                    index,\n                    constructors.get(index).expect(\"unknown variant index\"),\n                )\n            }\n            (RuntimeCheckKind::EmptyList, _) => RuntimeCheck::EmptyList,\n            (RuntimeCheckKind::NonEmptyList, BranchMode::List { inner_type }) => {\n                self.is_list_check(inner_type.clone())\n            }\n            (_, _) => unreachable!(\"type checking should make this impossible\"),\n        }\n    }\n\n    /// Comes up with new pattern cecks that have to match in case a given\n    /// runtime check succeeds for the given pattern.\n    ///\n    /// Let's make an example: when we have a pattern - say `a is Wibble(1, [])` -\n    /// we come up with a runtime check to perform on it. For our example the\n    /// runtime check is to make sure that `a` is indeed a `Wibble` variant.\n    /// However, after successfully performing that check we're left with much to\n    /// do! We know that `a` is `Wibble` but now we'll have to make sure that its","sourceCodeStart":2679,"sourceCodeEnd":2715,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-core/src/exhaustiveness.rs#L2679-L2715","documentation":"Panic when matching a `RuntimeCheckKind::Variant { index }` against a `BranchMode::NamedType`: `constructors.get(index)` returned None. The variant index recorded in the check must be valid for the constructors of the named type at that node; if not, the check and the branch were built from different types.","triggerScenarios":"Compiling a variant check whose `index` exceeds the constructor count of the branch's NamedType — typically after a type substitution changed the subject's type, or a check was built for a different variant-bearing type and reused at the wrong node.","commonSituations":"Seen by Gleam compiler contributors after changes to type inference/unification feeding exhaustiveness, or when custom types are reordered between check construction and tree compilation.","solutions":["Rebuild the Variant check from the current branch's constructors instead of carrying a stale positional index.","Verify the subject type used to build the RuntimeCheckKind is the same NamedType stored in BranchMode::NamedType.","Return an unreachable/bug error instead of panicking, and reduce the failing module to a minimal repro to file upstream."],"exampleFix":"// before\nconstructors.get(index).expect(\"unknown variant index\")\n// after\nconstructors.get(index).unwrap_or_else(|| {\n    panic!(\"variant index {} invalid for {} constructors\", index, constructors.len())\n})","handlingStrategy":"validation","validationCode":"assert!(index < constructors.len(), \"variant index {} invalid for this branch\", index);","typeGuard":"fn constructor_for(constructors: &[Constructor], index: usize) -> Option<&Constructor> {\n    constructors.get(index)\n}","tryCatchPattern":"std::panic::catch_unwind(|| compile_check(check)).map_err(|_| Bug::UnknownVariantIndex(index))","preventionTips":["Build RuntimeCheckKind::Variant from the same type stored in BranchMode::NamedType.","Re-derive indices after type substitution instead of carrying them.","Keep a mapping from variant name to per-type index rather than raw positions."],"tags":["rust","compiler","panics","exhaustiveness","variants"],"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"}