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...@@ -15975,7 +15975,6 @@ static void ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
15975 ensure_field_index(fn_def_val->type, "return_type", 7);15975 ensure_field_index(fn_def_val->type, "return_type", 7);
15976 fn_def_fields[7].special = ConstValSpecialStatic;15976 fn_def_fields[7].special = ConstValSpecialStatic;
15977 fn_def_fields[7].type = ira->codegen->builtin_types.entry_type;15977 fn_def_fields[7].type = ira->codegen->builtin_types.entry_type;
15978 // @TODO Check whether this is correct.
15979 if (fn_entry->src_implicit_return_type != nullptr)15978 if (fn_entry->src_implicit_return_type != nullptr)
15980 fn_def_fields[7].data.x_type = fn_entry->src_implicit_return_type;15979 fn_def_fields[7].data.x_type = fn_entry->src_implicit_return_type;
15981 else if (fn_entry->type_entry->data.fn.gen_return_type != nullptr)15980 else if (fn_entry->type_entry->data.fn.gen_return_type != nullptr)
test/cases/union.zig+10
...@@ -45,6 +45,16 @@ test "basic unions" {...@@ -45,6 +45,16 @@ test "basic unions" {
45 assert(foo.float == 12.34);45 assert(foo.float == 12.34);
46}46}
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
48test "init union with runtime value" {58test "init union with runtime value" {
49 var foo: Foo = undefined;59 var foo: Foo = undefined;
5060
test/compile_errors.zig+12
...@@ -3209,4 +3209,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {...@@ -3209,4 +3209,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
3209 \\}3209 \\}
3210 ,3210 ,
3211 ".tmp_source.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset");3211 ".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");
3212}3224}