From ddf40e02c5fd3bcc6d9ee41c7b149d12af663156 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sun, 11 Oct 2026 12:22:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(deref):=20=E8=A7=A3=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E5=86=99=E5=85=A5=20*p=20=3D=20v=20=E9=87=87=E7=94=A8=20DerefA?= =?UTF-8?q?s=20=E5=AE=89=E5=85=A8=E8=AF=AD=E4=B9=89=EF=BC=88=E6=96=B9?= =?UTF-8?q?=E6=A1=881=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当 v 是指针且 pointee(p) != typeof(v)(原会因'指针<->非基本类型不匹配'fail-fast)时,把指针 p bitcast 成 typeof(v)* 再按值存 v,即 *(T*(p)) = v(DerefAs 期望写法;旧 DerefAs 的 *(intptr_t*(p))=(intptr_t)v 是错实现)。标量宽度等其余情形维持 pointee(p) 语义不变 --- lib/core/Handles/HandlesAssign.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/core/Handles/HandlesAssign.py b/lib/core/Handles/HandlesAssign.py index 59b9014..e5134d6 100644 --- a/lib/core/Handles/HandlesAssign.py +++ b/lib/core/Handles/HandlesAssign.py @@ -605,6 +605,15 @@ class AssignHandle(BaseHandle): if DerefValue is None: self._RaiseUnresolvedExpr("解引用写入", Node.value, Gen) return + # 方案1(DerefAs 的安全语义):`*p = v` 只在本会被"指针↔pointee 类型不匹配" + # 拒绝时才改用 `*(T*(p)) = v`(T = v 的类型):把 **指针** p bitcast 成 T* 再按值 + # 存 v。这正是 c.DerefAs 期望的写法(旧 DerefAs 的 `*(intptr_t*(p))=(intptr_t)v` + # 把"值"bitcast 成指针大小整数,是错实现)。标量宽度等其余情形维持原 + # `pointee(p)` 语义不变(不引入标量宽度漂移)。 + PtrPointee: ir.Type | None = Ptr.type.pointee if isinstance(Ptr.type, ir.PointerType) else None + if (isinstance(DerefValue.type, ir.PointerType) and PtrPointee is not None + and PtrPointee != DerefValue.type): + Ptr = Gen.builder.bitcast(Ptr, ir.PointerType(DerefValue.type), name="deref_store_as") self._StoreWithCoerce(DerefValue, Ptr, Node.value) elif isinstance(Target, (ast.Tuple, ast.List)): # ast.List 与 ast.Tuple 字段表逐字相同(Nodes.py:119-120 均为