authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-11 01:02:52-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-11 03:01:26-07:00
log7e5dea6366fa194b54cc391ba48c18754df198e7
treeae3b9a640862d1da6bc65c76c0bc175682d98802
parent0ec012e0c080e1686e270f033ef5aa2d74c7cc9f

Sema: fix `std.builtin.Type.EnumField.value` when not auto-numbered


1 files changed, 14 insertions(+), 8 deletions(-)

src/Sema.zig+14-8
......@@ -16418,9 +16418,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1641816418 },
1641916419 .Enum => {
1642016420 // TODO: look into memoizing this result.
16421 const enum_type = ip.indexToKey(ty.toIntern()).enum_type;
16422
16423 const is_exhaustive = Value.makeBool(enum_type.tag_mode != .nonexhaustive);
16421 const is_exhaustive = Value.makeBool(ip.indexToKey(ty.toIntern()).enum_type.tag_mode != .nonexhaustive);
1642416422
1642516423 var fields_anon_decl = try block.startAnonDecl();
1642616424 defer fields_anon_decl.deinit();
......@@ -16438,10 +16436,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1643816436 break :t enum_field_ty_decl.val.toType();
1643916437 };
1644016438
16441 const enum_field_vals = try sema.arena.alloc(InternPool.Index, enum_type.names.len);
16439 const enum_field_vals = try sema.arena.alloc(InternPool.Index, ip.indexToKey(ty.toIntern()).enum_type.names.len);
1644216440 for (enum_field_vals, 0..) |*field_val, i| {
16441 const enum_type = ip.indexToKey(ty.toIntern()).enum_type;
16442 const value_val = if (enum_type.values.len > 0)
16443 try mod.intern_pool.getCoerced(gpa, enum_type.values[i], .comptime_int_type)
16444 else
16445 try mod.intern(.{ .int = .{
16446 .ty = .comptime_int_type,
16447 .storage = .{ .u64 = @intCast(u64, i) },
16448 } });
1644316449 // TODO: write something like getCoercedInts to avoid needing to dupe
16444 const name = try sema.arena.dupe(u8, ip.stringToSlice(ip.indexToKey(ty.toIntern()).enum_type.names[i]));
16450 const name = try sema.arena.dupe(u8, ip.stringToSlice(enum_type.names[i]));
1644516451 const name_val = v: {
1644616452 var anon_decl = try block.startAnonDecl();
1644716453 defer anon_decl.deinit();
......@@ -16468,7 +16474,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1646816474 // name: []const u8,
1646916475 name_val,
1647016476 // value: comptime_int,
16471 (try mod.intValue(Type.comptime_int, i)).toIntern(),
16477 value_val,
1647216478 };
1647316479 field_val.* = try mod.intern(.{ .aggregate = .{
1647416480 .ty = enum_field_ty.toIntern(),
......@@ -16503,7 +16509,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1650316509 } });
1650416510 };
1650516511
16506 const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, enum_type.namespace);
16512 const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ip.indexToKey(ty.toIntern()).enum_type.namespace);
1650716513
1650816514 const type_enum_ty = t: {
1650916515 const type_enum_ty_decl_index = (try sema.namespaceLookup(
......@@ -16520,7 +16526,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1652016526
1652116527 const field_values = .{
1652216528 // tag_type: type,
16523 enum_type.tag_ty,
16529 ip.indexToKey(ty.toIntern()).enum_type.tag_ty,
1652416530 // fields: []const EnumField,
1652516531 fields_val,
1652616532 // decls: []const Declaration,