authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-10 20:00:07+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-10 20:00:07+02:00
logf9e4344bb51c70aa9959680f3dfc9d7712858395
tree6c115c0a6c81fed9657d1e6be710fc8f7ee422a2
parent673fafc23142b0bdc467efcb8554cd4e70bba17b

Sema: implement zirStructInit is_ref=true union


3 files changed, 16 insertions(+), 12 deletions(-)

src/Sema.zig+13-6
......@@ -3995,7 +3995,7 @@ pub fn analyzeExport(
39953995 try mod.ensureDeclAnalyzed(exported_decl);
39963996 // TODO run the same checks as we do for C ABI struct fields
39973997 switch (exported_decl.ty.zigTypeTag()) {
3998 .Fn, .Int, .Struct, .Array, .Float => {},
3998 .Fn, .Int, .Enum, .Struct, .Union, .Array, .Float => {},
39993999 else => return sema.fail(block, src, "unable to export type '{}'", .{exported_decl.ty}),
40004000 }
40014001
......@@ -11674,18 +11674,25 @@ fn zirStructInit(
1167411674 const field_name = sema.code.nullTerminatedString(field_type_extra.name_start);
1167511675 const field_index = try sema.unionFieldIndex(block, resolved_ty, field_name, field_src);
1167611676
11677 if (is_ref) {
11678 return sema.fail(block, src, "TODO: Sema.zirStructInit is_ref=true union", .{});
11679 }
11680
1168111677 const init_inst = sema.resolveInst(item.data.init);
1168211678 if (try sema.resolveMaybeUndefVal(block, field_src, init_inst)) |val| {
1168311679 const tag_val = try Value.Tag.enum_field_index.create(sema.arena, field_index);
11684 return sema.addConstant(
11680 return sema.addConstantMaybeRef(
11681 block,
11682 src,
1168511683 resolved_ty,
1168611684 try Value.Tag.@"union".create(sema.arena, .{ .tag = tag_val, .val = val }),
11685 is_ref,
1168711686 );
1168811687 }
11688
11689 if (is_ref) {
11690 const alloc = try block.addTy(.alloc, resolved_ty);
11691 const field_ptr = try sema.unionFieldPtr(block, field_src, alloc, field_name, field_src, resolved_ty);
11692 try sema.storePtr(block, src, field_ptr, init_inst);
11693 return alloc;
11694 }
11695
1168911696 return sema.fail(block, src, "TODO: Sema.zirStructInit for runtime-known union values", .{});
1169011697 }
1169111698 unreachable;
test/behavior.zig+3-3
......@@ -57,6 +57,7 @@ test {
5757 _ = @import("behavior/bugs/5487.zig");
5858 _ = @import("behavior/bugs/6850.zig");
5959 _ = @import("behavior/bugs/7003.zig");
60 _ = @import("behavior/bugs/7047.zig");
6061 _ = @import("behavior/bugs/7250.zig");
6162 _ = @import("behavior/bugs/11100.zig");
6263 _ = @import("behavior/bugs/10970.zig");
......@@ -142,12 +143,14 @@ test {
142143 if (builtin.zig_backend != .stage2_c) {
143144 // Tests that pass for stage1 and the llvm backend.
144145 _ = @import("behavior/atomics.zig");
146 _ = @import("behavior/export.zig");
145147 _ = @import("behavior/maximum_minimum.zig");
146148 _ = @import("behavior/popcount.zig");
147149 _ = @import("behavior/saturating_arithmetic.zig");
148150 _ = @import("behavior/widening.zig");
149151 _ = @import("behavior/bugs/2114.zig");
150152 _ = @import("behavior/bugs/3779.zig");
153 _ = @import("behavior/bugs/10147.zig");
151154 _ = @import("behavior/union_with_members.zig");
152155
153156 if (builtin.zig_backend == .stage1) {
......@@ -164,10 +167,7 @@ test {
164167 _ = @import("behavior/bugs/6456.zig");
165168 _ = @import("behavior/bugs/6781.zig");
166169 _ = @import("behavior/bugs/7027.zig");
167 _ = @import("behavior/bugs/7047.zig");
168 _ = @import("behavior/bugs/10147.zig");
169170 _ = @import("behavior/const_slice_child.zig");
170 _ = @import("behavior/export.zig");
171171 _ = @import("behavior/select.zig");
172172 _ = @import("behavior/shuffle.zig");
173173 _ = @import("behavior/struct_contains_slice_of_itself.zig");
test/behavior/export.zig-3
......@@ -38,9 +38,6 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion) void {
3838test "exporting enum type and value" {
3939 const S = struct {
4040 const E = enum(c_int) { one, two };
41 comptime {
42 @export(E, .{ .name = "E" });
43 }
4441 const e: E = .two;
4542 comptime {
4643 @export(e, .{ .name = "e" });