{"record":{"id":"8bc2625880a1d032","repo":"BoundaryML/baml","slug":"bitwise-requires-integer-operands-interpret","errorCode":null,"errorMessage":"bitwise &= requires integer operands","messagePattern":"bitwise &= requires integer operands","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":981,"sourceCode":"                                }\n                                _ => bail!(\"unsupported types for %= operator\"),\n                            },\n                            AssignOp::BitXorAssign => {\n                                match (current_val.clone(), rhs_val.clone()) {\n                                    (\n                                        BamlValueWithMeta::Int(a, meta),\n                                        BamlValueWithMeta::Int(b, _),\n                                    ) => BamlValueWithMeta::Int(a ^ b, meta),\n                                    _ => bail!(\"bitwise ^= requires integer operands\"),\n                                }\n                            }\n                            AssignOp::BitAndAssign => {\n                                match (current_val.clone(), rhs_val.clone()) {\n                                    (\n                                        BamlValueWithMeta::Int(a, meta),\n                                        BamlValueWithMeta::Int(b, _),\n                                    ) => BamlValueWithMeta::Int(a & b, meta),\n                                    _ => bail!(\"bitwise &= requires integer operands\"),\n                                }\n                            }\n                            AssignOp::BitOrAssign => match (current_val.clone(), rhs_val.clone()) {\n                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {\n                                    BamlValueWithMeta::Int(a | b, meta)\n                                }\n                                _ => bail!(\"bitwise |= requires integer operands\"),\n                            },\n                            AssignOp::ShlAssign => match (current_val.clone(), rhs_val.clone()) {\n                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {\n                                    if b < 0 {\n                                        bail!(\"negative shift amount in <<= operator\");\n                                    }\n                                    BamlValueWithMeta::Int(a << b, meta)\n                                }\n                                _ => bail!(\"shift <<= requires integer operands\"),\n                            },\n                            AssignOp::ShrAssign => match (current_val.clone(), rhs_val.clone()) {","sourceCodeStart":963,"sourceCodeEnd":999,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L963-L999","documentation":"Raised by the BAML interpreter when evaluating `x &= y`. Bitwise and-assign is only implemented for Int/Int operands; any other combination of BamlValue variants falls through to this bail. It enforces BAML's integer-only bitwise operator semantics.","triggerScenarios":"Executing `flags &= mask` where `flags` or `mask` is not an Int at runtime — e.g. a float bitfield, a string, or a bool used as a bit.","commonSituations":"Bitmask-style code where the mask was computed as a float or read from config/LLM output as a non-int type; also bool-as-bit habits from other languages.","solutions":["Make both operands Int before `&=` (explicitly convert bools/floats).","For bool conjunction use `&&` / `flag = flag && other` instead of `&=`.","Check where the mask value originates and fix its declared type to Int."],"exampleFix":"// before (BAML)\nlet mask = 0.75;\nflags &= mask; // error: bitwise &= requires integer operands\n\n// after\nlet mask = 0b0110;\nflags &= mask; // Int operands","handlingStrategy":"type-guard","validationCode":"// validate mask/flags are ints before &= executes\nfunction assertAndOperands(flags: unknown, mask: unknown): void {\n  if (!Number.isInteger(flags) || !Number.isInteger(mask)) {\n    throw new Error(\"bitwise &= requires integer operands in BAML\");\n  }\n}","typeGuard":"const isInt = (v: unknown): v is number => typeof v === \"number\" && Number.isInteger(v);","tryCatchPattern":"try {\n  await runBaml(program);\n} catch (e) {\n  if (String(e).includes(\"bitwise &= requires integer operands\")) {\n    // coerce mask to Int or switch to logical && and retry\n  } else { throw e; }\n}","preventionTips":["Keep bitmasks in dedicated Int variables with Int defaults (0).","Validate config/LLM-sourced mask values are integers before use.","Use `&&` for boolean conjunction instead of `&=`.","Grep BAML programs for `&=` during code review."],"tags":["baml","interpreter","bitwise","type-mismatch"],"backgroundTag":"unsupported-operation","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"}