authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-05-01 13:00:39+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-05-01 13:00:39+03:00
logff1c4e1f13943b63dcb6d257a2ee58ae88d4b12a
treeca7b68900670baa3ca414be1864d41e64af82704
parent013f548202ae1ffb584c211a0ea2cea53b745583

Added tests.


3 files changed, 22 insertions(+), 1 deletions(-)

src/ir.cpp-1
......@@ -15975,7 +15975,6 @@ static void ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
1597515975 ensure_field_index(fn_def_val->type, "return_type", 7);
1597615976 fn_def_fields[7].special = ConstValSpecialStatic;
1597715977 fn_def_fields[7].type = ira->codegen->builtin_types.entry_type;
15978 // @TODO Check whether this is correct.
1597915978 if (fn_entry->src_implicit_return_type != nullptr)
1598015979 fn_def_fields[7].data.x_type = fn_entry->src_implicit_return_type;
1598115980 else if (fn_entry->type_entry->data.fn.gen_return_type != nullptr)
test/cases/union.zig+10
......@@ -45,6 +45,16 @@ test "basic unions" {
4545 assert(foo.float == 12.34);
4646}
4747
48test "comptime union field access" {
49 comptime {
50 var foo = Foo { .int = 0 };
51 assert(foo.int == 0);
52
53 foo = Foo { .float = 42.42 };
54 assert(foo.float == 42.42);
55 }
56}
57
4858test "init union with runtime value" {
4959 var foo: Foo = undefined;
5060
test/compile_errors.zig+12
......@@ -3209,4 +3209,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
32093209 \\}
32103210 ,
32113211 ".tmp_source.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset");
3212
3213 cases.add("invalid union field access in comptime",
3214 \\const Foo = union {
3215 \\ Bar: u8,
3216 \\ Baz: void,
3217 \\};
3218 \\comptime {
3219 \\ var foo = Foo {.Baz = {}};
3220 \\ const bar_val = foo.Bar;
3221 \\}
3222 ,
3223 ".tmp_source.zig:7:24: error: accessing union field 'Bar' while field 'Baz' is set");
32123224}