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(...@@ -13247,32 +13247,20 @@ fn zirShl(
13247 }13247 }
13248 break :rs rhs_src;13248 break :rs rhs_src;
13249 };13249 };
1325013250 const val = if (scalar_ty.zigTypeTag(mod) == .ComptimeInt)
13251 const val = switch (air_tag) {13251 try lhs_val.shl(rhs_val, lhs_ty, sema.arena, mod)
13252 else switch (air_tag) {
13252 .shl_exact => val: {13253 .shl_exact => val: {
13253 const shifted = try lhs_val.shlWithOverflow(rhs_val, lhs_ty, sema.arena, mod);13254 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 }
13257 if (shifted.overflow_bit.compareAllWithZero(.eq, mod)) {13255 if (shifted.overflow_bit.compareAllWithZero(.eq, mod)) {
13258 break :val shifted.wrapped_result;13256 break :val shifted.wrapped_result;
13259 }13257 }
13260 return sema.fail(block, src, "operation caused overflow", .{});13258 return sema.fail(block, src, "operation caused overflow", .{});
13261 },13259 },
1326213260 .shl_sat => try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, mod),
13263 .shl_sat => if (scalar_ty.zigTypeTag(mod) == .ComptimeInt)13261 .shl => try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, mod),
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
13273 else => unreachable,13262 else => unreachable,
13274 };13263 };
13275
13276 return Air.internedToRef(val.toIntern());13264 return Air.internedToRef(val.toIntern());
13277 } else lhs_src;13265 } 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