Skip to content

Commit

Permalink
Sema: fix crash compiling @shlExact
Browse files Browse the repository at this point in the history
Updated `zirShl`, to compute `shl_exact` with `comptime_int` LHS operand
like `shl`, and added test case for `@shlExact` with `comptime_int` LHS
operand.
  • Loading branch information
amp-59 authored and andrewrk committed Jan 4, 2024
1 parent ecd520f commit 10016e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13247,32 +13247,20 @@ fn zirShl(
}
break :rs rhs_src;
};

const val = switch (air_tag) {
const val = if (scalar_ty.zigTypeTag(mod) == .ComptimeInt)
try lhs_val.shl(rhs_val, lhs_ty, sema.arena, mod)
else switch (air_tag) {
.shl_exact => val: {
const shifted = try lhs_val.shlWithOverflow(rhs_val, lhs_ty, sema.arena, mod);
if (scalar_ty.zigTypeTag(mod) == .ComptimeInt) {
break :val shifted.wrapped_result;
}
if (shifted.overflow_bit.compareAllWithZero(.eq, mod)) {
break :val shifted.wrapped_result;
}
return sema.fail(block, src, "operation caused overflow", .{});
},

.shl_sat => if (scalar_ty.zigTypeTag(mod) == .ComptimeInt)
try lhs_val.shl(rhs_val, lhs_ty, sema.arena, mod)
else
try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, mod),

.shl => if (scalar_ty.zigTypeTag(mod) == .ComptimeInt)
try lhs_val.shl(rhs_val, lhs_ty, sema.arena, mod)
else
try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, mod),

.shl_sat => try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, mod),
.shl => try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, mod),
else => unreachable,
};

return Air.internedToRef(val.toIntern());
} else lhs_src;

Expand Down
6 changes: 6 additions & 0 deletions test/cases/shl_exact_comptime_int_lhs.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export fn entry() void {
if (@shlExact(1, 1) != 2) @compileError("should be 2");
}

// compile
// output_mode=Obj

0 comments on commit 10016e0

Please sign in to comment.