fix(gen): new heap/memgr 的尺寸改用 ClassGenEnum._get_type_size,修结构体报 cannot determine the size

原先调用 TypeGenResolve.get_type_size_bytes,它只解析 typedef/标量,结构体一律返回 0,于是 new heap Point(3,4) 在语义期 fail-fast。

改用语言级 T.__sizeof__() 的同一实现 ClassGenEnum._get_type_size(struct_ty),保证 new 分配的尺寸与源码手写 __sizeof__() 完全一致(含对齐/填充)。
This commit is contained in:
TermiNexus committed 2026-10-08 18:07:28 +08:00
1 parent 14920c0e1e
commit c77d38e923
1 file changed
+6 -2
+6 -2
View File
@@ -340,9 +340,13 @@ novirtual class CallGenCtor(CallGenBase.CallGenBase):
self.Gen.StructGen.ensure_struct_def_in_module(pool, mod, struct_ty)
storage = self.Gen.VarGen._alloca_at_entry(builder, struct_ty)
else:
size_bytes: int = self.Gen.TypeGenResolve.get_type_size_bytes(pool, class_name, trans._imported_modules, trans._from_imports)
# 尺寸必须与语言级 `T.__sizeof__()` 用**同一实现**(ClassGenEnum._get_type_size),
# 否则 `new heap T()` 与源码手写的 `T.__sizeof__()` 会不一致。
# 不能用 TypeGenResolve.get_type_size_bytes:它只解析 typedef/标量,结构体一律返回 0
# (现场:NewTest 的 `new heap Point(3,4)` 报 "cannot determine the size of 'Point'")。
size_bytes: int = self.Gen.ClassGenEnum._get_type_size(struct_ty)
if size_bytes <= 0:
self.Gen.TypeGenErr.fatal_error(new_node, f"`new`: cannot determine the size of '{class_name}' (heap/memgr allocation needs a concrete layout)", "TypeError")
self.Gen.TypeGenErr.fatal_error(new_node, f"`new`: cannot determine the size of '{class_name}' (registered but size 0: field list not filled?); heap/memgr allocation needs a concrete layout", "TypeError")
return None
size_val: mlirlite.Value* = mlirlite.ConstInt(pool, mlirlite.Int64(pool), int64(size_bytes), "newsize")
raw: mlirlite.Value* = None