{"record":{"id":"876646683bc0349e","repo":"BoundaryML/baml","slug":"undefined-enum-variant-name-field","errorCode":null,"errorMessage":"undefined enum variant: {name}.{field}","messagePattern":"undefined enum variant: (.+?)\\.(.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/codegen.rs","lineNumber":1154,"sourceCode":"                self.compile_expression(base);\n                self.compile_expression(index);\n\n                // Determine if it's an array or map and emit appropriate instruction\n                self.emit(\n                    match base.meta().1.as_ref().expect(\"must have a resolved type\") {\n                        TypeIR::List(_, _) => Instruction::LoadArrayElement,\n                        TypeIR::Map(_, _, _) => Instruction::LoadMapElement,\n                        _ => panic!(\"array access should be either map or array.\"),\n                    },\n                );\n            }\n\n            thir::Expr::FieldAccess { base, field, .. } => {\n                // Direct enum access: Share.Rectangle\n                if let thir::Expr::Var(name, _) = base.as_ref() {\n                    if let Some(enm) = self.enums.get(name) {\n                        let Some(variant_index) = enm.get(field) else {\n                            panic!(\"undefined enum variant: {name}.{field}\");\n                        };\n\n                        let Some(enum_index) = self.globals.get(name) else {\n                            panic!(\"undefined enum: {name}\");\n                        };\n\n                        let const_index = self.add_constant(Value::Int(*variant_index as i64));\n                        self.emit(Instruction::LoadConst(const_index));\n\n                        let allocation_instruction =\n                            self.emit(Instruction::AllocVariant(ObjectIndex::from_raw(usize::MAX)));\n\n                        // TODO: Confusing name because of class alloc reuse.\n                        self.class_alloc_patch_list.push(AllocInstancePatch {\n                            location: allocation_instruction,\n                            global: *enum_index,\n                        });\n","sourceCodeStart":1136,"sourceCodeEnd":1172,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/codegen.rs#L1136-L1172","documentation":"Panic when compiling a field access like `EnumName.Variant` where the base resolves to a known enum but the field name is not a variant of that enum. Codegen needs a variant index to emit the enum constant; a missing variant means the name resolution/typecheck stages let an invalid enum variant through, so codegen aborts.","triggerScenarios":"Compiling `Expr::FieldAccess` where `base` is a `Var` found in `self.enums`, but `enm.get(field)` returns None — e.g. `Share.Rectengle` when the enum only defines `Rectangle`.","commonSituations":"Typos in enum variant names; variants removed or renamed in the enum definition while expression references were not updated; enum defined in another file with a divergent variant list.","solutions":["Check the enum definition and correct the variant name spelling","Update references after renaming/removing an enum variant","If the enum is defined in an imported file, verify you are referencing the correct enum version","If the variant exists, file a compiler bug: enum tables are inconsistent between phases"],"exampleFix":"// before\nlet s = Share.Rectengle\n// after\nlet s = Share.Rectangle","handlingStrategy":"validation","validationCode":"// Verify the variant exists on the enum before emitting EnumName.Variant\nfn validate_enum_access(enum_def: &Enum, variant: &str) -> Result<(), String> {\n    if enum_def.variants.iter().any(|v| v.name == variant) { Ok(()) }\n    else { Err(format!(\"variant `{}` not defined on enum `{}`\", variant, enum_def.name)) }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use editor completion for enum variants instead of typing them from memory","Regenerate/stale-check after renaming enum variants","Keep enum definitions centralized and grep usages on rename"],"tags":["compiler","codegen","panic","enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}