{"record":{"id":"18924b0b661eac82","repo":"BoundaryML/baml","slug":"negative-shift-amount-in-operator-interpret","errorCode":null,"errorMessage":"negative shift amount in >>= operator","messagePattern":"negative shift amount in >>= operator","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1002,"sourceCode":"                            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()) {\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                        };\n\n                        // Assign the result back to the target expression\n                        assign_to_expr(\n                            left,\n                            result_val,\n                            scopes,\n                            thir,\n                            run_llm_function,\n                            watch_handler,\n                            function_name,\n                        )\n                        .await?;","sourceCodeStart":984,"sourceCodeEnd":1020,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L984-L1020","documentation":"This error is raised by the THIR interpreter while executing a `>>=` compound-assignment statement in a BAML function. After both operands have already been confirmed to be integers, the evaluator checks the right-hand shift amount `b` and aborts with `bail!` when it is negative, because Rust's `>>` on i64 panics (or overflows in debug) for negative shift counts, so the interpreter surfaces a controlled diagnostic instead of crashing the VM. The input at fault is the RHS expression of the `x >>= n` statement: it evaluated to an integer less than zero — typically the result of an arithmetic expression such as `x >>= a - b` where `a < b`. Fix by clamping or validating the shift amount (e.g. `if n < 0 { 0 } else { n }`) before the assignment.","triggerScenarios":"Executing `x >>= y` where `y` is an Int < 0 at runtime, e.g. `x >>= -1` or a computed expression like `x >>= k - j` that can go negative.","commonSituations":"Dynamic shift amounts from subtraction or external input that was assumed non-negative; mirrored left-shift code where the sign flipped.","solutions":["Clamp the shift amount: `let s = if d < 0 { 0 } else { d };` before `x >>= s;`.","If negative means a left shift, branch to `<<=` for that case.","Validate inputs that determine the shift amount."],"exampleFix":"// before (BAML)\nx >>= j - i; // error if j < i: negative shift amount in >>= operator\n\n// after\nlet amt = if j > i { j - i } else { 0 };\nx >>= amt;","handlingStrategy":"validation","validationCode":"// clamp the right-shift amount before executing >>=\nif (!Number.isInteger(sh) || sh < 0) sh = 0;\nconst clampShr = (n: number): number => (Number.isInteger(n) && n >= 0 ? n : 0);","typeGuard":"const validShiftAmount = (v: unknown): v is number => typeof v === \"number\" && Number.isInteger(v) && v >= 0;","tryCatchPattern":"try {\n  await runBaml(program);\n} catch (e) {\n  if (String(e).includes(\"negative shift amount in >>= operator\")) {\n    // clamp to 0 or route negative cases to <<= and retry\n  } else { throw e; }\n}","preventionTips":["Guard computed amounts like `a - b` with a max(0, ...) before `>>=`.","Validate external inputs feeding shift amounts are non-negative ints.","Branch deliberately between `<<=` and `>>=` instead of relying on sign.","Include negative-amount cases in shift tests."],"tags":["baml","interpreter","bitwise","shift"],"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-14T05:17:10.506Z"}