From 1e7dcaa3ae57294ab5998b44a8c13ccc5019e7ea Mon Sep 17 00:00:00 2001 From: mlugg Date: Sat, 10 Jun 2023 01:50:05 +0100 Subject: [PATCH] Sema: resolve peer vector types before comparison Resolves: #15732 --- src/Sema.zig | 12 ++++++++---- test/behavior/vector.zig | 13 +++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index aed4dc7248..9e21bfa83d 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -30158,15 +30158,19 @@ fn cmpVector( assert(rhs_ty.zigTypeTag() == .Vector); try sema.checkVectorizableBinaryOperands(block, src, lhs_ty, rhs_ty, lhs_src, rhs_src); + const resolved_ty = try sema.resolvePeerTypes(block, src, &.{ lhs, rhs }, .{ .override = &.{ lhs_src, rhs_src } }); + const casted_lhs = try sema.coerce(block, resolved_ty, lhs, lhs_src); + const casted_rhs = try sema.coerce(block, resolved_ty, rhs, rhs_src); + const result_ty = try Type.vector(sema.arena, lhs_ty.vectorLen(), Type.bool); const runtime_src: LazySrcLoc = src: { - if (try sema.resolveMaybeUndefVal(lhs)) |lhs_val| { - if (try sema.resolveMaybeUndefVal(rhs)) |rhs_val| { + if (try sema.resolveMaybeUndefVal(casted_lhs)) |lhs_val| { + if (try sema.resolveMaybeUndefVal(casted_rhs)) |rhs_val| { if (lhs_val.isUndef() or rhs_val.isUndef()) { return sema.addConstUndef(result_ty); } - const cmp_val = try sema.compareVector(lhs_val, op, rhs_val, lhs_ty); + const cmp_val = try sema.compareVector(lhs_val, op, rhs_val, resolved_ty); return sema.addConstant(result_ty, cmp_val); } else { break :src rhs_src; @@ -30177,7 +30181,7 @@ fn cmpVector( }; try sema.requireRuntimeBlock(block, src, runtime_src); - return block.addCmpVector(lhs, rhs, op); + return block.addCmpVector(casted_lhs, casted_rhs, op); } fn wrapOptional( diff --git a/test/behavior/vector.zig b/test/behavior/vector.zig index fca845608b..4103f0c2c7 100644 --- a/test/behavior/vector.zig +++ b/test/behavior/vector.zig @@ -1330,3 +1330,16 @@ test "addition of vectors represented as strings" { const bar: V = @typeName(u32).*; try expectEqual(V{ 219, 162, 161 }, foo + bar); } + +test "compare vectors with different element types" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO + + var a: @Vector(2, u8) = .{ 1, 2 }; + var b: @Vector(2, u9) = .{ 3, 0 }; + try expectEqual(@Vector(2, bool){ true, false }, a < b); +}