{"record":{"id":"2830a37e82075aad","repo":"BoundaryML/baml","slug":"undefined-variant-enum-name-str-variant-str","errorCode":null,"errorMessage":"undefined variant: {enum_name_str}.{variant_str}","messagePattern":"undefined variant: (.+?)\\.(.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_compiler2_emit/src/emit.rs","lineNumber":2220,"sourceCode":"                if enum_obj_idx.is_some() {\n                    self.references.record(&enum_name_str);\n                }\n                let Some(enum_obj_idx) = enum_obj_idx else {\n                    let idx = self.add_constant(ConstValue::Null);\n                    let inst = self.emit(Instruction::LoadConst(idx));\n                    self.set_operand(\n                        inst,\n                        OperandMeta::Const(format!(\"undefined_enum::{enum_name_str}.{variant}\")),\n                    );\n                    return;\n                };\n\n                let variant_str = variant.to_string();\n                let variant_idx = *self\n                    .enum_variants\n                    .get(&enum_name_str)\n                    .and_then(|variants| variants.get(&variant_str))\n                    .unwrap_or_else(|| panic!(\"undefined variant: {enum_name_str}.{variant_str}\"));\n\n                #[allow(clippy::cast_possible_wrap)]\n                let idx = self.add_constant(ConstValue::Int(variant_idx as i64));\n                let lc_inst = self.emit(Instruction::LoadConst(idx));\n                self.set_operand(\n                    lc_inst,\n                    OperandMeta::Const(format!(\"{enum_name_str}.{variant_str}\")),\n                );\n                let inst = self.emit(Instruction::AllocVariant(ObjectIndex::from_raw(\n                    enum_obj_idx,\n                )));\n                self.set_operand(inst, OperandMeta::Object(enum_name_str));\n            }\n        }\n    }\n\n    // ========================================================================\n    // Store Emission","sourceCodeStart":2202,"sourceCodeEnd":2238,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_compiler2_emit/src/emit.rs#L2202-L2238","documentation":"When emitting an enum variant constant (`Constant::EnumVariant`), the compiler looks up the variant's index in `enum_variants` keyed by the enum's rendered name and variant string. Unlike the enum itself (which degrades gracefully to a Null constant when undefined), a missing variant entry panics: the enum object exists but this variant name is not registered for it. This means the source referenced a variant that doesn't exist on the (known) enum, or the variant table is out of sync.","triggerScenarios":"Compiling code like `MyEnum.SomeVariant` where `MyEnum` is registered but `SomeVariant` is not among its declared variants (typo or removed variant), or where the `enum_variants` map for that enum was populated from a stale/different definition that lacks the variant (stale incremental state, cross-package enum with divergent definitions).","commonSituations":"Typos in variant names at the use site; removing/renaming an enum variant while other code (or a stale cache) still references the old name; two packages declaring the same enum name with different variant sets.","solutions":["Check the spelling of the variant at the use site against the enum declaration and fix the typo","If the variant was renamed/removed, update all references to the new name","Clear incremental caches and rebuild so `enum_variants` matches the current declarations","If the enum is declared in multiple packages, resolve the name collision or align the definitions","Consider matching the enum-constant path's graceful behavior (emit an error diagnostic instead of panicking) if you maintain the compiler"],"exampleFix":"// before (BAML)\nlet c = Color\\u{2e}Grean;\n// after\nlet c = Color.Green;","handlingStrategy":"validation","validationCode":"// pre-flight: validate enum variant references against declared variants\nfn validate_variant(enum_variants: &HashMap<String, HashMap<String, usize>>, enum_name: &str, variant: &str) -> Result<(), String> {\n    enum_variants\n        .get(enum_name)\n        .and_then(|v| v.get(variant))\n        .map(|_| ())\n        .ok_or_else(|| format!(\"undefined variant: {enum_name}.{variant}\"))\n}","typeGuard":"fn variant_exists(enum_variants: &HashMap<String, HashMap<String, usize>>, enum_name: &str, variant: &str) -> bool {\n    enum_variants.get(enum_name).is_some_and(|v| v.contains_key(variant))\n}","tryCatchPattern":"match std::panic::catch_unwind(|| compile(inputs)) {\n    Ok(out) => out,\n    Err(_) => {\n        eprintln!(\"compile panicked (likely stale variant table); rebuilding from scratch\");\n        clean_cache();\n        compile(inputs)\n    }\n}","preventionTips":["Double-check variant spellings against the enum declaration","Rebuild after removing or renaming enum variants","Avoid declaring two enums with the same name across packages","Use editor tooling/autocomplete for variant names to avoid typos"],"tags":["compiler","enum","undefined-variant","emit"],"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-14T05:17:10.506Z"}