From 7ef1eb1c27754cb0349fdc10db1f02ff2dddd99b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 Aug 2023 14:25:18 -0700 Subject: InternPool: safer enum API The key changes in this commit are: ```diff - names: []const NullTerminatedString, + names: NullTerminatedString.Slice, - values: []const Index, + values: Index.Slice, ``` Which eliminates the slices from `InternPool.Key.EnumType` and replaces them with structs that contain `start` and `len` indexes. This makes the lifetime of `EnumType` change from expiring with updates to InternPool, to expiring when the InternPool is garbage-collected, which is currently never. This is gearing up for a larger change I started working on locally which moves union types into InternPool. As a bonus, I fixed some unnecessary instances of `@as`. --- src/type.zig | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/type.zig') diff --git a/src/type.zig b/src/type.zig index df39192a63..b82d3723d0 100644 --- a/src/type.zig +++ b/src/type.zig @@ -2434,11 +2434,11 @@ pub const Type = struct { /// resolves field types rather than asserting they are already resolved. pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value { var ty = starting_type; - + const ip = &mod.intern_pool; while (true) switch (ty.toIntern()) { .empty_struct_type => return Value.empty_struct, - else => switch (mod.intern_pool.indexToKey(ty.toIntern())) { + else => switch (ip.indexToKey(ty.toIntern())) { .int_type => |int_type| { if (int_type.bits == 0) { return try mod.intValue(ty, 0); @@ -2619,7 +2619,7 @@ pub const Type = struct { } }); return only.toValue(); } else { - return enum_type.values[0].toValue(); + return enum_type.values.get(ip)[0].toValue(); } }, else => return null, @@ -2967,7 +2967,8 @@ pub const Type = struct { } pub fn enumFields(ty: Type, mod: *Module) []const InternPool.NullTerminatedString { - return mod.intern_pool.indexToKey(ty.toIntern()).enum_type.names; + const ip = &mod.intern_pool; + return ip.indexToKey(ty.toIntern()).enum_type.names.get(ip); } pub fn enumFieldCount(ty: Type, mod: *Module) usize { @@ -2975,7 +2976,8 @@ pub const Type = struct { } pub fn enumFieldName(ty: Type, field_index: usize, mod: *Module) InternPool.NullTerminatedString { - return mod.intern_pool.indexToKey(ty.toIntern()).enum_type.names[field_index]; + const ip = &mod.intern_pool; + return ip.indexToKey(ty.toIntern()).enum_type.names.get(ip)[field_index]; } pub fn enumFieldIndex(ty: Type, field_name: InternPool.NullTerminatedString, mod: *Module) ?u32 { -- cgit v1.2.3