zig

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

commit b5376050092bf63c870e8518db73717d0d183eba (tree)
parent 5e1ea76798ad781f24bea3f69ca6a37259b7b69e
Author: Motiejus <motiejus@jakstys.lt>
Date:   Sat,  7 Mar 2026 09:09:35 +0000

sema: fix zirCmpEq equality check to include sign for comptime ints

The comptime equality check compared only lo/hi magnitude fields,
ignoring is_negative. This meant (-5) == 5 would return true.

Fix by also checking lhs_neg == rhs_neg. Zero is never negative
(internComptimeInt already clears neg for zero values), so this
handles the -0 == 0 edge case correctly.

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

Diffstat:
Mstage0/sema.c | 5++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/stage0/sema.c b/stage0/sema.c @@ -1497,7 +1497,10 @@ static AirInstRef zirCmpEq( (void)semaCoerce(sema, block, result_ty, lhs); (void)semaCoerce(sema, block, result_ty, rhs); } - bool eq = (lhs_lo == rhs_lo && lhs_hi == rhs_hi); + // Equality: same magnitude AND same sign. + // Zero is never negative (internComptimeInt clears neg for 0). + bool eq + = (lhs_lo == rhs_lo) && (lhs_hi == rhs_hi) && (lhs_neg == rhs_neg); return AIR_REF_FROM_IP((eq == (air_tag == AIR_INST_CMP_EQ)) ? IP_INDEX_BOOL_TRUE : IP_INDEX_BOOL_FALSE);