diff options
| author | John Schmidt <john.schmidt.h@gmail.com> | 2023-03-08 17:49:52 +0100 |
|---|---|---|
| committer | John Schmidt <john.schmidt.h@gmail.com> | 2023-03-09 00:55:17 +0100 |
| commit | 505e720421ce4f7ed11730fe68d32e3bba711f3c (patch) | |
| tree | 9bb76c6995902416714832b5506218b8b12a8251 /src | |
| parent | 06b263825a67e68cec128c640a6287fa1716dc63 (diff) | |
| download | zig-505e720421ce4f7ed11730fe68d32e3bba711f3c.tar.gz zig-505e720421ce4f7ed11730fe68d32e3bba711f3c.zip | |
sema: add peer type resolution for vectors
This is consistent with how coercion for vectors work. So now you can do
this:
```
var a: @Vector(2, u16) = .{1, 2};
var b: @Vector(2, u8) = .{2, 1};
const c = @min(a, b);
```
where previously you had to cast explicitly:
```
var a: @Vector(2, u16) = .{1, 2};
var b: @Vector(2, u8) = .{2, 1};
var c: @Vector(2, u16) = b;
const c = @min(a, c);
```
Diffstat (limited to 'src')
| -rw-r--r-- | src/Sema.zig | 25 |
1 files changed, 25 insertions, 0 deletions
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; |
