BoundaryML/baml · error

type_arg_templates count fits in u16

Error message

type_arg_templates count fits in u16

What it means

The emitter encodes the number of type-argument templates in a u16 operand (ntypeargs) when emitting InitInstance. u16::try_from with .expect means exceeding 65535 type-arg templates is treated as an unrecoverable internal invariant violation rather than a user error. This is a compiler-side sanity assert.

Source

Thrown at baml_language/crates/baml_compiler2_emit/src/emit.rs:1719

        &mut self,
        class_name: &str,
        type_arg_templates: &[TyTemplate],
        fields: &[Operand<'ctx>],
    ) -> bool {
        if fields.is_empty()
            || fields
                .iter()
                .any(|field| Self::field_copy_operand(field).is_some())
        {
            return false;
        }

        for field in fields {
            self.emit_operand_pull(field);
        }

        let ntypeargs =
            u16::try_from(type_arg_templates.len()).expect("type_arg_templates count fits in u16");
        for template in type_arg_templates {
            unwrap_infallible(self.load_type(template));
        }
        self.emit_init_instance(class_name, ntypeargs, fields.len());
        true
    }

    fn place_mentions_stack_carried_local(&self, place: &Place) -> bool {
        match place {
            Place::Local(local) => matches!(
                self.analysis
                    .classifications
                    .get(local)
                    .copied()
                    .unwrap_or(LocalClassification::Real),
                LocalClassification::PhiLike
                    | LocalClassification::ReturnPhi
                    | LocalClassification::CallResultImmediate

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Report it as a compiler bug with the failing input; reduce the size/complexity of the instantiation that overflows u16
  2. Check for emitter regressions that duplicate type-arg templates for the same class
  3. Work around by splitting the huge class/instantiation into smaller ones
Defensive patterns

Strategy: try-catch

Try / catch

let ntypeargs = u16::try_from(type_arg_templates.len())
    .map_err(|_| anyhow!("too many type-arg templates: {}", type_arg_templates.len()))?;

Prevention

When it happens

Trigger: emit_init_instance path in emit.rs encountering a class instantiation whose type_arg_templates slice has more than 65535 entries — only possible with a pathologically large generated/desugared program or an emitter bug producing duplicate templates.

Common situations: Hitting this in normal code is essentially impossible; it appears from compiler bugs, code-generation blowup, or malicious/generated BAML with tens of thousands of type arguments in one instantiation.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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