feat(generic): 泛型函数类型实参推断与单态化

支持 def f[T](x: T) -> T 及 f(x)/f[T](x):模板注册(Phase1Scan/Phase2 顶层 FunctionDef,键 {sha1}.{name})、按形参注解对齐从实参推断类型实参(无来源即 fail-fast,不静默默认 int)、克隆模板并 set_generic_context 后翻译生成特化体(名字 {sha1}.f[int],规范化复用 _mangle_generic_class_name)、同签名去重、跨模块按模板签名补 declare。泛型函数模板不再发 vpi(模板签名含未绑定 T),也不再发射 int 兜底版本。已知限制(未静默):bound [T: bound] 解析期已被丢弃;t.CExport/t.CExtern/t.State 泛型函数、关键字/默认参数调用、mod.f[T](x) 显式下标等暂 fail-fast 或走既有报错。
This commit is contained in:
TermiNexus committed 2026-10-03 11:59:26 +08:00
1 parent 1fbebb2a62
commit 692998820c
10 files changed
+886 -57

No files matched your search

+7
View File
@@ -984,6 +984,13 @@ def _GeneratePyiFromAst(mb: memhub.MemBuddy*, tree: ast.AST*,
# 函数定义(_PyiFunction 已以 "\n" 收尾,不再补空行)
elif k == ast.ASTKind.FunctionDef:
fn: ast.FunctionDef* = ast.FunctionDef*(stmt)
# 泛型函数模板(def f[T])不发 vpi:模板签名含未绑定类型形参 T,
# 注入侧无法解析(旧行为会写出 `def f(x: T)` 的错误/失败签名)。
# 特化体按调用点单态化生成({模板SHA1}.f[args]),其签名由编译器
# 内存中的模板表 + 实参跨模块复现(见 ClassGenGeneric.
# _ensure_generic_func_declare),不落 vpi。
if fn.type_params is not None:
continue
pos = _PyiFunction(buf, buf_size, pos, fn, 0, "")
# 类定义