2017-10-26 22:22:48 -07:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2017-12-03 17:43:56 -08:00
|
|
|
const Value = union(enum) {
|
2017-10-26 22:22:48 -07:00
|
|
|
Int: u64,
|
|
|
|
Array: [9]u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
const Agg = struct {
|
|
|
|
val1: Value,
|
|
|
|
val2: Value,
|
|
|
|
};
|
|
|
|
|
2018-04-30 17:35:54 -07:00
|
|
|
const v1 = Value{ .Int = 1234 };
|
|
|
|
const v2 = Value{ .Array = []u8{3} ** 9 };
|
2017-10-26 22:22:48 -07:00
|
|
|
|
2018-04-30 17:35:54 -07:00
|
|
|
const err = (error!Agg)(Agg{
|
2017-10-26 22:22:48 -07:00
|
|
|
.val1 = v1,
|
|
|
|
.val2 = v2,
|
|
|
|
});
|
|
|
|
|
2018-04-30 17:35:54 -07:00
|
|
|
const array = []Value{
|
|
|
|
v1,
|
|
|
|
v2,
|
|
|
|
v1,
|
|
|
|
v2,
|
|
|
|
};
|
2017-10-26 22:22:48 -07:00
|
|
|
|
|
|
|
test "unions embedded in aggregate types" {
|
|
|
|
switch (array[1]) {
|
|
|
|
Value.Array => |arr| assert(arr[4] == 3),
|
|
|
|
else => unreachable,
|
|
|
|
}
|
2018-04-30 17:35:54 -07:00
|
|
|
switch ((err catch unreachable).val1) {
|
2017-10-26 22:22:48 -07:00
|
|
|
Value.Int => |x| assert(x == 1234),
|
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 20:53:53 -08:00
|
|
|
|
|
|
|
const Foo = union {
|
|
|
|
float: f64,
|
|
|
|
int: i32,
|
|
|
|
};
|
|
|
|
|
|
|
|
test "basic unions" {
|
2018-04-30 17:35:54 -07:00
|
|
|
var foo = Foo{ .int = 1 };
|
2017-11-14 20:53:53 -08:00
|
|
|
assert(foo.int == 1);
|
2018-04-30 17:35:54 -07:00
|
|
|
foo = Foo{ .float = 12.34 };
|
2017-11-14 20:53:53 -08:00
|
|
|
assert(foo.float == 12.34);
|
|
|
|
}
|
2017-11-15 19:52:47 -08:00
|
|
|
|
2018-05-01 03:00:39 -07:00
|
|
|
test "comptime union field access" {
|
|
|
|
comptime {
|
2018-05-28 17:23:55 -07:00
|
|
|
var foo = Foo{ .int = 0 };
|
2018-05-01 03:00:39 -07:00
|
|
|
assert(foo.int == 0);
|
|
|
|
|
2018-05-28 17:23:55 -07:00
|
|
|
foo = Foo{ .float = 42.42 };
|
2018-05-01 03:00:39 -07:00
|
|
|
assert(foo.float == 42.42);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 19:06:08 -08:00
|
|
|
test "init union with runtime value" {
|
|
|
|
var foo: Foo = undefined;
|
|
|
|
|
|
|
|
setFloat(&foo, 12.34);
|
|
|
|
assert(foo.float == 12.34);
|
|
|
|
|
|
|
|
setInt(&foo, 42);
|
|
|
|
assert(foo.int == 42);
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn setFloat(foo: *Foo, x: f64) void {
|
2018-04-30 17:35:54 -07:00
|
|
|
foo.* = Foo{ .float = x };
|
2017-11-16 19:06:08 -08:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn setInt(foo: *Foo, x: i32) void {
|
2018-04-30 17:35:54 -07:00
|
|
|
foo.* = Foo{ .int = x };
|
2017-11-16 19:06:08 -08:00
|
|
|
}
|
2017-11-15 19:52:47 -08:00
|
|
|
|
|
|
|
const FooExtern = extern union {
|
|
|
|
float: f64,
|
|
|
|
int: i32,
|
|
|
|
};
|
|
|
|
|
|
|
|
test "basic extern unions" {
|
2018-04-30 17:35:54 -07:00
|
|
|
var foo = FooExtern{ .int = 1 };
|
2017-11-15 19:52:47 -08:00
|
|
|
assert(foo.int == 1);
|
|
|
|
foo.float = 12.34;
|
|
|
|
assert(foo.float == 12.34);
|
|
|
|
}
|
2017-11-16 19:06:08 -08:00
|
|
|
|
2017-12-03 17:43:56 -08:00
|
|
|
const Letter = enum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
};
|
|
|
|
const Payload = union(Letter) {
|
|
|
|
A: i32,
|
|
|
|
B: f64,
|
|
|
|
C: bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
test "union with specified enum tag" {
|
|
|
|
doTest();
|
|
|
|
comptime doTest();
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn doTest() void {
|
2018-04-30 17:35:54 -07:00
|
|
|
assert(bar(Payload{ .A = 1234 }) == -10);
|
2017-12-03 17:43:56 -08:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn bar(value: *const Payload) i32 {
|
2018-04-30 17:35:54 -07:00
|
|
|
assert(Letter(value.*) == Letter.A);
|
|
|
|
return switch (value.*) {
|
2017-12-03 17:43:56 -08:00
|
|
|
Payload.A => |x| return x - 1244,
|
|
|
|
Payload.B => |x| if (x == 12.34) i32(20) else 21,
|
|
|
|
Payload.C => |x| if (x) i32(30) else 31,
|
|
|
|
};
|
|
|
|
}
|
2017-12-03 19:36:01 -08:00
|
|
|
|
2017-12-03 21:32:12 -08:00
|
|
|
const MultipleChoice = union(enum(u32)) {
|
|
|
|
A = 20,
|
|
|
|
B = 40,
|
|
|
|
C = 60,
|
|
|
|
D = 1000,
|
|
|
|
};
|
|
|
|
test "simple union(enum(u32))" {
|
|
|
|
var x = MultipleChoice.C;
|
|
|
|
assert(x == MultipleChoice.C);
|
2018-06-19 00:50:38 -07:00
|
|
|
assert(@enumToInt(@TagType(MultipleChoice)(x)) == 60);
|
2017-12-03 21:32:12 -08:00
|
|
|
}
|
|
|
|
|
2017-12-03 19:36:01 -08:00
|
|
|
const MultipleChoice2 = union(enum(u32)) {
|
|
|
|
Unspecified1: i32,
|
|
|
|
A: f32 = 20,
|
|
|
|
Unspecified2: void,
|
|
|
|
B: bool = 40,
|
|
|
|
Unspecified3: i32,
|
|
|
|
C: i8 = 60,
|
|
|
|
Unspecified4: void,
|
|
|
|
D: void = 1000,
|
|
|
|
Unspecified5: i32,
|
|
|
|
};
|
|
|
|
|
|
|
|
test "union(enum(u32)) with specified and unspecified tag values" {
|
|
|
|
comptime assert(@TagType(@TagType(MultipleChoice2)) == u32);
|
2018-04-30 17:35:54 -07:00
|
|
|
testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
|
|
|
|
comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
|
2017-12-03 19:36:01 -08:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: *const MultipleChoice2) void {
|
2018-06-19 00:50:38 -07:00
|
|
|
assert(@enumToInt(@TagType(MultipleChoice2)(x.*)) == 60);
|
2018-04-30 17:35:54 -07:00
|
|
|
assert(1123 == switch (x.*) {
|
2017-12-03 19:36:01 -08:00
|
|
|
MultipleChoice2.A => 1,
|
|
|
|
MultipleChoice2.B => 2,
|
|
|
|
MultipleChoice2.C => |v| i32(1000) + v,
|
|
|
|
MultipleChoice2.D => 4,
|
|
|
|
MultipleChoice2.Unspecified1 => 5,
|
|
|
|
MultipleChoice2.Unspecified2 => 6,
|
|
|
|
MultipleChoice2.Unspecified3 => 7,
|
|
|
|
MultipleChoice2.Unspecified4 => 8,
|
|
|
|
MultipleChoice2.Unspecified5 => 9,
|
|
|
|
});
|
|
|
|
}
|
2017-12-03 21:32:12 -08:00
|
|
|
|
2017-12-03 23:04:08 -08:00
|
|
|
const ExternPtrOrInt = extern union {
|
2018-05-31 07:56:59 -07:00
|
|
|
ptr: *u8,
|
2018-04-30 17:35:54 -07:00
|
|
|
int: u64,
|
2017-12-03 23:04:08 -08:00
|
|
|
};
|
|
|
|
test "extern union size" {
|
|
|
|
comptime assert(@sizeOf(ExternPtrOrInt) == 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
const PackedPtrOrInt = packed union {
|
2018-05-31 07:56:59 -07:00
|
|
|
ptr: *u8,
|
2018-04-30 17:35:54 -07:00
|
|
|
int: u64,
|
2017-12-03 23:04:08 -08:00
|
|
|
};
|
|
|
|
test "extern union size" {
|
|
|
|
comptime assert(@sizeOf(PackedPtrOrInt) == 8);
|
|
|
|
}
|
2017-12-03 23:05:33 -08:00
|
|
|
|
|
|
|
const ZeroBits = union {
|
|
|
|
OnlyField: void,
|
|
|
|
};
|
|
|
|
test "union with only 1 field which is void should be zero bits" {
|
|
|
|
comptime assert(@sizeOf(ZeroBits) == 0);
|
|
|
|
}
|
2017-12-03 23:12:13 -08:00
|
|
|
|
2018-04-30 17:35:54 -07:00
|
|
|
const TheTag = enum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
};
|
|
|
|
const TheUnion = union(TheTag) {
|
|
|
|
A: i32,
|
|
|
|
B: i32,
|
|
|
|
C: i32,
|
|
|
|
};
|
2017-12-03 23:12:13 -08:00
|
|
|
test "union field access gives the enum values" {
|
|
|
|
assert(TheUnion.A == TheTag.A);
|
|
|
|
assert(TheUnion.B == TheTag.B);
|
|
|
|
assert(TheUnion.C == TheTag.C);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "cast union to tag type of union" {
|
2018-04-30 17:35:54 -07:00
|
|
|
testCastUnionToTagType(TheUnion{ .B = 1234 });
|
|
|
|
comptime testCastUnionToTagType(TheUnion{ .B = 1234 });
|
2017-12-03 23:12:13 -08:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn testCastUnionToTagType(x: *const TheUnion) void {
|
2018-04-30 17:35:54 -07:00
|
|
|
assert(TheTag(x.*) == TheTag.B);
|
2017-12-03 23:12:13 -08:00
|
|
|
}
|
2017-12-05 17:46:58 -08:00
|
|
|
|
|
|
|
test "cast tag type of union to union" {
|
|
|
|
var x: Value2 = Letter2.B;
|
|
|
|
assert(Letter2(x) == Letter2.B);
|
|
|
|
}
|
2018-04-30 17:35:54 -07:00
|
|
|
const Letter2 = enum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
};
|
|
|
|
const Value2 = union(Letter2) {
|
|
|
|
A: i32,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
};
|
2017-12-05 18:10:47 -08:00
|
|
|
|
|
|
|
test "implicit cast union to its tag type" {
|
|
|
|
var x: Value2 = Letter2.B;
|
2017-12-05 18:33:24 -08:00
|
|
|
assert(x == Letter2.B);
|
2017-12-05 18:10:47 -08:00
|
|
|
giveMeLetterB(x);
|
|
|
|
}
|
2018-01-25 01:10:11 -08:00
|
|
|
fn giveMeLetterB(x: Letter2) void {
|
2017-12-05 18:10:47 -08:00
|
|
|
assert(x == Value2.B);
|
|
|
|
}
|
2017-12-08 14:49:14 -08:00
|
|
|
|
|
|
|
test "implicit cast from @EnumTagType(TheUnion) to &const TheUnion" {
|
|
|
|
assertIsTheUnion2Item1(TheUnion2.Item1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const TheUnion2 = union(enum) {
|
|
|
|
Item1,
|
|
|
|
Item2: i32,
|
|
|
|
};
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn assertIsTheUnion2Item1(value: *const TheUnion2) void {
|
2018-04-30 17:35:54 -07:00
|
|
|
assert(value.* == TheUnion2.Item1);
|
2017-12-08 14:49:14 -08:00
|
|
|
}
|
|
|
|
|
2017-12-24 01:10:26 -08:00
|
|
|
pub const PackThis = union(enum) {
|
|
|
|
Invalid: bool,
|
|
|
|
StringLiteral: u2,
|
|
|
|
};
|
|
|
|
|
|
|
|
test "constant packed union" {
|
2018-04-30 17:35:54 -07:00
|
|
|
testConstPackedUnion([]PackThis{PackThis{ .StringLiteral = 1 }});
|
2017-12-24 01:10:26 -08:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn testConstPackedUnion(expected_tokens: []const PackThis) void {
|
2017-12-24 01:10:26 -08:00
|
|
|
assert(expected_tokens[0].StringLiteral == 1);
|
|
|
|
}
|
2018-01-22 14:23:23 -08:00
|
|
|
|
|
|
|
test "switch on union with only 1 field" {
|
|
|
|
var r: PartialInst = undefined;
|
|
|
|
r = PartialInst.Compiled;
|
|
|
|
switch (r) {
|
|
|
|
PartialInst.Compiled => {
|
|
|
|
var z: PartialInstWithPayload = undefined;
|
2018-04-30 17:35:54 -07:00
|
|
|
z = PartialInstWithPayload{ .Compiled = 1234 };
|
2018-01-22 14:23:23 -08:00
|
|
|
switch (z) {
|
|
|
|
PartialInstWithPayload.Compiled => |x| {
|
|
|
|
assert(x == 1234);
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PartialInst = union(enum) {
|
|
|
|
Compiled,
|
|
|
|
};
|
|
|
|
|
|
|
|
const PartialInstWithPayload = union(enum) {
|
|
|
|
Compiled: i32,
|
|
|
|
};
|
|
|
|
|
2018-05-07 13:43:20 -07:00
|
|
|
test "access a member of tagged union with conflicting enum tag name" {
|
|
|
|
const Bar = union(enum) {
|
|
|
|
A: A,
|
|
|
|
B: B,
|
|
|
|
|
|
|
|
const A = u8;
|
|
|
|
const B = void;
|
|
|
|
};
|
|
|
|
|
|
|
|
comptime assert(Bar.A == u8);
|
|
|
|
}
|
2018-08-03 12:21:08 -07:00
|
|
|
|
|
|
|
test "tagged union initialization with runtime void" {
|
|
|
|
assert(testTaggedUnionInit({}));
|
|
|
|
}
|
|
|
|
|
|
|
|
const TaggedUnionWithAVoid = union(enum) {
|
|
|
|
A,
|
|
|
|
B: i32,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn testTaggedUnionInit(x: var) bool {
|
|
|
|
const y = TaggedUnionWithAVoid{ .A = x };
|
|
|
|
return @TagType(TaggedUnionWithAVoid)(y) == TaggedUnionWithAVoid.A;
|
|
|
|
}
|
2018-09-05 13:19:58 -07:00
|
|
|
|
|
|
|
pub const UnionEnumNoPayloads = union(enum) {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
};
|
|
|
|
|
|
|
|
test "tagged union with no payloads" {
|
|
|
|
const a = UnionEnumNoPayloads{ .B = {} };
|
|
|
|
switch (a) {
|
|
|
|
@TagType(UnionEnumNoPayloads).A => @panic("wrong"),
|
|
|
|
@TagType(UnionEnumNoPayloads).B => {},
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 10:29:43 -07:00
|
|
|
|
|
|
|
test "union with only 1 field casted to its enum type" {
|
|
|
|
const Literal = union(enum) {
|
|
|
|
Number: f64,
|
|
|
|
Bool: bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
const Expr = union(enum) {
|
|
|
|
Literal: Literal,
|
|
|
|
};
|
|
|
|
|
|
|
|
var e = Expr{ .Literal = Literal{ .Bool = true } };
|
|
|
|
const Tag = @TagType(Expr);
|
|
|
|
comptime assert(@TagType(Tag) == comptime_int);
|
|
|
|
var t = Tag(e);
|
|
|
|
assert(t == Expr.Literal);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "union with only 1 field casted to its enum type which has enum value specified" {
|
|
|
|
const Literal = union(enum) {
|
|
|
|
Number: f64,
|
|
|
|
Bool: bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
const Tag = enum {
|
|
|
|
Literal = 33,
|
|
|
|
};
|
|
|
|
|
|
|
|
const Expr = union(Tag) {
|
|
|
|
Literal: Literal,
|
|
|
|
};
|
|
|
|
|
|
|
|
var e = Expr{ .Literal = Literal{ .Bool = true } };
|
|
|
|
comptime assert(@TagType(Tag) == comptime_int);
|
|
|
|
var t = Tag(e);
|
|
|
|
assert(t == Expr.Literal);
|
|
|
|
assert(@enumToInt(t) == 33);
|
|
|
|
comptime assert(@enumToInt(t) == 33);
|
|
|
|
}
|