{"record":{"id":"067a48571b75e132","repo":"BoundaryML/baml","slug":"baml-panics-negativebitshift","errorCode":"baml.panics.NegativeBitShift","errorMessage":"negative bit shift: {message}","messagePattern":"negative bit shift: (.+?)","errorType":"error_code","errorClass":"VmPanic","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_vm_types/src/errors.rs","lineNumber":91,"sourceCode":"    /// BAML `int` is `i64`, so the signal carries the full value the\n    /// user wrote; the host narrows to `i32` for `std::process::exit`.\n    #[error(\"baml.sys.exit({code})\")]\n    Exit { code: i64 },\n\n    /// The graceful-ish way to handle potential OOM errors, instead of hard-crashing.\n    #[error(\"memory allocation failed: {message}\")]\n    AllocFailure { message: String },\n\n    /// A required host resource is unavailable — e.g. the OS entropy source\n    /// returned an error in a sandboxed runtime. Catchable so user code can\n    /// fall back gracefully instead of aborting the host process.\n    #[error(\"host resource '{resource}' unavailable: {message}\")]\n    HostUnavailable { resource: String, message: String },\n\n    /// The right operand of a bigint shift (`<<` / `>>`) was negative.\n    /// Catchable because the count is a runtime `bigint` and the type\n    /// system can't rule out negative values.\n    #[error(\"negative bit shift: {message}\")]\n    NegativeBitShift { message: String },\n\n    /// A host callable returned a value of the wrong type, or threw a value\n    /// that does not match its declared `throws` contract `E`. Surfaces in\n    /// BAML as `baml.panics.HostContractViolation`.\n    ///\n    /// `class_name` / `language` are populated when the violation arose from\n    /// a host throw (echoing the offending host exception's identity) and\n    /// `None` when it arose from a wrong-type return (no exception class to\n    /// echo).\n    #[error(\"host contract violation: {message} [class={class_name:?}, lang={language:?}]\")]\n    HostContractViolation {\n        message: String,\n        class_name: Option<String>,\n        language: Option<String>,\n    },\n}\n","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm_types/src/errors.rs#L73-L109","documentation":"This panic variant is raised when the right operand of a bigint shift (`<<` or `>>`) is negative. It is catchable because the shift amount is a runtime `bigint` value and the BAML type system cannot statically rule out negative counts.","triggerScenarios":"Executing `some_bigint << n` or `some_bigint >> n` where `n` is a bigint whose runtime value is negative, e.g. the result of user input or arithmetic that went below zero.","commonSituations":"Shifting by values computed from parsed user input or subtraction that underflowed; porting algorithms from languages where negative shifts silently misbehave or wrap; table-driven bit manipulation where the shift exponent can be negative.","solutions":["Clamp or validate the shift amount before shifting: raise/skip when it is negative.","Catch baml.panics.NegativeBitShift in BAML code and handle the negative case explicitly.","Fix the upstream arithmetic that produces the negative shift count.","Use explicit comparison/branching (`if shift < 0`) instead of shifting directly."],"exampleFix":"// before\nlet result = value << amount; // amount may be negative\n// after\nlet result = if amount < 0 {\n  value >> -amount\n} else {\n  value << amount\n};","handlingStrategy":"validation","validationCode":"// BAML: validate shift amount before shifting\nif shift_amount < 0 {\n  return err(\"shift count must be >= 0\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  let r = value << shift_amount;\n} catch e: baml.panics.NegativeBitShift {\n  let r = handle_negative_shift(value, shift_amount);\n}","preventionTips":["Clamp or branch on shift counts before shifting.","Check the arithmetic that computes shift counts for possible underflow.","Add unit tests covering negative shift inputs.","Prefer explicit direction handling (left vs right) over negating counts."],"tags":["bigint","bit-shift","arithmetic","vm-panic","catchable"],"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"}