authorgravatar for 114923809+amp-59@users.noreply.github.comamp-59 <114923809+amp-59@users.noreply.github.com> 2023-12-26 23:14:48+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-04 00:10:41-07:00
log10016e0368c382363e168e45fac2cca4bc64e38f
treeca3e36c7c8e26b5c9353a40602d07293933a3a9c
parentecd520f6619c08cac17f8f662facc90595444ac5

Sema: fix crash compiling `@shlExact`

Updated `zirShl`, to compute `shl_exact` with `comptime_int` LHS operand like `shl`, and added test case for `@shlExact` with `comptime_int` LHS operand.

2 files changed, 11 insertions(+), 17 deletions(-)

src/Sema.zig+5-17
......@@ -13247,32 +13247,20 @@ fn zirShl(
1324713247 }
1324813248 break :rs rhs_src;
1324913249 };
13250
13251 const val = switch (air_tag) {
13250 const val = if (scalar_ty.zigTypeTag(mod) == .ComptimeInt)
13251 try lhs_val.shl(rhs_val, lhs_ty, sema.arena, mod)
13252 else switch (air_tag) {
1325213253 .shl_exact => val: {
1325313254 const shifted = try lhs_val.shlWithOverflow(rhs_val, lhs_ty, sema.arena, mod);
13254 if (scalar_ty.zigTypeTag(mod) == .ComptimeInt) {
13255 break :val shifted.wrapped_result;
13256 }
1325713255 if (shifted.overflow_bit.compareAllWithZero(.eq, mod)) {
1325813256 break :val shifted.wrapped_result;
1325913257 }
1326013258 return sema.fail(block, src, "operation caused overflow", .{});
1326113259 },
13262
13263 .shl_sat => if (scalar_ty.zigTypeTag(mod) == .ComptimeInt)
13264 try lhs_val.shl(rhs_val, lhs_ty, sema.arena, mod)
13265 else
13266 try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, mod),
13267
13268 .shl => if (scalar_ty.zigTypeTag(mod) == .ComptimeInt)
13269 try lhs_val.shl(rhs_val, lhs_ty, sema.arena, mod)
13270 else
13271 try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, mod),
13272
13260 .shl_sat => try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, mod),
13261 .shl => try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, mod),
1327313262 else => unreachable,
1327413263 };
13275
1327613264 return Air.internedToRef(val.toIntern());
1327713265 } else lhs_src;
1327813266
test/cases/shl_exact_comptime_int_lhs.zig created+6
......@@ -0,0 +1,6 @@
1export fn entry() void {
2 if (@shlExact(1, 1) != 2) @compileError("should be 2");
3}
4
5// compile
6// output_mode=Obj