From 815a6245a58aceb51948a77b10b479937380c01c Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sun, 11 Oct 2026 09:51:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(narrow):=20HandlesAnnAssign.=5FStoreWithCoe?= =?UTF-8?q?rce=20=E8=A1=A5=E6=B3=A8=E8=A7=A3=E5=BC=8F=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E7=AA=84=E5=8C=96=20fail-fast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 此前该路径对整数窄化静默 trunc,导致 x: <32位> = <64位> 注解声明绕过规则;现与 MemoryOps._coerce_value 同口径:豁免 i1 归一化与落进目标位宽的编译期常量,其余抛 FailFastError --- lib/core/Handles/HandlesAnnAssign.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/core/Handles/HandlesAnnAssign.py b/lib/core/Handles/HandlesAnnAssign.py index c3cb6f2..801d911 100644 --- a/lib/core/Handles/HandlesAnnAssign.py +++ b/lib/core/Handles/HandlesAnnAssign.py @@ -281,8 +281,19 @@ class AnnAssignHandle(BaseHandle): # 不是 trunc 取最低位(运行时值 2 会得 false,与 `if 2:` 相反) if TargetType.width == 1: Value = Gen._CoerceIntToI1(Value, name="bool_norm") + # 隐式窄化**禁止**(与 MemoryOps._coerce_value 同口径,规则 2026-10-10): + # 位宽高→低会丢高位,必须显式 cast 或加宽目标类型。 + # 唯一豁免:源是**编译期常量且值落进目标位宽**(无信息损失)。 + elif (isinstance(Value, ir.Constant) and isinstance(Value.constant, int) + and -(1 << (TargetType.width - 1)) <= Value.constant <= (1 << TargetType.width) - 1): + Value = Gen.builder.trunc(Value, TargetType, name="trunc_const") else: - Value = Gen.builder.trunc(Value, TargetType, name="trunc") + from lib.core.LLVMCG.FuncGen import FailFastError + raise FailFastError( + f"隐式窄化禁止:{Value.type.width} 位整数不能隐式赋给 {TargetType.width} 位类型" + f"(会丢高位);请显式转换(如 int32(x) / t.CInt32T(x) )或把目标类型加宽" + f"{Gen._DescribeValueNode(Node)}" + ) else: Value = Gen.builder.zext(Value, TargetType, name="zext") elif isinstance(TargetType, ir.PointerType) and isinstance(Value.type, ir.PointerType):