authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-20 20:38:05-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 18:54:36-07:00
log996b4118097c747e65ef1127091f7e48a54bfafc
tree3ed4671d2652c99e823b216d0709b6ad932e5cf5
parent4cbc03dce31b63818da1fb47f959b70708683443

Configuration: more type safety for adding data

erased method still exists for when the result will be converted to an int anyway.

2 files changed, 83 insertions(+), 78 deletions(-)

lib/compiler/configurer.zig+63-68
......@@ -166,11 +166,11 @@ const Serialize = struct {
166166 const wc = s.wc;
167167 const gop = try s.package_map.getOrPut(arena, b);
168168 if (!gop.found_existing) {
169 gop.value_ptr.* = @enumFromInt(try wc.addExtra(@as(Configuration.Package, .{
169 gop.value_ptr.* = try wc.addExtra(Configuration.Package, .{
170170 .hash = try wc.addString(b.pkg_hash),
171171 .dep_prefix = try wc.addString(b.dep_prefix),
172172 .root_path = try wc.addString(try b.root.toString(arena)),
173 })));
173 });
174174 }
175175 return gop.value_ptr.*;
176176 }
......@@ -180,38 +180,38 @@ const Serialize = struct {
180180 return @enumFromInt(switch (lp orelse return .none) {
181181 .src_path => |src_path| i: {
182182 const sub_path = try wc.addString(src_path.sub_path);
183 break :i try wc.addExtra(@as(Configuration.LazyPath.SourcePath, .{
183 break :i try wc.addExtraErased(Configuration.LazyPath.SourcePath, .{
184184 .owner = try s.builderToPackage(src_path.owner),
185185 .sub_path = sub_path,
186 }));
186 });
187187 },
188188 .generated => |generated| i: {
189189 const sub_path = try wc.addString(generated.sub_path);
190 break :i try wc.addExtra(@as(Configuration.LazyPath.Generated, .{
190 break :i try wc.addExtraErased(Configuration.LazyPath.Generated, .{
191191 .flags = .{ .up = @intCast(generated.up) },
192192 .index = generated.index,
193193 .sub_path = sub_path,
194 }));
194 });
195195 },
196196 .cwd_relative => |cwd_relative_sub_path| i: {
197197 const sub_path = try wc.addString(cwd_relative_sub_path);
198 break :i try wc.addExtra(@as(Configuration.LazyPath.Relative, .{
198 break :i try wc.addExtraErased(Configuration.LazyPath.Relative, .{
199199 .flags = .{ .base = .cwd },
200200 .sub_path = sub_path,
201 }));
201 });
202202 },
203203 .relative => |relative| i: {
204 break :i try wc.addExtra(@as(Configuration.LazyPath.Relative, .{
204 break :i try wc.addExtraErased(Configuration.LazyPath.Relative, .{
205205 .flags = .{ .base = relative.base },
206206 .sub_path = relative.sub_path,
207 }));
207 });
208208 },
209209 .dependency => |dependency| i: {
210210 const sub_path = try wc.addString(dependency.sub_path);
211 break :i try wc.addExtra(@as(Configuration.LazyPath.SourcePath, .{
211 break :i try wc.addExtraErased(Configuration.LazyPath.SourcePath, .{
212212 .owner = try s.builderToPackage(dependency.dependency.builder),
213213 .sub_path = sub_path,
214 }));
214 });
215215 },
216216 });
217217 }
......@@ -249,21 +249,21 @@ const Serialize = struct {
249249 fn addCSourceFile(s: *Serialize, csf: *const std.Build.Module.CSourceFile) !Configuration.CSourceFile.Index {
250250 const wc = s.wc;
251251 const args = try initStringList(s, csf.flags);
252 return @enumFromInt(try wc.addExtra(@as(Configuration.CSourceFile, .{
252 return try wc.addExtra(Configuration.CSourceFile, .{
253253 .flags = .{
254254 .args_len = @intCast(args.len),
255255 .lang = .init(csf.language),
256256 },
257257 .file = try addLazyPath(s, csf.file),
258258 .args = .{ .slice = args },
259 })));
259 });
260260 }
261261
262262 fn addCSourceFiles(s: *Serialize, csf: *const std.Build.Module.CSourceFiles) !Configuration.CSourceFiles.Index {
263263 const wc = s.wc;
264264 const sub_paths = try initStringList(s, csf.files);
265265 const args = try initStringList(s, csf.flags);
266 return @enumFromInt(try wc.addExtra(@as(Configuration.CSourceFiles, .{
266 return try wc.addExtra(Configuration.CSourceFiles, .{
267267 .flags = .{
268268 .args_len = @intCast(args.len),
269269 .lang = .init(csf.language),
......@@ -271,14 +271,14 @@ const Serialize = struct {
271271 .root = try addLazyPath(s, csf.root),
272272 .sub_paths = .{ .slice = sub_paths },
273273 .args = .{ .slice = args },
274 })));
274 });
275275 }
276276
277277 fn addRcSourceFile(s: *Serialize, rsf: *const std.Build.Module.RcSourceFile) !Configuration.RcSourceFile.Index {
278278 const wc = s.wc;
279279 const include_paths = try initLazyPathList(s, rsf.include_paths);
280280 const args = try initStringList(s, rsf.flags);
281 return @enumFromInt(try wc.addExtra(@as(Configuration.RcSourceFile, .{
281 return try wc.addExtra(Configuration.RcSourceFile, .{
282282 .flags = .{
283283 .args_len = @intCast(args.len),
284284 .include_paths = include_paths.len != 0,
......@@ -286,7 +286,7 @@ const Serialize = struct {
286286 .file = try addLazyPath(s, rsf.file),
287287 .include_paths = .{ .slice = include_paths },
288288 .args = .{ .slice = args },
289 })));
289 });
290290 }
291291
292292 fn addEnvironMap(s: *Serialize, opt_map: ?*std.process.Environ.Map) !?Configuration.EnvironMap.Index {
......@@ -302,7 +302,7 @@ const Serialize = struct {
302302 const wc = s.wc;
303303 const result = try s.arena.alloc(Configuration.Step.Run.Arg.Index, args.len);
304304 for (result, args) |*dest, src| {
305 dest.* = @enumFromInt(try wc.addExtra(@as(Configuration.Step.Run.Arg, switch (src) {
305 dest.* = try wc.addExtra(Configuration.Step.Run.Arg, switch (src) {
306306 .artifact => |a| .{
307307 .flags = .{
308308 .tag = .artifact,
......@@ -492,7 +492,7 @@ const Serialize = struct {
492492 .generated = .{ .value = null },
493493 .target_query = .{ .value = a.target_query.unwrap() },
494494 },
495 })));
495 });
496496 }
497497 return result;
498498 }
......@@ -580,7 +580,7 @@ const Serialize = struct {
580580 const c_macros = try initStringList(s, m.c_macros.items);
581581 const export_symbol_names = try initStringList(s, m.export_symbol_names);
582582
583 const module_index: Configuration.Module.Index = @enumFromInt(try wc.addExtra(@as(Configuration.Module, .{
583 const module_index: Configuration.Module.Index = try wc.addExtra(Configuration.Module, .{
584584 .flags = .{
585585 .optimize = .init(m.optimize),
586586 .strip = .init(m.strip),
......@@ -622,7 +622,7 @@ const Serialize = struct {
622622 .rpaths = .init(rpaths),
623623 .link_objects = .init(link_objects),
624624 .frameworks = .{ .slice = frameworks },
625 })));
625 });
626626
627627 // The import table is the only place that modules can form dependency
628628 // loops. Therefore, we populate the module indexes only after adding
......@@ -699,12 +699,12 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
699699 .owner = try s.builderToPackage(step.owner),
700700 .deps = deps,
701701 .max_rss = .fromBytes(step.max_rss),
702 .extended = switch (step.tag) {
702 .extended = @enumFromInt(switch (step.tag) {
703703 .top_level => e: {
704704 const top_level: *Step.TopLevel = @fieldParentPtr("step", step);
705 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.TopLevel, .{
705 break :e try wc.addExtraErased(Configuration.Step.TopLevel, .{
706706 .description = try wc.addString(top_level.description),
707 })));
707 });
708708 },
709709 .compile => e: {
710710 const c: *Step.Compile = @fieldParentPtr("step", step);
......@@ -712,14 +712,14 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
712712 const installed_headers: []u32 = try arena.alloc(u32, c.installed_headers.items.len);
713713 for (installed_headers, c.installed_headers.items) |*dst, src| switch (src) {
714714 .file => |file| {
715 dst.* = try wc.addExtra(@as(Configuration.Step.Compile.InstalledHeader.File, .{
715 dst.* = try wc.addExtraErased(Configuration.Step.Compile.InstalledHeader.File, .{
716716 .source = try s.addLazyPath(file.source),
717717 .dest_sub_path = try wc.addString(file.dest_rel_path),
718 }));
718 });
719719 },
720720 .directory => |directory| {
721721 const include_extensions = directory.options.include_extensions orelse &.{};
722 dst.* = try wc.addExtra(@as(Configuration.Step.Compile.InstalledHeader.Directory, .{
722 dst.* = try wc.addExtraErased(Configuration.Step.Compile.InstalledHeader.Directory, .{
723723 .flags = .{
724724 .include_extensions = include_extensions.len != 0,
725725 .exclude_extensions = directory.options.exclude_extensions.len != 0,
......@@ -728,11 +728,11 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
728728 .dest_sub_path = try wc.addString(directory.dest_rel_path),
729729 .exclude_extensions = .{ .slice = try s.initStringList(directory.options.exclude_extensions) },
730730 .include_extensions = .{ .slice = try s.initStringList(include_extensions) },
731 }));
731 });
732732 },
733733 };
734734
735 const extra_index = try wc.addExtra(@as(Configuration.Step.Compile, .{
735 break :e try wc.addExtraErased(Configuration.Step.Compile, .{
736736 .flags = .{
737737 .filters_len = c.filters.len != 0,
738738 .exec_cmd_args_len = exec_cmd_args.len != 0,
......@@ -891,13 +891,11 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
891891 .generated_llvm_bc = .{ .value = c.generated_llvm_bc.unwrap() },
892892 .generated_llvm_ir = .{ .value = c.generated_llvm_ir.unwrap() },
893893 .generated_h = .{ .value = c.generated_h.unwrap() },
894 }));
895
896 break :e @enumFromInt(extra_index);
894 });
897895 },
898896 .install_artifact => e: {
899897 const ia: *Step.InstallArtifact = @fieldParentPtr("step", step);
900 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.InstallArtifact, .{
898 break :e try wc.addExtraErased(Configuration.Step.InstallArtifact, .{
901899 .flags = .{
902900 .dylib_symlinks = ia.dylib_symlinks,
903901 .bin_dir = ia.dest_dir != null,
......@@ -911,15 +909,15 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
911909 .pdb_dir = .{ .value = try addInstallDirDefaultNull(wc, ia.pdb_dir) },
912910 .h_dir = .{ .value = try addInstallDirDefaultNull(wc, ia.h_dir) },
913911 .bin_sub_path = .{ .value = try s.addOptionalString(ia.dest_sub_path) },
914 })));
912 });
915913 },
916914 .install_file => e: {
917915 const sif: *Step.InstallFile = @fieldParentPtr("step", step);
918 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.InstallFile, .{
916 break :e try wc.addExtraErased(Configuration.Step.InstallFile, .{
919917 .source = try s.addLazyPath(sif.source),
920918 .dest_dir = try addInstallDir(wc, sif.dir),
921919 .dest_sub_path = try wc.addString(sif.dest_rel_path),
922 })));
920 });
923921 },
924922 .install_dir => e: {
925923 const sid: *Step.InstallDir = @fieldParentPtr("step", step);
......@@ -928,7 +926,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
928926 else
929927 null;
930928 const include_extensions = sid.options.include_extensions orelse &.{};
931 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.InstallDir, .{
929 break :e try wc.addExtraErased(Configuration.Step.InstallDir, .{
932930 .flags = .{
933931 .dest_sub_path = dest_sub_path != null,
934932 .exclude_extensions = sid.options.exclude_extensions.len != 0,
......@@ -942,18 +940,18 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
942940 .exclude_extensions = .{ .slice = try s.initStringList(sid.options.exclude_extensions) },
943941 .include_extensions = .{ .slice = try s.initStringList(include_extensions) },
944942 .blank_extensions = .{ .slice = try s.initStringList(sid.options.blank_extensions) },
945 })));
943 });
946944 },
947945 .fail => e: {
948946 const sf: *Step.Fail = @fieldParentPtr("step", step);
949 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.Fail, .{
947 break :e try wc.addExtraErased(Configuration.Step.Fail, .{
950948 .msg = sf.error_msg,
951 })));
949 });
952950 },
953951 .find_program => @panic("TODO"),
954952 .fmt => e: {
955953 const sf: *Step.Fmt = @fieldParentPtr("step", step);
956 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.Fmt, .{
954 break :e try wc.addExtraErased(Configuration.Step.Fmt, .{
957955 .flags = .{
958956 .paths = sf.paths.len != 0,
959957 .exclude_paths = sf.exclude_paths.len != 0,
......@@ -961,7 +959,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
961959 },
962960 .paths = .{ .slice = try s.initLazyPathList(sf.paths) },
963961 .exclude_paths = .{ .slice = try s.initLazyPathList(sf.exclude_paths) },
964 })));
962 });
965963 },
966964 .translate_c => e: {
967965 const tc: *Step.TranslateC = @fieldParentPtr("step", step);
......@@ -969,7 +967,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
969967 const system_libs = try arena.alloc(Configuration.SystemLib.Index, tc.system_libs.items.len);
970968 for (system_libs, tc.system_libs.items) |*dest, *src| dest.* = try s.addSystemLib(src);
971969
972 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.TranslateC, .{
970 break :e try wc.addExtraErased(Configuration.Step.TranslateC, .{
973971 .flags = .{
974972 .include_dirs = tc.include_dirs.items.len != 0,
975973 .system_libs = system_libs.len != 0,
......@@ -983,7 +981,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
983981 .system_libs = .{ .slice = system_libs },
984982 .c_macros = .{ .slice = tc.c_macros.items },
985983 .target = try addOptionalResolvedTarget(wc, tc.target),
986 })));
984 });
987985 },
988986 .write_file => e: {
989987 const wf: *Step.WriteFile = @fieldParentPtr("step", step);
......@@ -999,7 +997,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
999997 .include_extensions = src.include_extensions,
1000998 };
1001999
1002 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.WriteFile, .{
1000 break :e try wc.addExtraErased(Configuration.Step.WriteFile, .{
10031001 .flags = .{
10041002 .embeds = wf.embeds.items.len != 0,
10051003 .copies = wf.copies.items.len != 0,
......@@ -1018,18 +1016,18 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
10181016 .mutate => |lp| try s.addLazyPath(lp),
10191017 .whole_cached, .tmp => null,
10201018 } },
1021 })));
1019 });
10221020 },
10231021 .update_source_files => e: {
10241022 const usf: *Step.UpdateSourceFiles = @fieldParentPtr("step", step);
1025 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.UpdateSourceFiles, .{
1023 break :e try wc.addExtraErased(Configuration.Step.UpdateSourceFiles, .{
10261024 .flags = .{
10271025 .embeds = usf.embeds.items.len != 0,
10281026 .copies = usf.copies.items.len != 0,
10291027 },
10301028 .embeds = .{ .slice = usf.embeds.items },
10311029 .copies = .{ .slice = try s.initCopyList(usf.copies.items) },
1032 })));
1030 });
10331031 },
10341032 .run => e: {
10351033 const run: *Step.Run = @fieldParentPtr("step", step);
......@@ -1061,7 +1059,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
10611059 else => {},
10621060 }
10631061
1064 const extra_index = try wc.addExtra(@as(Configuration.Step.Run, .{
1062 break :e try wc.addExtraErased(Configuration.Step.Run, .{
10651063 .flags = .{
10661064 .disable_zig_progress = run.disable_zig_progress,
10671065 .skip_foreign_checks = run.skip_foreign_checks,
......@@ -1121,12 +1119,11 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
11211119 .bytes => |bytes| .{ .bytes = try wc.addBytes(bytes) },
11221120 .lazy_path => |lp| .{ .lazy_path = try s.addLazyPath(lp) },
11231121 } },
1124 }));
1125 break :e @enumFromInt(extra_index);
1122 });
11261123 },
11271124 .check_file => e: {
11281125 const cf: *Step.CheckFile = @fieldParentPtr("step", step);
1129 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.CheckFile, .{
1126 break :e try wc.addExtraErased(Configuration.Step.CheckFile, .{
11301127 .flags = .{
11311128 .expected_exact = cf.expected_exact != null,
11321129 .expected_matches = cf.expected_matches.len != 0,
......@@ -1136,7 +1133,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
11361133 .expected_exact = .{ .value = cf.expected_exact },
11371134 .expected_matches = .{ .slice = cf.expected_matches },
11381135 .max_bytes = .{ .value = cf.max_bytes },
1139 })));
1136 });
11401137 },
11411138 .config_header => e: {
11421139 const ch: *Step.ConfigHeader = @fieldParentPtr("step", step);
......@@ -1154,11 +1151,9 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
11541151 .int => |x| switch (x) {
11551152 0 => .int_0,
11561153 1 => .int_1,
1157 else => @enumFromInt(try wc.addExtra(
1158 Configuration.Step.ConfigHeader.Value.initSigned(x),
1159 )),
1154 else => try wc.addExtra(Configuration.Step.ConfigHeader.Value, .initSigned(x)),
11601155 },
1161 .ident => |x| @enumFromInt(try wc.addExtra(@as(Configuration.Step.ConfigHeader.Value, .{
1156 .ident => |x| try wc.addExtra(Configuration.Step.ConfigHeader.Value, .{
11621157 .flags = .{
11631158 .tag = .ident,
11641159 .small = 0,
......@@ -1167,8 +1162,8 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
11671162 .u64 = .{ .value = null },
11681163 .ident = .{ .value = try wc.addString(x) },
11691164 .string = .{ .value = null },
1170 }))),
1171 .string => |x| @enumFromInt(try wc.addExtra(@as(Configuration.Step.ConfigHeader.Value, .{
1165 }),
1166 .string => |x| try wc.addExtra(Configuration.Step.ConfigHeader.Value, .{
11721167 .flags = .{
11731168 .tag = .string,
11741169 .small = 0,
......@@ -1177,10 +1172,10 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
11771172 .u64 = .{ .value = null },
11781173 .ident = .{ .value = null },
11791174 .string = .{ .value = try wc.addString(x) },
1180 }))),
1175 }),
11811176 },
11821177 };
1183 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.ConfigHeader, .{
1178 break :e try wc.addExtraErased(Configuration.Step.ConfigHeader, .{
11841179 .flags = .{
11851180 .template_file = lazy_path != null,
11861181 .style = .init(ch.style),
......@@ -1193,7 +1188,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
11931188 .include_path = try wc.addString(ch.include_path),
11941189 .include_guard = .{ .value = ch.include_guard.unwrap() },
11951190 .values = .{ .slice = pairs },
1196 })));
1191 });
11971192 },
11981193 .obj_copy => e: {
11991194 const oc: *Step.ObjCopy = @fieldParentPtr("step", step);
......@@ -1217,7 +1212,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
12171212 .file_path = try s.addLazyPath(src.file_path),
12181213 };
12191214
1220 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.ObjCopy, .{
1215 break :e try wc.addExtraErased(Configuration.Step.ObjCopy, .{
12211216 .flags = .{
12221217 .basename = oc.basename != .none,
12231218 .debug_file = debug_file != null,
......@@ -1239,7 +1234,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
12391234 .pad_to = .{ .value = oc.pad_to },
12401235 .add_section = .{ .slice = add_sections },
12411236 .update_section = .{ .slice = oc.update_sections.items },
1242 })));
1237 });
12431238 },
12441239 .options => e: {
12451240 const so: *Step.Options = @fieldParentPtr("step", step);
......@@ -1250,16 +1245,16 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
12501245 .path = try s.addLazyPath(src.path),
12511246 };
12521247
1253 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.Options, .{
1248 break :e try wc.addExtraErased(Configuration.Step.Options, .{
12541249 .flags = .{
12551250 .args = so.args.items.len != 0,
12561251 },
12571252 .generated_file = so.generated_file,
12581253 .contents = try wc.addBytes(so.contents.items),
12591254 .args = .{ .slice = args },
1260 })));
1255 });
12611256 },
1262 },
1257 }),
12631258 });
12641259 }
12651260 }
lib/std/Build/Configuration.zig+20-10
......@@ -254,7 +254,7 @@ pub const Wip = struct {
254254 null;
255255 const cpu_features_add_empty = q.cpu_features_add.isEmpty();
256256 const cpu_features_sub_empty = q.cpu_features_sub.isEmpty();
257 const result_index: TargetQuery.Index = @enumFromInt(try wip.addExtra(@as(TargetQuery, .{
257 const result_index: TargetQuery.Index = try wip.addExtra(TargetQuery, .{
258258 .flags = .{
259259 .cpu_arch = .init(q.cpu_arch),
260260 .cpu_model = .init(q.cpu_model),
......@@ -277,7 +277,7 @@ pub const Wip = struct {
277277 .cpu_name = .{ .value = cpu_name },
278278 .os_version_min = .{ .u = os_version_min },
279279 .os_version_max = .{ .u = os_version_max },
280 })));
280 });
281281
282282 // Deduplicate.
283283 const gop = try wip.targets_table.getOrPutContext(gpa, result_index, @as(TargetsTableContext, .{
......@@ -329,7 +329,7 @@ pub const Wip = struct {
329329 };
330330 const dynamic_linker: ?String = if (t.dynamic_linker.get()) |dl| try wip.addString(dl) else null;
331331 const cpu_features_add_empty = t.cpu.features.isEmpty();
332 const result_index: TargetQuery.Index = @enumFromInt(try wip.addExtra(@as(TargetQuery, .{
332 const result_index = try wip.addExtra(TargetQuery, .{
333333 .flags = .{
334334 .cpu_arch = .init(t.cpu.arch),
335335 .cpu_model = .explicit,
......@@ -352,7 +352,7 @@ pub const Wip = struct {
352352 .cpu_name = .{ .value = cpu_name },
353353 .os_version_min = .{ .u = os_version_min },
354354 .os_version_max = .{ .u = os_version_max },
355 })));
355 });
356356
357357 // Deduplicate.
358358 const gop = try wip.targets_table.getOrPutContext(gpa, result_index, @as(TargetsTableContext, .{
......@@ -366,10 +366,16 @@ pub const Wip = struct {
366366 }
367367 }
368368
369 pub fn addExtra(wip: *Wip, extra: anytype) Allocator.Error!u32 {
370 const extra_len = Storage.extraLen(extra);
369 pub fn addExtra(wip: *Wip, comptime T: type, v: T) Allocator.Error!T.Index {
370 const extra_len = Storage.extraLen(v);
371371 try wip.extra.ensureUnusedCapacity(wip.gpa, extra_len);
372 return addExtraAssumeCapacity(wip, extra);
372 return addExtraReserved(wip, T, v);
373 }
374
375 pub fn addExtraErased(wip: *Wip, comptime T: type, v: T) Allocator.Error!u32 {
376 const extra_len = Storage.extraLen(v);
377 try wip.extra.ensureUnusedCapacity(wip.gpa, extra_len);
378 return addExtraReservedErased(wip, T, v);
373379 }
374380
375381 /// Same as `addExtra` but uses a hash map to possibly return an already
......@@ -382,7 +388,7 @@ pub const Wip = struct {
382388 try wip.dedupe_table.ensureUnusedCapacityContext(gpa, 1, @as(ExtraSlice.Context, .{
383389 .extra = wip.extra.items,
384390 }));
385 const new_index = addExtraAssumeCapacity(wip, v);
391 const new_index = addExtraReservedErased(wip, T, v);
386392 const len: u32 = @intCast(wip.extra.items.len - new_index);
387393 assert(len != 0);
388394 const gop = wip.dedupe_table.getOrPutAssumeCapacityContext(.{
......@@ -398,9 +404,13 @@ pub const Wip = struct {
398404 return @enumFromInt(new_index);
399405 }
400406
401 pub fn addExtraAssumeCapacity(wip: *Wip, extra: anytype) u32 {
407 pub fn addExtraReserved(wip: *Wip, comptime T: type, v: T) T.Index {
408 return @enumFromInt(addExtraReservedErased(wip, T, v));
409 }
410
411 pub fn addExtraReservedErased(wip: *Wip, comptime T: type, v: T) u32 {
402412 const result: u32 = @intCast(wip.extra.items.len);
403 wip.extra.items.len = Storage.setExtra(wip.extra.allocatedSlice(), result, extra);
413 wip.extra.items.len = Storage.setExtra(wip.extra.allocatedSlice(), result, v);
404414 return result;
405415 }
406416