{"record":{"id":"0c9ddc3e25ecc7f5","repo":"BoundaryML/baml","slug":"enum-not-found-for-variant","errorCode":null,"errorMessage":"Enum not found for variant: {:?}","messagePattern":"Enum not found for variant: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"engine/baml-vm/src/test.rs","lineNumber":119,"sourceCode":"                    anyhow::bail!(\"Class not found for instance: {:?}\", instance);\n                };\n\n                let mut fields = BamlMap::new();\n\n                for (i, value) in instance.fields.iter().enumerate() {\n                    let value = Value::from_vm_value(value, vm)?;\n                    fields.insert(vm_class.field_names[i].clone(), value);\n                }\n\n                Ok(Object::Instance(Instance {\n                    class: vm_class.name.clone(),\n                    fields,\n                }))\n            }\n\n            VmObject::Variant(variant) => {\n                let VmObject::Enum(vm_enum) = &vm.objects[variant.enm] else {\n                    anyhow::bail!(\"Enum not found for variant: {:?}\", variant);\n                };\n\n                Ok(Object::Variant(Variant {\n                    enm: vm_enum.name.clone(),\n                    variant: vm_enum.variant_names[variant.index].clone(),\n                }))\n            }\n\n            VmObject::Media(media) => Ok(Object::Media(media.clone())),\n\n            VmObject::Function(f) => Ok(Object::Function(f.name.clone())),\n\n            VmObject::Class(c) => Ok(Object::Class(c.name.clone())),\n\n            VmObject::Enum(e) => Ok(Object::Enum(e.name.clone())),\n\n            _ => anyhow::bail!(\"Unsupported object type for testing: {:?}\", obj),\n        }","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-vm/src/test.rs#L101-L137","documentation":"from_vm_object converts a VmObject::Variant into a test-facing Object. Before reading the variant's name it dereferences the variant's enum handle from the VM object heap, expecting an enum. If the handle does not point to a VmObject::Enum, the internal representation is corrupt or the wrong object index was stored, so this bail fires.","triggerScenarios":"Calling from_vm_object on a VmObject::Variant whose `enm` field indexes a non-Enum object in vm.objects (e.g. a class or instance), typically due to a compiler/VM bug writing the wrong index into Variant.enm.","commonSituations":"VM developers testing watch/notification serialization after changing the layout of VmObject variants or enum heap indexing; stale indices after GC or heap compaction changes.","solutions":["Fix the code that constructs VmObject::Variant to store the correct enum index in the `enm` field","Dump vm.objects[variant.enm] at the failure point to identify what type the index actually points to","Check for GC/heap-compaction code that may invalidate Variant.enm indices","Add a debug assertion when constructing variants that the handle resolves to VmObject::Enum"],"exampleFix":"// before\nlet VmObject::Enum(vm_enum) = &vm.objects[variant.enm] else {\n    anyhow::bail!(\"Enum not found for variant: {:?}\", variant);\n};\n// after\nlet Some(vm_enum) = vm.objects.get(variant.enm) else {\n    anyhow::bail!(\"Enum index {} out of bounds for variant: {:?}\", variant.enm, variant);\n};\nlet VmObject::Enum(vm_enum) = vm_enum else {\n    anyhow::bail!(\"Handle {:?} points to {:?}, expected enum\", variant.enm, vm_enum);\n};","handlingStrategy":"type-guard","validationCode":"fn is_enum_handle(vm: &Vm, idx: usize) -> bool {\n    matches!(vm.objects.get(idx), Some(VmObject::Enum(_)))\n}","typeGuard":"if let VmObject::Enum(vm_enum) = &vm.objects[variant.enm] { /* use vm_enum */ } else { /* skip or log */ }","tryCatchPattern":"let obj = Object::from_vm_object(vm, variant).map_err(|e| { log::warn!(\"variant conversion failed: {e}\"); e })?;","preventionTips":["Add assertions when constructing VmObject::Variant that the enm handle resolves to an enum","Run VM snapshot/serialization tests after changing heap layout","Avoid raw index handles; use typed handle wrappers"],"tags":["internal","vm","enum"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}