From c77d38e923c22c0f8516c21741b8796551ba2a88 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Thu, 8 Oct 2026 18:07:28 +0800 Subject: [PATCH] =?UTF-8?q?fix(gen):=20new=20heap/memgr=20=E7=9A=84?= =?UTF-8?q?=E5=B0=BA=E5=AF=B8=E6=94=B9=E7=94=A8=20ClassGenEnum.=5Fget=5Fty?= =?UTF-8?q?pe=5Fsize=EF=BC=8C=E4=BF=AE=E7=BB=93=E6=9E=84=E4=BD=93=E6=8A=A5?= =?UTF-8?q?=20cannot=20determine=20the=20size?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原先调用 TypeGenResolve.get_type_size_bytes,它只解析 typedef/标量,结构体一律返回 0,于是 new heap Point(3,4) 在语义期 fail-fast。 改用语言级 T.__sizeof__() 的同一实现 ClassGenEnum._get_type_size(struct_ty),保证 new 分配的尺寸与源码手写 __sizeof__() 完全一致(含对齐/填充)。 --- App/lib/core/Gen/Call/CallGenCtor.vp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/App/lib/core/Gen/Call/CallGenCtor.vp b/App/lib/core/Gen/Call/CallGenCtor.vp index 95b38b9..5ea0d4a 100644 --- a/App/lib/core/Gen/Call/CallGenCtor.vp +++ b/App/lib/core/Gen/Call/CallGenCtor.vp @@ -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