BoundaryML/baml · error

shift >> requires integer operands at {:?}

Error message

shift >> requires integer operands at {:?}

What it means

This error is thrown by the BAML interpreter when the right-shift (>>) binary operator is applied to operands that are not both integers, or to a non-integer at all. The interpreter only implements >> for Int/Int pairs (with a non-negative shift amount); any other combination falls through to the catch-all bail. It exists because BAML's typechecker may not always catch operand types before evaluation (e.g. dynamic/any-typed values).

Source

Thrown at engine/baml-compiler/src/thir/interpret.rs:2526

            _ => bail!("bitwise ^ requires integer operands at {:?}", meta.0),
        },
        BinaryOperator::Shl => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                if b < 0 {
                    bail!("negative shift amount at {:?}", meta.0);
                }
                BamlValueWithMeta::Int(a << b, meta.clone())
            }
            _ => bail!("shift << requires integer operands at {:?}", meta.0),
        },
        BinaryOperator::Shr => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                if b < 0 {
                    bail!("negative shift amount at {:?}", meta.0);
                }
                BamlValueWithMeta::Int(a >> b, meta.clone())
            }
            _ => bail!("shift >> requires integer operands at {:?}", meta.0),
        },
        BinaryOperator::InstanceOf => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Class(class, ..), BamlValueWithMeta::Class(right_class, ..)) => {
                BamlValueWithMeta::Bool(class == right_class, meta.clone())
            }
            _ => bail!("instanceof requires class operands at {:?}", meta.0),
        },
    })
}

fn evaluate_unary_op(
    operator: &crate::hir::UnaryOperator,
    val: &BamlValueWithMeta<ExprMetadata>,
    meta: &ExprMetadata,
) -> Result<BamlValueWithMeta<ExprMetadata>> {
    use crate::hir::UnaryOperator;
    Ok(match operator {
        UnaryOperator::Not => match val.clone() {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check both operands of >> at runtime; wrap non-int operands in an integer conversion before shifting
  2. Verify the BAML types of the operands; annotate variables as int so the typechecker rejects floats/strings earlier
  3. If the value comes from JSON input, ensure the target type is Int and the JSON number is integral (see 'Expected integer' error)
  4. Replace >> with an equivalent integer-only expression (e.g. division by 2^n using int arithmetic) if operands are inherently fractional

Example fix

// before (BAML)
let scaled = amount >> 2;   // amount: float
// after
let scaled = (amount as int) >> 2;  // or make amount an int upstream
Defensive patterns

Strategy: type-guard

Validate before calling

// before shifting, verify integer operands
if (!Number.isInteger(a) || !Number.isInteger(b) || b < 0) {
  throw new Error(`shift operands must be non-negative ints, got a=${a} b=${b}`);
}

Type guard

const isInt = (v: unknown): v is number => typeof v === 'number' && Number.isInteger(v);

Try / catch

try {
  result = evaluate(expr);
} catch (e) {
  if (String(e).includes('shift >> requires integer operands')) {
    // coerce operands to int or fix expression
  }
  throw e;
}

Prevention

When it happens

Trigger: Evaluating a BAML expression `a >> b` where either `a` or `b` is a Float, String, Bool, or other non-Int BamlValue at runtime. Note that a separate error covers negative shift amounts; this one fires only for wrong operand types.

Common situations: Dividing then shifting with float results (e.g. `x / 2 >> y`), shifting a value parsed from JSON that was inferred as Float, or typos where a string flag is passed instead of an int mask.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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