Fixes bug in explicit union tag parsing

This commit is contained in:
Mason Remaley 2024-11-11 16:57:27 -08:00
parent ea92351baa
commit 78735d83a0
2 changed files with 22 additions and 24 deletions

View File

@ -1039,19 +1039,20 @@ fn parseUnion(self: LowerZon, node: Ast.Node.Index, res_ty: Type) !InternPool.In
const name_index = enum_tag_info.nameIndex(ip, field_name_string) orelse {
return self.fail(.{ .node_abs = node }, "expected {}", .{res_ty.fmt(self.sema.pt)});
};
const tag = if (enum_tag_info.values.len == 0) b: {
const tag_int = if (enum_tag_info.values.len == 0) b: {
// Fields are auto numbered
break :b try self.sema.pt.intern(.{ .enum_tag = .{
.ty = union_info.enum_tag_ty,
.int = try self.sema.pt.intern(.{ .int = .{
.ty = enum_tag_info.tag_ty,
.storage = .{ .u64 = name_index },
} }),
break :b try self.sema.pt.intern(.{ .int = .{
.ty = enum_tag_info.tag_ty,
.storage = .{ .u64 = name_index },
} });
} else b: {
// Fields are explicitly numbered
break :b enum_tag_info.values.get(ip)[name_index];
};
const tag = try self.sema.pt.intern(.{ .enum_tag = .{
.ty = union_info.enum_tag_ty,
.int = tag_int,
} });
const field_type = Type.fromInterned(union_info.field_types.get(ip)[name_index]);
const val = try self.parseExpr(field_node, field_type);
return ip.getUnion(self.sema.pt.zcu.gpa, self.sema.pt.tid, .{

View File

@ -53,26 +53,23 @@ test "union" {
try expectEqual(union2.y, true);
}
// Skip because explicit tags aren't yet working as expected
return error.SkipZigTest;
// Explicit tag
// {
// const Tag = enum(i128) {
// x = -1,
// y = 2,
// };
// const Union = union(Tag) {
// x: f32,
// y: bool,
// };
{
const Tag = enum(i128) {
x = -1,
y = 2,
};
const Union = union(Tag) {
x: f32,
y: bool,
};
// const union1: Union = @import("zon/union1.zon");
// const union2: Union = @import("zon/union2.zon");
const union1: Union = @import("zon/union1.zon");
const union2: Union = @import("zon/union2.zon");
// try expectEqual(union1.x, 1.5);
// try expectEqual(union2.y, true);
// }
try expectEqual(union1.x, 1.5);
try expectEqual(union2.y, true);
}
}
test "struct" {