BoundaryML/baml · error · VmPanic

baml.panics.NegativeBitShift

baml.panics.NegativeBitShift

Error message

negative bit shift: {message}

What it means

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.

Solutions

  1. Clamp or validate the shift amount before shifting: raise/skip when it is negative.
  2. Catch baml.panics.NegativeBitShift in BAML code and handle the negative case explicitly.
  3. Fix the upstream arithmetic that produces the negative shift count.
  4. Use explicit comparison/branching (`if shift < 0`) instead of shifting directly.

Example fix

// before
let result = value << amount; // amount may be negative
// after
let result = if amount < 0 {
  value >> -amount
} else {
  value << amount
};
Defensive patterns

Strategy: validation

Validate before calling

// BAML: validate shift amount before shifting
if shift_amount < 0 {
  return err("shift count must be >= 0");
}

Try / catch

try {
  let r = value << shift_amount;
} catch e: baml.panics.NegativeBitShift {
  let r = handle_negative_shift(value, shift_amount);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/067a48571b75e132. Report an issue: GitHub.

Appendix: source

Thrown at baml_language/crates/bex_vm_types/src/errors.rs:91

    /// BAML `int` is `i64`, so the signal carries the full value the
    /// user wrote; the host narrows to `i32` for `std::process::exit`.
    #[error("baml.sys.exit({code})")]
    Exit { code: i64 },

    /// The graceful-ish way to handle potential OOM errors, instead of hard-crashing.
    #[error("memory allocation failed: {message}")]
    AllocFailure { message: String },

    /// A required host resource is unavailable — e.g. the OS entropy source
    /// returned an error in a sandboxed runtime. Catchable so user code can
    /// fall back gracefully instead of aborting the host process.
    #[error("host resource '{resource}' unavailable: {message}")]
    HostUnavailable { resource: String, message: String },

    /// The right operand of a bigint shift (`<<` / `>>`) was negative.
    /// Catchable because the count is a runtime `bigint` and the type
    /// system can't rule out negative values.
    #[error("negative bit shift: {message}")]
    NegativeBitShift { message: String },

    /// A host callable returned a value of the wrong type, or threw a value
    /// that does not match its declared `throws` contract `E`. Surfaces in
    /// BAML as `baml.panics.HostContractViolation`.
    ///
    /// `class_name` / `language` are populated when the violation arose from
    /// a host throw (echoing the offending host exception's identity) and
    /// `None` when it arose from a wrong-type return (no exception class to
    /// echo).
    #[error("host contract violation: {message} [class={class_name:?}, lang={language:?}]")]
    HostContractViolation {
        message: String,
        class_name: Option<String>,
        language: Option<String>,
    },
}

View on GitHub (pinned to bd85ce9dee)