{"record":{"id":"8881ec5832fa9ac1","repo":"gleam-lang/gleam","slug":"case-with-no-subjects","errorCode":null,"errorMessage":"case with no subjects","messagePattern":"case with no subjects","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/exhaustiveness.rs","lineNumber":3700,"sourceCode":"    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)], false);\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())\n        };\n\n        CompileCaseResult {\n            diagnostics: compiler.diagnostics,\n            compiled_case: CompiledCase {\n                tree: decision,\n                subject_variables: self.subject_variables,\n            },\n        }\n    }\n\n    /// Registers a typed pattern (and all its sub-patterns) into this\n    /// `CaseToCompile`'s pattern arena, returning an id to get the pattern back.","sourceCodeStart":3682,"sourceCodeEnd":3718,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-core/src/exhaustiveness.rs#L3682-L3718","documentation":"When a `case` expression being compiled has no branches, the compiler still needs at least one subject variable to split the decision tree on; `subject_variables.first().expect(\"case with no subjects\")` panics if the expression has zero subjects. This signals a malformed (subject-less) case expression reached the exhaustiveness compiler.","triggerScenarios":"Compiling `CompileCaseResult`/case compilation where `branches` is empty AND the original case expression had no subjects (e.g. `case` with no arguments produced by a buggy earlier pass).","commonSituations":"Encountered when working on Gleam compiler internals: a desugaring or codegen pass constructing a case expression without subjects, or fuzz input where subject validation was skipped.","solutions":["Ensure every case expression reaching compilation has at least one subject; fix the producer of the malformed AST node","If a subject-less case is legitimate, handle it before calling `split_and_compile_with_pivot_var` (e.g. return a catch-all decision)","Trace with a backtrace which pass created the empty-subject case and add validation there","End users: file a compiler bug with the triggering source code"],"exampleFix":"// before\nlet var = self.subject_variables.first().expect(\"case with no subjects\").clone();\n// after\nlet var = self.subject_variables.first().cloned().unwrap_or_else(|| panic!(\"compiling case with zero subjects; frontend should have rejected this\"));","handlingStrategy":"validation","validationCode":"if branches.is_empty() && subject_variables.is_empty() {\n    return Err(Error::CaseExpressionMissingSubjects);\n}","typeGuard":"fn compilable(case: &CaseToCompile) -> bool { !case.subject_variables.is_empty() }","tryCatchPattern":null,"preventionTips":["Reject subject-less case expressions in the typechecker","Validate the AST before lowering to exhaustiveness structures","Add an invariant test that every CompileCaseResult has >= 1 subject"],"tags":["rust","compiler","pattern-matching","panic"],"backgroundTag":"internal-invariant-violation","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"}