diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-09-27 23:11:00 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-09-27 23:11:00 -0700 |
| commit | 09e1f37cb6a8e0df4c521f4b76eab07f0c811852 (patch) | |
| tree | cce1b86a1cd273841a660e5438f714cfbaea74a7 /src/value.zig | |
| parent | c2a7542df5e9e93289a5d487ba3bbc37c12ffc11 (diff) | |
| download | zig-09e1f37cb6a8e0df4c521f4b76eab07f0c811852.tar.gz zig-09e1f37cb6a8e0df4c521f4b76eab07f0c811852.zip | |
stage2: implement union coercion to its own tag
* AIR: add `get_union_tag` instruction
- implement in LLVM backend
* Sema: implement == and != for union and enum literal
- Also implement coercion from union to its own tag type
* Value: implement hashing for union values
The motivating example is this snippet:
comptime assert(@typeInfo(T) == .Float);
This was the next blocker for stage2 building compiler-rt.
Now it is switch at compile-time on an integer.
Diffstat (limited to 'src/value.zig')
| -rw-r--r-- | src/value.zig | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/value.zig b/src/value.zig index cb5d211b1e..69f8945e01 100644 --- a/src/value.zig +++ b/src/value.zig @@ -1275,7 +1275,12 @@ pub const Value = extern union { } }, .Union => { - @panic("TODO implement hashing union values"); + const union_obj = val.castTag(.@"union").?.data; + if (ty.unionTagType()) |tag_ty| { + union_obj.tag.hash(tag_ty, hasher); + } + const active_field_ty = ty.unionFieldType(union_obj.tag); + union_obj.val.hash(active_field_ty, hasher); }, .Fn => { @panic("TODO implement hashing function values"); @@ -1431,6 +1436,14 @@ pub const Value = extern union { } } + pub fn unionTag(val: Value) Value { + switch (val.tag()) { + .undef => return val, + .@"union" => return val.castTag(.@"union").?.data.tag, + else => unreachable, + } + } + /// Returns a pointer to the element value at the index. pub fn elemPtr(self: Value, allocator: *Allocator, index: usize) !Value { if (self.castTag(.elem_ptr)) |elem_ptr| { |
