Merge pull request #18376 from amp-59/shl_exact_comptime_int_to_shl

Sema: Updated `zirShl` to compute `shl_exact` with `comptime_int` LHS using `shl`
This commit is contained in:
Andrew Kelley 2024-01-04 04:19:26 -08:00 committed by GitHub
commit 9a16085093
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 17 deletions

View File

@ -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.
</p>
<p>
{#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.
</p>
{#see_also|@shrExact|@shlWithOverflow#}
{#header_close#}

View File

@ -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;

View File

@ -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);