Commit ff1c4e1f13

Alexandros Naskos <alex_naskos@hotmail.com>
2018-05-01 12:00:39
Added tests.
1 parent 013f548
Changed files (3)
src/ir.cpp
@@ -15975,7 +15975,6 @@ static void ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
                     ensure_field_index(fn_def_val->type, "return_type", 7);
                     fn_def_fields[7].special = ConstValSpecialStatic;
                     fn_def_fields[7].type = ira->codegen->builtin_types.entry_type;
-                    // @TODO Check whether this is correct.
                     if (fn_entry->src_implicit_return_type != nullptr)
                         fn_def_fields[7].data.x_type = fn_entry->src_implicit_return_type;
                     else if (fn_entry->type_entry->data.fn.gen_return_type != nullptr)
test/cases/union.zig
@@ -45,6 +45,16 @@ test "basic unions" {
     assert(foo.float == 12.34);
 }
 
+test "comptime union field access" {
+    comptime {
+        var foo = Foo { .int = 0 };
+        assert(foo.int == 0);
+
+        foo = Foo { .float = 42.42 };
+        assert(foo.float == 42.42);
+    }
+}
+
 test "init union with runtime value" {
     var foo: Foo = undefined;
 
test/compile_errors.zig
@@ -3209,4 +3209,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
         \\}
     ,
         ".tmp_source.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset");
+
+    cases.add("invalid union field access in comptime",
+        \\const Foo = union {
+        \\    Bar: u8,
+        \\    Baz: void,
+        \\};
+        \\comptime {
+        \\    var foo = Foo {.Baz = {}};    
+        \\    const bar_val = foo.Bar;
+        \\}
+    ,
+        ".tmp_source.zig:7:24: error: accessing union field 'Bar' while field 'Baz' is set");
 }