{"record":{"id":"2b40a34a6cd6b0d9","repo":"BoundaryML/baml","slug":"ntypeargs-fits-u16","errorCode":null,"errorMessage":"ntypeargs fits u16","messagePattern":"ntypeargs fits u16","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_compiler2_emit/src/emit.rs","lineNumber":3833,"sourceCode":"\n    fn make_closure_with_type_args(\n        &mut self,\n        lambda_idx: usize,\n        capture_count: usize,\n        ntypeargs: usize,\n    ) -> Result<(), Self::Error> {\n        self.emit_make_closure_bytecode(lambda_idx, capture_count, ntypeargs);\n        Ok(())\n    }\n\n    fn make_generic_function(\n        &mut self,\n        item: &baml_compiler2_mir::ItemRef<'ctx>,\n        ntypeargs: usize,\n    ) -> Result<(), Self::Error> {\n        let func_name = item.to_string();\n        let global_idx = self.function_global_index(item, \"MakeGenericFunction: global not found\");\n        let ntypeargs = u16::try_from(ntypeargs).expect(\"ntypeargs fits u16\");\n        let inst = self.emit(Instruction::MakeGenericFunction {\n            function: GlobalIndex::from_raw(global_idx),\n            ntypeargs,\n        });\n        self.set_operand(inst, OperandMeta::Global(func_name));\n        Ok(())\n    }\n\n    fn make_generic_function_from_value(&mut self, ntypeargs: usize) -> Result<(), Self::Error> {\n        // The callable value and `ntypeargs` `Object::Type` values are already\n        // on the stack (pushed by `walk_rvalue_pull`); just emit the opcode.\n        let ntypeargs = u16::try_from(ntypeargs).expect(\"ntypeargs fits u16\");\n        self.emit(Instruction::MakeGenericFunctionFromValue { ntypeargs });\n        Ok(())\n    }\n\n    fn load_capture(&mut self, idx: usize) -> Result<(), Self::Error> {\n        if self.loading_for_closure_capture {","sourceCodeStart":3815,"sourceCodeEnd":3851,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_compiler2_emit/src/emit.rs#L3815-L3851","documentation":"In `make_generic_function` the compiler narrows a `usize` generic-type-argument count to `u16` for the `MakeGenericFunction` bytecode instruction via `u16::try_from(...).expect(...)`. The panic means a generic function was declared with more than 65535 type parameters, which the instruction encoding cannot represent.","triggerScenarios":"Emitting `MakeGenericFunction` for an item whose `ntypeargs` (number of generic type parameters) exceeds `u16::MAX`; in practice only reachable through synthesized/concatenated generic signatures or an arity-counting bug.","commonSituations":"Auto-generated code with pathologically large generic arity; an off-by-large bug when computing the parameter count for a MIR item.","solutions":["Reduce the number of type parameters on the offending generic function/declaration","Audit how `ntypeargs` is computed for the item; fix the count if it is inflated","If >65535 type args is a real requirement, widen the instruction field to u32 in both emitter and VM"],"exampleFix":"// before\nlet ntypeargs = u16::try_from(ntypeargs).expect(\"ntypeargs fits u16\");\n// after\nlet ntypeargs = u16::try_from(ntypeargs).map_err(|_| EmitError::TooManyTypeArgs(ntypeargs))?;","handlingStrategy":"validation","validationCode":"if ntypeargs > u16::MAX as usize {\n    return Err(format!(\"too many type args: {ntypeargs} > 65535\"));\n}","typeGuard":"fn fits_u16(n: usize) -> Option<u16> {\n    u16::try_from(n).ok()\n}","tryCatchPattern":"// Rust: validate before emit; catch_unwind only as a last resort\nstd::panic::catch_unwind(|| make_generic_function(item, ntypeargs))\n    .unwrap_or_else(|_| report_internal_error(\"ntypeargs exceeded u16\"));","preventionTips":["Validate generic arity at parse/declare time with a friendly limit (e.g., <= 255)","Return a compiler diagnostic instead of expect for encodable-range violations","Add fuzz/property tests that emit functions with large generic arities"],"tags":["compiler","rust","panic","integer-overflow","bytecode"],"backgroundTag":"value-out-of-range","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"}