BoundaryML/baml · error
negative shift amount in <<= operator
Error message
negative shift amount in <<= operator
What it means
Raised by the BAML interpreter when evaluating `x <<= y` with a negative Int shift amount. Even with both operands Int, a shift by a negative count is undefined/unsupported, so the interpreter bails before performing `a << b`.
Source
Thrown at engine/baml-compiler/src/thir/interpret.rs:993
AssignOp::BitAndAssign => {
match (current_val.clone(), rhs_val.clone()) {
(
BamlValueWithMeta::Int(a, meta),
BamlValueWithMeta::Int(b, _),
) => BamlValueWithMeta::Int(a & b, meta),
_ => bail!("bitwise &= requires integer operands"),
}
}
AssignOp::BitOrAssign => match (current_val.clone(), rhs_val.clone()) {
(BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
BamlValueWithMeta::Int(a | b, meta)
}
_ => bail!("bitwise |= requires integer operands"),
},
AssignOp::ShlAssign => match (current_val.clone(), rhs_val.clone()) {
(BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
if b < 0 {
bail!("negative shift amount in <<= operator");
}
BamlValueWithMeta::Int(a << b, meta)
}
_ => bail!("shift <<= requires integer operands"),
},
AssignOp::ShrAssign => match (current_val.clone(), rhs_val.clone()) {
(BamlValueWithMeta::Int(a, meta), BamlValueWithMeta::Int(b, _)) => {
if b < 0 {
bail!("negative shift amount in >>= operator");
}
BamlValueWithMeta::Int(a >> b, meta)
}
_ => bail!("shift >>= requires integer operands"),
},
};
// Assign the result back to the target expression
assign_to_expr(View on GitHub (pinned to bd85ce9dee)
Solutions
- Clamp the shift amount before the assignment, e.g. `let s = if n < 0 { 0 } else { n };` then `x <<= s;`.
- If a negative shift means a right shift, use `>>=` for the negative case explicitly.
- Validate/normalize any external input feeding the shift amount.
Example fix
// before (BAML)
x <<= n - m; // error if n < m: negative shift amount in <<= operator
// after
let amt = if n > m { n - m } else { 0 };
x <<= amt; Defensive patterns
Strategy: validation
Validate before calling
// clamp shift amount before executing <<= const clampShift = (n: number): number => (Number.isInteger(n) && n >= 0 ? n : 0); if (!Number.isInteger(shift) || shift < 0) shift = 0;
Type guard
const validShiftAmount = (v: unknown): v is number => typeof v === "number" && Number.isInteger(v) && v >= 0;
Try / catch
try {
await runBaml(program);
} catch (e) {
if (String(e).includes("negative shift amount in <<= operator")) {
// clamp the amount to 0 (or switch to >>= for negative) and retry
} else { throw e; }
} Prevention
- Clamp or branch on computed shift amounts before every `<<=`.
- If negative shifts are meaningful, use `>>=` explicitly for that case.
- Validate user/LLM-provided shift values are non-negative integers.
- Add tests with boundary shift values (0, negative, large).
When it happens
Trigger: Executing `x <<= y` where `y` is an Int < 0 at runtime, e.g. `x <<= -2` or a computed shift like `x <<= n - m` where `n < m`.
Common situations: Dynamic shift amounts derived from arithmetic or user/LLM input that can go negative; loop code where a counter was expected to be non-negative but isn't.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- shift <<= requires integer operands
- negative shift amount in >>= operator
- shift >>= requires integer operands
- bitwise ^= requires integer operands
- bitwise &= requires integer operands
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/a6ada077ecb8aa31.
Report an issue: GitHub.