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 均为