From 10016e0368c382363e168e45fac2cca4bc64e38f Mon Sep 17 00:00:00 2001 From: amp-59 <114923809+amp-59@users.noreply.github.com> Date: Tue, 26 Dec 2023 23:14:48 +0000 Subject: [PATCH 1/2] 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. --- src/Sema.zig | 22 +++++----------------- test/cases/shl_exact_comptime_int_lhs.zig | 6 ++++++ 2 files changed, 11 insertions(+), 17 deletions(-) create mode 100644 test/cases/shl_exact_comptime_int_lhs.zig diff --git a/src/Sema.zig b/src/Sema.zig index a2ed81161b..c6cb1a8b2f 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -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; diff --git a/test/cases/shl_exact_comptime_int_lhs.zig b/test/cases/shl_exact_comptime_int_lhs.zig new file mode 100644 index 0000000000..60860f5113 --- /dev/null +++ b/test/cases/shl_exact_comptime_int_lhs.zig @@ -0,0 +1,6 @@ +export fn entry() void { + if (@shlExact(1, 1) != 2) @compileError("should be 2"); +} + +// compile +// output_mode=Obj From 52ebba6bdf5662353ce11a701a3361cc0616c01c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 4 Jan 2024 00:44:44 -0700 Subject: [PATCH 2/2] `@shlExact` fixups * Add clarification in langref * move test case to behavior tests --- doc/langref.html.in | 5 +++++ test/behavior/math.zig | 2 ++ test/cases/shl_exact_comptime_int_lhs.zig | 6 ------ 3 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 test/cases/shl_exact_comptime_int_lhs.zig diff --git a/doc/langref.html.in b/doc/langref.html.in index c07633273b..7b5d086f2d 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -9163,6 +9163,11 @@ test "@setRuntimeSafety" { The type of {#syntax#}shift_amt{#endsyntax#} is an unsigned integer with {#syntax#}log2(@typeInfo(T).Int.bits){#endsyntax#} bits. This is because {#syntax#}shift_amt >= @typeInfo(T).Int.bits{#endsyntax#} is undefined behavior.

+

+ {#syntax#}comptime_int{#endsyntax#} is modeled as an integer with an infinite number of bits, + meaning that in such case, {#syntax#}@shlExact{#endsyntax#} always produces a result and + cannot produce a compile error. +

{#see_also|@shrExact|@shlWithOverflow#} {#header_close#} diff --git a/test/behavior/math.zig b/test/behavior/math.zig index 3d3c282854..4f9f7a673d 100644 --- a/test/behavior/math.zig +++ b/test/behavior/math.zig @@ -1314,6 +1314,8 @@ test "exact shift left" { try testShlExact(0b00110101); try comptime testShlExact(0b00110101); + + if (@shlExact(1, 1) != 2) @compileError("should be 2"); } fn testShlExact(x: u8) !void { const shifted = @shlExact(x, 2); diff --git a/test/cases/shl_exact_comptime_int_lhs.zig b/test/cases/shl_exact_comptime_int_lhs.zig deleted file mode 100644 index 60860f5113..0000000000 --- a/test/cases/shl_exact_comptime_int_lhs.zig +++ /dev/null @@ -1,6 +0,0 @@ -export fn entry() void { - if (@shlExact(1, 1) != 2) @compileError("should be 2"); -} - -// compile -// output_mode=Obj