{"record":{"id":"f8fb45b59d50b5bd","repo":"gleam-lang/gleam","slug":"wrong-number-of-subject-variables-for-pattern","errorCode":null,"errorMessage":"wrong number of subject variables for pattern","messagePattern":"wrong number of subject variables for pattern","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/exhaustiveness.rs","lineNumber":3583,"sourceCode":"            let branch = Branch::new(self.number_of_clauses, alternative_index, checks, guard);\n            self.branches.push(branch);\n        }\n\n        self.number_of_clauses += 1;\n    }\n\n    /// Add a single pattern as a branch to be compiled.\n    ///\n    /// This is useful in case one wants to check exhaustiveness of a single\n    /// pattern without having a fully fledged `TypedClause` to pass to the `add_clause`\n    /// method. For example, in `let` destructurings.\n    ///\n    pub fn add_pattern(&mut self, pattern: &TypedPattern) {\n        let pattern = self.register(pattern);\n        let var = self\n            .subject_variables\n            .first()\n            .expect(\"wrong number of subject variables for pattern\");\n        let branch = Branch::new(self.number_of_clauses, 0, vec![var.is(pattern)], None);\n        self.number_of_clauses += 1;\n        self.branches.push(branch);\n    }\n\n    pub fn compile(self, env: &Environment<'_>) -> CompileCaseResult {\n        let mut compiler = Compiler::new(env, self.variable_id, self.patterns);\n\n        let decision = if self.branches.is_empty() {\n            let var = self\n                .subject_variables\n                .first()\n                .expect(\"case with no subjects\")\n                .clone();\n\n            compiler.split_and_compile_with_pivot_var(var, VecDeque::new())\n        } else {\n            compiler.compile(self.branches.into())","sourceCodeStart":3565,"sourceCodeEnd":3601,"githubUrl":"https://github.com/gleam-lang/gleam/blob/15b07c783065c92bb016b1b74ed0995a5259850f/compiler-core/src/exhaustiveness.rs#L3565-L3601","documentation":"`add_pattern` compiles a single top-level pattern and expects at least one registered subject variable, taking `subject_variables.first()`. If none exist the `.expect` panics with this message. It means `add_pattern` was called for a case expression that had no subjects, violating the compiler's invariant that every pattern has a subject.","triggerScenarios":"Calling `add_pattern` on the exhaustiveness `Compiler` before `Compiler::new` was given any subjects, or with an empty subject list (e.g. desugaring a synthetic case with zero subjects).","commonSituations":"Seen during compiler development when constructing the exhaustiveness compiler programmatically — e.g. in tests or in a lowering pass that builds a case expression from code and passes an empty subject vector.","solutions":["Ensure `Compiler::new(env, variable_id, patterns)` receives a non-empty subject/pattern list before `add_pattern` is called","Guard the caller: only call `add_pattern` when there is at least one subject for the case expression","For subject-less cases, compile with an empty branch list path (`compile` handles the branches.is_empty() case differently)","If triggered by real Gleam code, report a compiler bug with the minimal case expression"],"exampleFix":"// before\nlet var = self.subject_variables.first().expect(\"wrong number of subject variables for pattern\");\n// after\nlet var = self.subject_variables.first().copied().unwrap_or_else(|| panic!(\"add_pattern called with no subject variables\"));","handlingStrategy":"validation","validationCode":"if subjects.is_empty() {\n    return Err(Error::CaseWithNoSubjects);\n}","typeGuard":"fn has_subjects(compiler: &Compiler) -> bool { !compiler.subject_variables.is_empty() }","tryCatchPattern":null,"preventionTips":["Never call add_pattern on a case without at least one subject","Assert subject count > 0 right after Compiler::new in debug builds","Cover subject-less synthetic cases in tests"],"tags":["rust","compiler","pattern-matching","panic"],"backgroundTag":"internal-invariant-violation","analyzedSha":"15b07c783065c92bb016b1b74ed0995a5259850f","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}