authorgravatar for contact@leroycepearson.devLeRoyce Pearson <contact@leroycepearson.dev> 2022-08-09 16:08:59-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-10 16:14:30-04:00
log0e118ed0aca3d852d2499fa37d04adef62b03ead
tree16192710018414ad7cfc46833e29f724d9abbd4b
parent8fd20a5eb015fda4b27cef14c9e3149575861994

stage2: add compile error for shlExact overflow

- moves a stage1 test case and makes it target `llvm` backend instead of `stage1` backend

3 files changed, 15 insertions(+), 17 deletions(-)

src/Sema.zig+5-7
......@@ -10269,16 +10269,14 @@ fn zirShl(
1026910269
1027010270 const val = switch (air_tag) {
1027110271 .shl_exact => val: {
10272 const shifted = try lhs_val.shl(rhs_val, lhs_ty, sema.arena, target);
10272 const shifted = try lhs_val.shlWithOverflow(rhs_val, lhs_ty, sema.arena, target);
1027310273 if (scalar_ty.zigTypeTag() == .ComptimeInt) {
10274 break :val shifted;
10274 break :val shifted.wrapped_result;
1027510275 }
10276 const int_info = scalar_ty.intInfo(target);
10277 const truncated = try shifted.intTrunc(lhs_ty, sema.arena, int_info.signedness, int_info.bits, target);
10278 if (try sema.compare(block, src, truncated, .eq, shifted, lhs_ty)) {
10279 break :val shifted;
10276 if (shifted.overflowed.compareWithZero(.eq)) {
10277 break :val shifted.wrapped_result;
1028010278 }
10281 return sema.addConstUndef(lhs_ty);
10279 return sema.fail(block, src, "operation caused overflow", .{});
1028210280 },
1028310281
1028410282 .shl_sat => if (scalar_ty.zigTypeTag() == .ComptimeInt)
test/cases/compile_errors/shlExact_shifts_out_1_bits.zig created+10
......@@ -0,0 +1,10 @@
1comptime {
2 const x = @shlExact(@as(u8, 0b01010101), 2);
3 _ = x;
4}
5
6// error
7// backend=llvm
8// target=native
9//
10// :2:15: error: operation caused overflow
test/cases/compile_errors/stage1/obj/shlExact_shifts_out_1_bits.zig deleted-10
......@@ -1,10 +0,0 @@
1comptime {
2 const x = @shlExact(@as(u8, 0b01010101), 2);
3 _ = x;
4}
5
6// error
7// backend=stage1
8// target=native
9//
10// tmp.zig:2:15: error: operation caused overflow