BoundaryML/baml · error

shift << requires integer operands at

Error message

shift << requires integer operands at {:?}

What it means

THIR interpreter runtime error: the `<<` shift-left operator was applied to non-integer operands. Shifts require Int on both sides; evaluating with any other type combination (including float) aborts with this bail and the expression metadata.

Solutions

  1. Ensure both operands are integers; convert/round floats and parse strings first.
  2. Replace bool operands with 0/1 integer literals.
  3. For boolean doubling logic, use multiplication or if/else instead of `<<`.

Example fix

// before (BAML)
let scaled = value << bitWidth; // bitWidth is float
// after
let scaled = value << intBitWidth; // ensured Int
Defensive patterns

Strategy: validation

Validate before calling

// check integer operands before <<
if (!Number.isInteger(value) || !Number.isInteger(shift)) throw new Error('shift << requires integer operands');

Type guard

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

Try / catch

try {
  result = evaluate(expr);
} catch (e) {
  if (String(e).includes('shift << requires integer operands')) {
    result = evaluate(coerceToInts(expr));
  } else throw e;
}

Prevention

When it happens

Trigger: Evaluating `a << b` where either operand is a bool, float, string, or null — e.g. `1 << width` where width is a float from JSON, or `flags << 2` with bool flags.

Common situations: Shift amounts parsed from JSON/config as floats; using bools as bit values instead of 0/1 ints; string-encoded numbers not converted before shifting.

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/a630ace6c984e8e8. Report an issue: GitHub.

Appendix: source

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

            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                BamlValueWithMeta::Int(a | b, meta.clone())
            }
            _ => bail!("bitwise | requires integer operands at {:?}", meta.0),
        },
        BinaryOperator::BitXor => match (left_val.clone(), right_val.clone()) {
            (BamlValueWithMeta::Int(a, _), BamlValueWithMeta::Int(b, _)) => {
                BamlValueWithMeta::Int(a ^ b, meta.clone())
            }
            _ => 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),
        },
    })
}

View on GitHub (pinned to bd85ce9dee)