authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-05 13:12:28+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-06 21:26:38+00:00
log4c05a9a892d68749f3d7da26ee0e884158640720
tree0fca45e4c519f04b7904d5004cc9a42c097ee489
parente1d8187028ec6df7fbf420d3e4b56da8bf3dcf0a
signaturelock-open Commit is signed but in an unrecognized format.

Sema: do not destroy enum type if field analysis fails


2 files changed, 23 insertions(+), 25 deletions(-)

src/InternPool.zig+5-18
......@@ -6810,17 +6810,13 @@ pub const WipEnumType = struct {
68106810 names_start: u32,
68116811 values_map: OptionalMapIndex,
68126812 values_start: u32,
6813 expected_fields_len: if (std.debug.runtime_safety) u32 else void,
68146813
68156814 pub fn prepare(
68166815 wip: WipEnumType,
68176816 ip: *InternPool,
68186817 decl: DeclIndex,
68196818 namespace: OptionalNamespaceIndex,
6820 tag_ty: Index,
68216819 ) void {
6822 assert(ip.isIntegerType(tag_ty));
6823 ip.extra.items[wip.tag_ty_index] = @intFromEnum(tag_ty);
68246820 ip.extra.items[wip.decl_index] = @intFromEnum(decl);
68256821 if (wip.namespace_index) |i| {
68266822 ip.extra.items[i] = @intFromEnum(namespace.unwrap().?);
......@@ -6829,6 +6825,11 @@ pub const WipEnumType = struct {
68296825 }
68306826 }
68316827
6828 pub fn setTagTy(wip: WipEnumType, ip: *InternPool, tag_ty: Index) void {
6829 assert(ip.isIntegerType(tag_ty));
6830 ip.extra.items[wip.tag_ty_index] = @intFromEnum(tag_ty);
6831 }
6832
68326833 pub const FieldConflict = struct {
68336834 kind: enum { name, value },
68346835 prev_field_idx: u32,
......@@ -6858,18 +6859,6 @@ pub const WipEnumType = struct {
68586859 return null;
68596860 }
68606861
6861 pub fn finish(wip: WipEnumType, ip: *InternPool) Index {
6862 if (std.debug.runtime_safety) {
6863 const names_map = &ip.maps.items[@intFromEnum(wip.names_map)];
6864 assert(names_map.count() == wip.expected_fields_len);
6865 if (wip.values_map.unwrap()) |v| {
6866 const values_map = &ip.maps.items[@intFromEnum(v)];
6867 assert(values_map.count() == wip.expected_fields_len);
6868 }
6869 }
6870 return wip.index;
6871 }
6872
68736862 pub fn cancel(wip: WipEnumType, ip: *InternPool) void {
68746863 ip.remove(wip.index);
68756864 }
......@@ -6951,7 +6940,6 @@ pub fn getEnumType(
69516940 .names_start = @intCast(names_start),
69526941 .values_map = .none,
69536942 .values_start = undefined,
6954 .expected_fields_len = if (std.debug.runtime_safety) ini.fields_len else {},
69556943 } };
69566944 },
69576945 .explicit, .nonexhaustive => {
......@@ -7016,7 +7004,6 @@ pub fn getEnumType(
70167004 .names_start = @intCast(names_start),
70177005 .values_map = values_map,
70187006 .values_start = @intCast(values_start),
7019 .expected_fields_len = if (std.debug.runtime_safety) ini.fields_len else {},
70207007 } };
70217008 },
70227009 }
src/Sema.zig+18-7
......@@ -2993,7 +2993,12 @@ fn zirEnumDecl(
29932993 },
29942994 .existing => |ty| return Air.internedToRef(ty),
29952995 };
2996 errdefer wip_ty.cancel(ip);
2996
2997 // Once this is `true`, we will not delete the decl or type even upon failure, since we
2998 // have finished constructing the type and are in the process of analyzing it.
2999 var done = false;
3000
3001 errdefer if (!done) wip_ty.cancel(ip);
29973002
29983003 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
29993004 .ty = Type.type,
......@@ -3001,7 +3006,7 @@ fn zirEnumDecl(
30013006 }, small.name_strategy, "enum", inst);
30023007 const new_decl = mod.declPtr(new_decl_index);
30033008 new_decl.owns_tv = true;
3004 errdefer mod.abortAnonDecl(new_decl_index);
3009 errdefer if (!done) mod.abortAnonDecl(new_decl_index);
30053010
30063011 if (sema.mod.comp.debug_incremental) {
30073012 try mod.intern_pool.addDependency(
......@@ -3017,12 +3022,17 @@ fn zirEnumDecl(
30173022 .decl_index = new_decl_index,
30183023 .file_scope = block.getFileScope(mod),
30193024 })).toOptional() else .none;
3020 errdefer if (new_namespace_index.unwrap()) |ns| mod.destroyNamespace(ns);
3025 errdefer if (!done) if (new_namespace_index.unwrap()) |ns| mod.destroyNamespace(ns);
30213026
30223027 if (new_namespace_index.unwrap()) |ns| {
30233028 try mod.scanNamespace(ns, decls, new_decl);
30243029 }
30253030
3031 // We've finished the initial construction of this type, and are about to perform analysis.
3032 // Set the decl and namespace appropriately, and don't destroy anything on failure.
3033 wip_ty.prepare(ip, new_decl_index, new_namespace_index);
3034 done = true;
3035
30263036 const int_tag_ty = ty: {
30273037 // We create a block for the field type instructions because they
30283038 // may need to reference Decls from inside the enum namespace.
......@@ -3075,7 +3085,7 @@ fn zirEnumDecl(
30753085 }
30763086 };
30773087
3078 wip_ty.prepare(ip, new_decl_index, new_namespace_index, int_tag_ty.toIntern());
3088 wip_ty.setTagTy(ip, int_tag_ty.toIntern());
30793089
30803090 if (small.nonexhaustive and int_tag_ty.toIntern() != .comptime_int_type) {
30813091 if (fields_len > 1 and std.math.log2_int(u64, fields_len) == int_tag_ty.bitSize(mod)) {
......@@ -3177,7 +3187,7 @@ fn zirEnumDecl(
31773187 }
31783188
31793189 try mod.finalizeAnonDecl(new_decl_index);
3180 return Air.internedToRef(wip_ty.finish(ip));
3190 return Air.internedToRef(wip_ty.index);
31813191}
31823192
31833193fn zirUnionDecl(
......@@ -21537,7 +21547,8 @@ fn reifyEnum(
2153721547 mod.declPtr(new_decl_index).owns_tv = true;
2153821548 errdefer mod.abortAnonDecl(new_decl_index);
2153921549
21540 wip_ty.prepare(ip, new_decl_index, .none, tag_ty.toIntern());
21550 wip_ty.prepare(ip, new_decl_index, .none);
21551 wip_ty.setTagTy(ip, tag_ty.toIntern());
2154121552
2154221553 for (0..fields_len) |field_idx| {
2154321554 const field_info = try fields_val.elemValue(mod, field_idx);
......@@ -21582,7 +21593,7 @@ fn reifyEnum(
2158221593 }
2158321594
2158421595 try mod.finalizeAnonDecl(new_decl_index);
21585 return Air.internedToRef(wip_ty.finish(ip));
21596 return Air.internedToRef(wip_ty.index);
2158621597}
2158721598
2158821599fn reifyUnion(