{"record":{"id":"1f60a2c4e5e52c61","repo":"BoundaryML/baml","slug":"bitwise-requires-integer-operands","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":972,"sourceCode":"                                }\n                                _ => bail!(\"unsupported types for /= operator\"),\n                            },\n                            AssignOp::ModAssign => match (current_val.clone(), rhs_val.clone()) {\n                                (BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {\n                                    if b == 0 {\n                                        bail!(\"modulo by zero in %= operator\");\n                                    }\n                                    BamlValueWithMeta::Int(a % b, meta)\n                                }\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()) {","sourceCodeStart":954,"sourceCodeEnd":990,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L954-L990","documentation":"Raised by the BAML interpreter when evaluating `x ^= y`. Bitwise xor-assign is defined only for Int/Int operand pairs; when either side is another BAML value type the match falls through to this bail. It exists to keep BAML's bitwise operators strictly integer-only.","triggerScenarios":"Running `a ^= b` where `a` or `b` is a Float, String, Bool, or other non-Int BamlValue at runtime — e.g. `flag ^= true` treating bools as bits.","commonSituations":"Developers coming from C/JS expect bools or floats to coerce to ints for bitwise ops; in BAML they do not, so xor-assign on a bool flag or a float counter fails here.","solutions":["Ensure both operands are Int; convert bools with an explicit conditional (`if b { 1 } else { 0 }`) before `^=`.","Replace `^=` on a boolean toggle with `flag = !flag`.","Coerce numeric strings/floats to Int explicitly before the assignment."],"exampleFix":"// before (BAML)\nlet flag = true;\nflag ^= other; // error: bitwise ^= requires integer operands\n\n// after\nlet flag = 1;\nflag ^= otherAsInt; // both Int","handlingStrategy":"type-guard","validationCode":"// before xor-assign, verify both operands are integers\nfunction assertXorOperands(a: unknown, b: unknown): void {\n  if (!Number.isInteger(a) || !Number.isInteger(b)) {\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    // replace ^= with bool negation or convert operands to Int and retry\n  } else { throw e; }\n}","preventionTips":["Use Bool and `!`/`&&`/`||` for flag logic; reserve bitwise ops for Int bitfields.","Convert bools to 0/1 explicitly if bit tricks are truly needed.","Audit BAML sources for `^=` and confirm both operand types statically.","Test bitwise code with non-int inputs to catch regressions."],"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"}