From 0ad1239522c70418990dc7b9da4e128da7cdd1d5 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 3 Dec 2017 20:43:56 -0500 Subject: rework enums and unions and their relationship to each other * @enumTagName renamed to @tagName and it works on enums and union-enums * Remove the EnumTag type. Now there is only enum and union, and the tag type of a union is always an enum. * unions support specifying the tag enum type, and they support inferring an enum tag type. * Enums no longer support field types but they do support setting the tag values. Likewise union-enums when inferring an enum tag type support setting the tag values. * It is now an error for enums and unions to have 0 fields. * switch statements support union-enums closes #618 --- test/compile_errors.zig | 54 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 10 deletions(-) (limited to 'test/compile_errors.zig') diff --git a/test/compile_errors.zig b/test/compile_errors.zig index e1de167ac5..ac06b5aa2a 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -930,8 +930,8 @@ pub fn addCases(cases: &tests.CompileErrorContext) { \\fn bad_eql_1(a: []u8, b: []u8) -> bool { \\ a == b \\} - \\const EnumWithData = enum { - \\ One, + \\const EnumWithData = union(enum) { + \\ One: void, \\ Two: i32, \\}; \\fn bad_eql_2(a: &const EnumWithData, b: &const EnumWithData) -> bool { @@ -1145,19 +1145,19 @@ pub fn addCases(cases: &tests.CompileErrorContext) { \\const JasonHM = u8; \\const JasonList = &JsonNode; \\ - \\const JsonOA = enum { + \\const JsonOA = union(enum) { \\ JSONArray: JsonList, \\ JSONObject: JasonHM, \\}; \\ - \\const JsonType = enum { + \\const JsonType = union(enum) { \\ JSONNull: void, \\ JSONInteger: isize, \\ JSONDouble: f64, \\ JSONBool: bool, \\ JSONString: []u8, - \\ JSONArray, - \\ JSONObject, + \\ JSONArray: void, + \\ JSONObject: void, \\}; \\ \\pub const JsonNode = struct { @@ -2138,7 +2138,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { \\ \\const MdText = ArrayList(u8); \\ - \\const MdNode = enum { + \\const MdNode = union(enum) { \\ Header: struct { \\ text: MdText, \\ weight: HeaderValue, @@ -2297,6 +2297,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) { , ".tmp_source.zig:2:21: error: type 'i32' does not support @memberType"); + cases.add("@memberType on enum", + \\comptime { + \\ _ = @memberType(Foo, 0); + \\} + \\const Foo = enum {A,}; + , + ".tmp_source.zig:2:21: error: type 'Foo' does not support @memberType"); + cases.add("@memberType struct out of bounds", \\comptime { \\ _ = @memberType(Foo, 0); @@ -2305,11 +2313,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) { , ".tmp_source.zig:2:26: error: member index 0 out of bounds; 'Foo' has 0 members"); - cases.add("@memberType enum out of bounds", + cases.add("@memberType union out of bounds", \\comptime { \\ _ = @memberType(Foo, 1); \\} - \\const Foo = enum {A,}; + \\const Foo = union {A: void,}; , ".tmp_source.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members"); @@ -2336,6 +2344,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) { , ".tmp_source.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members"); + cases.add("@memberName union out of bounds", + \\comptime { + \\ _ = @memberName(Foo, 1); + \\} + \\const Foo = union {A:i32,}; + , + ".tmp_source.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members"); + cases.add("calling var args extern function, passing array instead of pointer", \\export fn entry() { \\ foo("hello"); @@ -2466,7 +2482,8 @@ pub fn addCases(cases: &tests.CompileErrorContext) { \\ var x: MultipleChoice = undefined; \\} , - ".tmp_source.zig:2:14: error: enums, not unions, support field assignment"); + ".tmp_source.zig:2:14: error: non-enum union field assignment", + ".tmp_source.zig:1:24: note: consider 'union(enum)' here"); cases.add("enum with 0 fields", \\const Foo = enum {}; @@ -2490,4 +2507,21 @@ pub fn addCases(cases: &tests.CompileErrorContext) { , ".tmp_source.zig:6:9: error: enum tag value 60 already taken", ".tmp_source.zig:4:9: note: other occurrence here"); + + cases.add("union with specified enum omits field", + \\const Letter = enum { + \\ A, + \\ B, + \\ C, + \\}; + \\const Payload = union(Letter) { + \\ A: i32, + \\ B: f64, + \\}; + \\export fn entry() -> usize { + \\ return @sizeOf(Payload); + \\} + , + ".tmp_source.zig:6:17: error: enum field missing: 'C'", + ".tmp_source.zig:4:5: note: declared here"); } -- cgit v1.2.3