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/type.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/type.zig')
| -rw-r--r-- | src/type.zig | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/type.zig b/src/type.zig index bb798959f4..781fe74d45 100644 --- a/src/type.zig +++ b/src/type.zig @@ -2487,6 +2487,12 @@ pub const Type = extern union { }; } + pub fn unionFieldType(ty: Type, enum_tag: Value) Type { + const union_obj = ty.cast(Payload.Union).?.data; + const index = union_obj.tag_ty.enumTagFieldIndex(enum_tag).?; + return union_obj.fields.values()[index].ty; + } + /// Asserts that the type is an error union. pub fn errorUnionPayload(self: Type) Type { return switch (self.tag()) { @@ -3801,6 +3807,8 @@ pub const Type = extern union { }; }; + pub const @"bool" = initTag(.bool); + pub fn ptr(arena: *Allocator, d: Payload.Pointer.Data) !Type { assert(d.host_size == 0 or d.bit_offset < d.host_size * 8); |
