zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit cd08f9002cddf1c4a49e9ff5012a6c00ecc11c15 (tree)
parent 4e623e11101dc7f900580772393c3d7855634291
Author: Motiejus <motiejus@jakstys.lt>
Date:   Sat,  7 Mar 2026 09:26:34 +0000

sema: add post-coercion float fold in analyzeArithmetic

Fixes a case where comptime_int + concrete_float would not fold at
comptime: after coercion, both become float IP entries, but the
pre-coercion float check had already passed without folding.

Added a second float fold check after resolvePeerTypes+coerce, so
mixed comptime_int/float operations fold correctly.

Ported from Sema.zig analyzeArithmetic which calls resolveValue on
the coerced operands.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>

Diffstat:
Mstage0/sema.c | 39+++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+), 0 deletions(-)

diff --git a/stage0/sema.c b/stage0/sema.c @@ -1041,6 +1041,45 @@ emit_runtime:; TypeIndex peer_ty = resolvePeerTypes(sema, lhs, rhs); lhs = semaCoerce(sema, block, peer_ty, lhs); rhs = semaCoerce(sema, block, peer_ty, rhs); + + // Post-coercion float fold: covers comptime_int + float, etc. + // Ported from Sema.zig analyzeArithmetic resolveValue after coercion. + if (AIR_REF_IS_IP(lhs) && AIR_REF_IS_IP(rhs)) { + InternPoolKey lkp = ipIndexToKey(sema->ip, AIR_REF_TO_IP(lhs)); + InternPoolKey rkp = ipIndexToKey(sema->ip, AIR_REF_TO_IP(rhs)); + if (lkp.tag == IP_KEY_FLOAT && rkp.tag == IP_KEY_FLOAT) { + double lv2 = lkp.data.float_val.val; + double rv2 = rkp.data.float_val.val; + double result2; + switch (air_tag) { + case AIR_INST_ADD: + case AIR_INST_ADD_WRAP: + case AIR_INST_ADD_SAT: + result2 = lv2 + rv2; + break; + case AIR_INST_SUB: + case AIR_INST_SUB_WRAP: + case AIR_INST_SUB_SAT: + result2 = lv2 - rv2; + break; + case AIR_INST_MUL: + case AIR_INST_MUL_WRAP: + case AIR_INST_MUL_SAT: + result2 = lv2 * rv2; + break; + default: + goto emit_air; + } + InternPoolKey key2; + memset(&key2, 0, sizeof(key2)); + key2.tag = IP_KEY_FLOAT; + key2.data.float_val.ty = peer_ty; + key2.data.float_val.val = result2; + return AIR_REF_FROM_IP(ipIntern(sema->ip, key2)); + } + } + +emit_air:; AirInstData data; memset(&data, 0, sizeof(data)); data.bin_op.lhs = lhs;