diff --git a/src/Sema.zig b/src/Sema.zig index 8c6e3cf05c..a33c9bee6d 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -29875,6 +29875,31 @@ fn resolvePeerTypes( continue; }, .Vector => switch (chosen_ty_tag) { + .Vector => { + const chosen_len = chosen_ty.vectorLen(); + const candidate_len = candidate_ty.vectorLen(); + if (chosen_len != candidate_len) + continue; + + const chosen_child_ty = chosen_ty.childType(); + const candidate_child_ty = candidate_ty.childType(); + if (chosen_child_ty.zigTypeTag() == .Int and candidate_child_ty.zigTypeTag() == .Int) { + const chosen_info = chosen_child_ty.intInfo(target); + const candidate_info = candidate_child_ty.intInfo(target); + if (chosen_info.bits < candidate_info.bits) { + chosen = candidate; + chosen_i = candidate_i + 1; + } + continue; + } + if (chosen_child_ty.zigTypeTag() == .Float and candidate_child_ty.zigTypeTag() == .Float) { + if (chosen_ty.floatBits(target) < candidate_ty.floatBits(target)) { + chosen = candidate; + chosen_i = candidate_i + 1; + } + continue; + } + }, .Array => { chosen = candidate; chosen_i = candidate_i + 1; diff --git a/test/behavior/vector.zig b/test/behavior/vector.zig index 1d9d517a96..2c7be227c4 100644 --- a/test/behavior/vector.zig +++ b/test/behavior/vector.zig @@ -175,6 +175,25 @@ test "array to vector" { comptime try S.doTheTest(); } +test "peer type resolution with coercible element types" { + 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_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + + const S = struct { + fn doTheTest() !void { + var b: @Vector(2, u8) = .{ 1, 2 }; + var a: @Vector(2, u16) = .{ 2, 1 }; + var t: bool = true; + var c = if (t) a else b; + try std.testing.expect(@TypeOf(c) == @Vector(2, u16)); + } + }; + comptime try S.doTheTest(); +} + test "tuple to vector" { if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO