{"record":{"id":"a6ada077ecb8aa31","repo":"BoundaryML/baml","slug":"negative-shift-amount-in-operator","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":993,"sourceCode":"                            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()) {\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(","sourceCodeStart":975,"sourceCodeEnd":1011,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L975-L1011","documentation":"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`.","triggerScenarios":"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`.","commonSituations":"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.","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."],"exampleFix":"// before (BAML)\nx <<= n - m; // error if n < m: negative shift amount in <<= operator\n\n// after\nlet amt = if n > m { n - m } else { 0 };\nx <<= amt;","handlingStrategy":"validation","validationCode":"// clamp shift amount before executing <<=\nconst clampShift = (n: number): number => (Number.isInteger(n) && n >= 0 ? n : 0);\nif (!Number.isInteger(shift) || shift < 0) shift = 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 the amount to 0 (or switch to >>= for negative) and retry\n  } else { throw e; }\n}","preventionTips":["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)."],"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"}