{"record":{"id":"990b56f93b80f145","repo":"BoundaryML/baml","slug":"shift-requires-integer-operands-at-interpret","errorCode":null,"errorMessage":"shift >> requires integer operands at {:?}","messagePattern":"shift >> requires integer operands at (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2526,"sourceCode":"            _ => bail!(\"bitwise ^ requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::Shl => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                if b < 0 {\n                    bail!(\"negative shift amount at {:?}\", meta.0);\n                }\n                BamlValueWithMeta::Int(a << b, meta.clone())\n            }\n            _ => bail!(\"shift << requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::Shr => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                if b < 0 {\n                    bail!(\"negative shift amount at {:?}\", meta.0);\n                }\n                BamlValueWithMeta::Int(a >> b, meta.clone())\n            }\n            _ => bail!(\"shift >> requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::InstanceOf => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Class(class, ..), BamlValueWithMeta::Class(right_class, ..)) => {\n                BamlValueWithMeta::Bool(class == right_class, meta.clone())\n            }\n            _ => bail!(\"instanceof requires class operands at {:?}\", meta.0),\n        },\n    })\n}\n\nfn evaluate_unary_op(\n    operator: &crate::hir::UnaryOperator,\n    val: &BamlValueWithMeta<ExprMetadata>,\n    meta: &ExprMetadata,\n) -> Result<BamlValueWithMeta<ExprMetadata>> {\n    use crate::hir::UnaryOperator;\n    Ok(match operator {\n        UnaryOperator::Not => match val.clone() {","sourceCodeStart":2508,"sourceCodeEnd":2544,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2508-L2544","documentation":"This error is thrown by the BAML interpreter when the right-shift (>>) binary operator is applied to operands that are not both integers, or to a non-integer at all. The interpreter only implements >> for Int/Int pairs (with a non-negative shift amount); any other combination falls through to the catch-all bail. It exists because BAML's typechecker may not always catch operand types before evaluation (e.g. dynamic/any-typed values).","triggerScenarios":"Evaluating a BAML expression `a >> b` where either `a` or `b` is a Float, String, Bool, or other non-Int BamlValue at runtime. Note that a separate error covers negative shift amounts; this one fires only for wrong operand types.","commonSituations":"Dividing then shifting with float results (e.g. `x / 2 >> y`), shifting a value parsed from JSON that was inferred as Float, or typos where a string flag is passed instead of an int mask.","solutions":["Check both operands of >> at runtime; wrap non-int operands in an integer conversion before shifting","Verify the BAML types of the operands; annotate variables as int so the typechecker rejects floats/strings earlier","If the value comes from JSON input, ensure the target type is Int and the JSON number is integral (see 'Expected integer' error)","Replace >> with an equivalent integer-only expression (e.g. division by 2^n using int arithmetic) if operands are inherently fractional"],"exampleFix":"// before (BAML)\nlet scaled = amount >> 2;   // amount: float\n// after\nlet scaled = (amount as int) >> 2;  // or make amount an int upstream","handlingStrategy":"type-guard","validationCode":"// before shifting, verify integer operands\nif (!Number.isInteger(a) || !Number.isInteger(b) || b < 0) {\n  throw new Error(`shift operands must be non-negative ints, got a=${a} b=${b}`);\n}","typeGuard":"const isInt = (v: unknown): v is number => typeof v === 'number' && Number.isInteger(v);","tryCatchPattern":"try {\n  result = evaluate(expr);\n} catch (e) {\n  if (String(e).includes('shift >> requires integer operands')) {\n    // coerce operands to int or fix expression\n  }\n  throw e;\n}","preventionTips":["Annotate BAML variables as int so the typechecker rejects float/string operands","Avoid float arithmetic immediately before shifts; convert explicitly","Check for negative shift amounts separately"],"tags":["baml","interpreter","type-mismatch","bitwise"],"backgroundTag":"type-mismatch","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"}