{"record":{"id":"8122b30c9542af33","repo":"BoundaryML/baml","slug":"type-arg-templates-count-fits-in-u16","errorCode":null,"errorMessage":"type_arg_templates count fits in u16","messagePattern":"type_arg_templates count fits in u16","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_compiler2_emit/src/emit.rs","lineNumber":1719,"sourceCode":"        &mut self,\n        class_name: &str,\n        type_arg_templates: &[TyTemplate],\n        fields: &[Operand<'ctx>],\n    ) -> bool {\n        if fields.is_empty()\n            || fields\n                .iter()\n                .any(|field| Self::field_copy_operand(field).is_some())\n        {\n            return false;\n        }\n\n        for field in fields {\n            self.emit_operand_pull(field);\n        }\n\n        let ntypeargs =\n            u16::try_from(type_arg_templates.len()).expect(\"type_arg_templates count fits in u16\");\n        for template in type_arg_templates {\n            unwrap_infallible(self.load_type(template));\n        }\n        self.emit_init_instance(class_name, ntypeargs, fields.len());\n        true\n    }\n\n    fn place_mentions_stack_carried_local(&self, place: &Place) -> bool {\n        match place {\n            Place::Local(local) => matches!(\n                self.analysis\n                    .classifications\n                    .get(local)\n                    .copied()\n                    .unwrap_or(LocalClassification::Real),\n                LocalClassification::PhiLike\n                    | LocalClassification::ReturnPhi\n                    | LocalClassification::CallResultImmediate","sourceCodeStart":1701,"sourceCodeEnd":1737,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_compiler2_emit/src/emit.rs#L1701-L1737","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Report it as a compiler bug with the failing input; reduce the size/complexity of the instantiation that overflows u16","Check for emitter regressions that duplicate type-arg templates for the same class","Work around by splitting the huge class/instantiation into smaller ones"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"let ntypeargs = u16::try_from(type_arg_templates.len())\n    .map_err(|_| anyhow!(\"too many type-arg templates: {}\", type_arg_templates.len()))?;","preventionTips":["Treat u16::try_from(...).expect as a bug-report trigger, not a user fix","Minimize repro inputs before filing compiler issues","Watch for emitter changes that grow type-arg template lists","Split very large parameterized classes"],"tags":["compiler","internal","overflow","emit"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}