{"record":{"id":"74197da9c8dc0d93","repo":"BoundaryML/baml","slug":"negative-shift-amount-at","errorCode":null,"errorMessage":"negative shift amount at {:?}","messagePattern":"negative shift amount at (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2513,"sourceCode":"            }\n            _ => bail!(\"bitwise & requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::BitOr => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                BamlValueWithMeta::Int(a | b, meta.clone())\n            }\n            _ => bail!(\"bitwise | requires integer operands at {:?}\", meta.0),\n        },\n        BinaryOperator::BitXor => match (left_val.clone(), right_val.clone()) {\n            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {\n                BamlValueWithMeta::Int(a ^ b, meta.clone())\n            }\n            _ => 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            }","sourceCodeStart":2495,"sourceCodeEnd":2531,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2495-L2531","documentation":"BAML's left-shift operator rejects a negative shift amount. Even when both operands are integers, evaluate_binary_op checks `b < 0` before shifting and bails with this message, since a negative shift is undefined/unsafe.","triggerScenarios":"Evaluating `a << b` where `b` is a negative integer, e.g. `1 << -2`, or a shift amount computed from a subtraction that went negative (`1 << (i - 5)` when i < 5).","commonSituations":"Loop-computed shift indices that underflow below zero; signed offsets from config or LLM output that were assumed non-negative.","solutions":["Clamp the shift amount before shifting: use a max(0, n) expression or an if-guard.","Validate the shift operand is >= 0 before evaluating the expression.","If right-shifting was intended (reversing direction), use `>>` instead."],"exampleFix":"// before (BAML)\nlet scaled = 1 << (index - offset);\n// after\nlet n = if (index - offset < 0) { 0 } else { index - offset };\nlet scaled = 1 << n;","handlingStrategy":"validation","validationCode":"// guard shift amount before <<\nif (shift < 0) throw new Error('negative shift amount');","typeGuard":"const validShift = (n) => Number.isInteger(n) && n >= 0;","tryCatchPattern":"try {\n  result = evaluate(expr);\n} catch (e) {\n  if (String(e).includes('negative shift amount')) {\n    result = evaluate(clampShiftAmounts(expr));\n  } else throw e;\n}","preventionTips":["Clamp computed shift indices with a max(0, n) guard","Add range assertions where shift amounts are derived from input data","Prefer explicit if/else over arithmetic that can go negative"],"tags":["baml","interpreter","shift-operator","value-out-of-range"],"backgroundTag":"value-out-of-range","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"}