diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2023-06-11 01:02:52 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-06-11 03:01:26 -0700 |
| commit | 7e5dea6366fa194b54cc391ba48c18754df198e7 (patch) | |
| tree | ae3b9a640862d1da6bc65c76c0bc175682d98802 /src/Sema.zig | |
| parent | 0ec012e0c080e1686e270f033ef5aa2d74c7cc9f (diff) | |
| download | zig-7e5dea6366fa194b54cc391ba48c18754df198e7.tar.gz zig-7e5dea6366fa194b54cc391ba48c18754df198e7.zip | |
Sema: fix `std.builtin.Type.EnumField.value` when not auto-numbered
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 99b6c1dba6..5cec8c19f9 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -16418,9 +16418,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai }, .Enum => { // TODO: look into memoizing this result. - const enum_type = ip.indexToKey(ty.toIntern()).enum_type; - - const is_exhaustive = Value.makeBool(enum_type.tag_mode != .nonexhaustive); + const is_exhaustive = Value.makeBool(ip.indexToKey(ty.toIntern()).enum_type.tag_mode != .nonexhaustive); var fields_anon_decl = try block.startAnonDecl(); defer fields_anon_decl.deinit(); @@ -16438,10 +16436,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai break :t enum_field_ty_decl.val.toType(); }; - const enum_field_vals = try sema.arena.alloc(InternPool.Index, enum_type.names.len); + const enum_field_vals = try sema.arena.alloc(InternPool.Index, ip.indexToKey(ty.toIntern()).enum_type.names.len); for (enum_field_vals, 0..) |*field_val, i| { + const enum_type = ip.indexToKey(ty.toIntern()).enum_type; + const value_val = if (enum_type.values.len > 0) + try mod.intern_pool.getCoerced(gpa, enum_type.values[i], .comptime_int_type) + else + try mod.intern(.{ .int = .{ + .ty = .comptime_int_type, + .storage = .{ .u64 = @intCast(u64, i) }, + } }); // TODO: write something like getCoercedInts to avoid needing to dupe - const name = try sema.arena.dupe(u8, ip.stringToSlice(ip.indexToKey(ty.toIntern()).enum_type.names[i])); + const name = try sema.arena.dupe(u8, ip.stringToSlice(enum_type.names[i])); const name_val = v: { var anon_decl = try block.startAnonDecl(); defer anon_decl.deinit(); @@ -16468,7 +16474,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai // name: []const u8, name_val, // value: comptime_int, - (try mod.intValue(Type.comptime_int, i)).toIntern(), + value_val, }; field_val.* = try mod.intern(.{ .aggregate = .{ .ty = enum_field_ty.toIntern(), @@ -16503,7 +16509,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai } }); }; - const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, enum_type.namespace); + const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ip.indexToKey(ty.toIntern()).enum_type.namespace); const type_enum_ty = t: { 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 const field_values = .{ // tag_type: type, - enum_type.tag_ty, + ip.indexToKey(ty.toIntern()).enum_type.tag_ty, // fields: []const EnumField, fields_val, // decls: []const Declaration, |
