From 15435597d9a0505b93c3b7ffd3f3f95be5fa31f0 Mon Sep 17 00:00:00 2001 From: brianferri Date: Fri, 28 Aug 2026 09:18:00 +0200 Subject: [PATCH] Sema: reject non-integer lhs of a saturating shift Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36510 Reviewed-by: mlugg --- src/Sema.zig | 8 ++++++-- .../saturating_shl_lhs_must_be_an_integer.zig | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/cases/compile_errors/saturating_shl_lhs_must_be_an_integer.zig diff --git a/src/Sema.zig b/src/Sema.zig index 1ff650b17b5918d4d732bf33209aee5741d4017d..fed1422c065e007420ef22a4f4a5e103a7138e61 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -12993,8 +12993,12 @@ fn zirShl( const scalar_rhs_ty = rhs_ty.scalarType(zcu); // AstGen currently forces the rhs of `<<` to coerce to the correct type before the `.shl` instruction, so - // we already know `scalar_rhs_ty` is valid for `.shl` -- we only need to validate for `.shl_sat`. - if (air_tag == .shl_sat) _ = try sema.checkIntType(block, rhs_src, scalar_rhs_ty); + // we already know `scalar_rhs_ty` is valid for `.shl`; likewise the lhs is validated when its + // `typeof_log2_int_type` is evaluated. `.shl_sat` gets neither coercion, so validate both operands here. + if (air_tag == .shl_sat) { + _ = try sema.log2IntType(block, lhs_ty, lhs_src); + _ = try sema.checkIntType(block, rhs_src, scalar_rhs_ty); + } const maybe_lhs_val = sema.resolveValue(lhs); const maybe_rhs_val = sema.resolveValue(rhs); diff --git a/test/cases/compile_errors/saturating_shl_lhs_must_be_an_integer.zig b/test/cases/compile_errors/saturating_shl_lhs_must_be_an_integer.zig new file mode 100644 index 0000000000000000000000000000000000000000..c5633f53f38abb4d80bd4b3a9dad1f01a7434266 --- /dev/null +++ b/test/cases/compile_errors/saturating_shl_lhs_must_be_an_integer.zig @@ -0,0 +1,12 @@ +export fn scalar() void { + _ = @as(f32, 1.5) <<| 2; +} + +export fn vector() void { + _ = @Vector(2, f32){ 1.5, 2.5 } <<| @Vector(2, u8){ 1, 1 }; +} + +// error +// +// :2:9: error: bit shifting operation expected integer type, found 'f32' +// :6:24: error: bit shifting operation expected integer type, found 'f32' -- 2.54.0