authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-02-02 13:32:06+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-10 10:26:09+00:00
loga9bfc94ee65f1dafe5c67bb8fbd58cd2372cfd28
tree83a4587f67d719c3eb262177984029d0ba62735f
parentb8997f871fc63cee28d94168330d84f177543f2c
signaturelock-open Commit is signed but in an unrecognized format.

compiler: small misc cleanups


4 files changed, 94 insertions(+), 123 deletions(-)

lib/std/zig.zig+2-2
...@@ -868,7 +868,7 @@ pub const SimpleComptimeReason = enum(u32) {...@@ -868,7 +868,7 @@ pub const SimpleComptimeReason = enum(u32) {
868 casted_to_comptime_enum,868 casted_to_comptime_enum,
869 casted_to_comptime_int,869 casted_to_comptime_int,
870 casted_to_comptime_float,870 casted_to_comptime_float,
871 panic_handler,871 std_builtin_decl,
872872
873 pub fn message(r: SimpleComptimeReason) []const u8 {873 pub fn message(r: SimpleComptimeReason) []const u8 {
874 return switch (r) {874 return switch (r) {
...@@ -957,7 +957,7 @@ pub const SimpleComptimeReason = enum(u32) {...@@ -957,7 +957,7 @@ pub const SimpleComptimeReason = enum(u32) {
957 .casted_to_comptime_enum => "value casted to enum with 'comptime_int' tag type must be comptime-known",957 .casted_to_comptime_enum => "value casted to enum with 'comptime_int' tag type must be comptime-known",
958 .casted_to_comptime_int => "value casted to 'comptime_int' must be comptime-known",958 .casted_to_comptime_int => "value casted to 'comptime_int' must be comptime-known",
959 .casted_to_comptime_float => "value casted to 'comptime_float' must be comptime-known",959 .casted_to_comptime_float => "value casted to 'comptime_float' must be comptime-known",
960 .panic_handler => "panic handler must be comptime-known",960 .std_builtin_decl => "'std.builtin' declaration values must be comptime-known",
961 // zig fmt: on961 // zig fmt: on
962 };962 };
963 }963 }
src/Compilation.zig+12-11
...@@ -986,7 +986,9 @@ const Job = union(enum) {...@@ -986,7 +986,9 @@ const Job = union(enum) {
986 /// If the unit is a *test* function, an `analyze_func` job will also be queued.986 /// If the unit is a *test* function, an `analyze_func` job will also be queued.
987 analyze_unit: InternPool.AnalUnit,987 analyze_unit: InternPool.AnalUnit,
988 /// The main source file for the module needs to be analyzed.988 /// The main source file for the module needs to be analyzed.
989 analyze_mod: *Package.Module,989 /// For every module which is an analysis root, analyze the main struct type of the module's
990 /// root source file. This is how semantic analysis begins.
991 analyze_roots,
990992
991 /// The value is the index into `windows_libs`.993 /// The value is the index into `windows_libs`.
992 windows_import_lib: usize,994 windows_import_lib: usize,
...@@ -1396,7 +1398,6 @@ pub const MiscTask = enum {...@@ -1396,7 +1398,6 @@ pub const MiscTask = enum {
1396 wasi_libc_crt_file,1398 wasi_libc_crt_file,
1397 compiler_rt,1399 compiler_rt,
1398 libzigc,1400 libzigc,
1399 analyze_mod,
1400 link_depfile,1401 link_depfile,
1401 docs_copy,1402 docs_copy,
1402 docs_wasm,1403 docs_wasm,
...@@ -4840,9 +4841,7 @@ fn performAllTheWork(...@@ -4840,9 +4841,7 @@ fn performAllTheWork(
4840 try zcu.flushRetryableFailures();4841 try zcu.flushRetryableFailures();
48414842
4842 // It's analysis time! Queue up our initial analysis.4843 // It's analysis time! Queue up our initial analysis.
4843 for (zcu.analysisRoots()) |mod| {4844 try comp.queueJob(.analyze_roots);
4844 try comp.queueJob(.{ .analyze_mod = mod });
4845 }
48464845
4847 zcu.sema_prog_node = main_progress_node.start("Semantic Analysis", 0);4846 zcu.sema_prog_node = main_progress_node.start("Semantic Analysis", 0);
4848 if (comp.bin_file != null) {4847 if (comp.bin_file != null) {
...@@ -5275,15 +5274,17 @@ fn processOneJob(tid: Zcu.PerThread.Id, comp: *Compilation, job: Job) JobError!v...@@ -5275,15 +5274,17 @@ fn processOneJob(tid: Zcu.PerThread.Id, comp: *Compilation, job: Job) JobError!v
5275 try pt.zcu.ensureFuncBodyAnalysisQueued(ip.getNav(nav).status.fully_resolved.val);5274 try pt.zcu.ensureFuncBodyAnalysisQueued(ip.getNav(nav).status.fully_resolved.val);
5276 }5275 }
5277 },5276 },
5278 .analyze_mod => |mod| {5277 .analyze_roots => {
5279 const tracy_trace = traceNamed(@src(), "analyze_mod");5278 const tracy_trace = traceNamed(@src(), "analyze_roots");
5280 defer tracy_trace.end();5279 defer tracy_trace.end();
52815280
5282 const pt: Zcu.PerThread = .activate(comp.zcu.?, tid);5281 const zcu = comp.zcu.?;
5282 const pt: Zcu.PerThread = .activate(zcu, tid);
5283 defer pt.deactivate();5283 defer pt.deactivate();
52845284 for (zcu.analysisRoots()) |analysis_root_mod| {
5285 const mod_root_file = pt.zcu.module_roots.get(mod).?.unwrap().?;5285 const analysis_root_file = zcu.module_roots.get(analysis_root_mod).?.unwrap().?;
5286 try pt.ensureFileAnalyzed(mod_root_file);5286 try pt.ensureFileAnalyzed(analysis_root_file);
5287 }
5287 },5288 },
5288 .windows_import_lib => |index| {5289 .windows_import_lib => |index| {
5289 const tracy_trace = traceNamed(@src(), "windows_import_lib");5290 const tracy_trace = traceNamed(@src(), "windows_import_lib");
src/Sema.zig+76-64
...@@ -2308,6 +2308,7 @@ pub fn resolveConstValue(...@@ -2308,6 +2308,7 @@ pub fn resolveConstValue(
2308 /// being comptime-resolved is that the block is being comptime-evaluated.2308 /// being comptime-resolved is that the block is being comptime-evaluated.
2309 reason: ?ComptimeReason,2309 reason: ?ComptimeReason,
2310) CompileError!Value {2310) CompileError!Value {
2311 assert(reason != null or block.isComptime());
2311 return sema.resolveValue(inst) orelse {2312 return sema.resolveValue(inst) orelse {
2312 return sema.failWithNeededComptime(block, src, reason);2313 return sema.failWithNeededComptime(block, src, reason);
2313 };2314 };
...@@ -12927,6 +12928,8 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air....@@ -12927,6 +12928,8 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
12927 try pt.ensureFileAnalyzed(file_index);12928 try pt.ensureFileAnalyzed(file_index);
12928 const ty: Type = .fromInterned(zcu.fileRootType(file_index));12929 const ty: Type = .fromInterned(zcu.fileRootType(file_index));
12929 try sema.addTypeReferenceEntry(operand_src, ty);12930 try sema.addTypeReferenceEntry(operand_src, ty);
12931 // No need for `ensureNamespaceUpToDate`, because `Zcu.PerThread.updateFileNamespace`
12932 // already made sure that all root file structs have up-to-date namespaces.
12930 return .fromType(ty);12933 return .fromType(ty);
12931 },12934 },
12932 .zon => {12935 .zon => {
...@@ -16342,7 +16345,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16342,7 +16345,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16342 const alignment_val = try pt.intValue(.comptime_int, bytes: {16345 const alignment_val = try pt.intValue(.comptime_int, bytes: {
16343 if (info.flags.alignment.toByteUnits()) |b| break :bytes b;16346 if (info.flags.alignment.toByteUnits()) |b| break :bytes b;
16344 const elem_ty: Type = .fromInterned(info.child);16347 const elem_ty: Type = .fromInterned(info.child);
16345 // MLUGG TODO: this resolution is sus, but i doubt i'll solve it in this branch
16346 try sema.ensureLayoutResolved(elem_ty, src);16348 try sema.ensureLayoutResolved(elem_ty, src);
16347 break :bytes elem_ty.abiAlignment(zcu).toByteUnits().?;16349 break :bytes elem_ty.abiAlignment(zcu).toByteUnits().?;
16348 });16350 });
...@@ -18942,12 +18944,13 @@ fn structInitAnon(...@@ -18942,12 +18944,13 @@ fn structInitAnon(
18942 .file_scope = block.getFileScopeIndex(zcu),18944 .file_scope = block.getFileScopeIndex(zcu),
18943 .generation = zcu.generation,18945 .generation = zcu.generation,
18944 });18946 });
18947 errdefer pt.destroyNamespace(new_namespace_index);
18945 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);18948 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);
18946
18947 break :ty .fromInterned(wip.finish(ip, new_namespace_index));18949 break :ty .fromInterned(wip.finish(ip, new_namespace_index));
18948 },18950 },
18949 };18951 };
18950 try sema.addTypeReferenceEntry(src, struct_ty);18952 try sema.addTypeReferenceEntry(src, struct_ty);
18953 // No need for `ensureNamespaceUpToDate` because this type's namespace is always empty.
18951 try sema.ensureLayoutResolved(struct_ty, src);18954 try sema.ensureLayoutResolved(struct_ty, src);
1895218955
18953 _ = opt_runtime_index orelse {18956 _ = opt_runtime_index orelse {
...@@ -20150,6 +20153,7 @@ fn zirReifyStruct(...@@ -20150,6 +20153,7 @@ fn zirReifyStruct(
20150 })) {20153 })) {
20151 .existing => |ty| {20154 .existing => |ty| {
20152 try sema.addTypeReferenceEntry(src, .fromInterned(ty));20155 try sema.addTypeReferenceEntry(src, .fromInterned(ty));
20156 // No need for `ensureNamespaceUpToDate` because this type's namespace is always empty.
20153 return .fromIntern(ty);20157 return .fromIntern(ty);
20154 },20158 },
20155 .wip => |wip| {20159 .wip => |wip| {
...@@ -20210,9 +20214,9 @@ fn zirReifyStruct(...@@ -20210,9 +20214,9 @@ fn zirReifyStruct(
20210 .file_scope = block.getFileScopeIndex(zcu),20214 .file_scope = block.getFileScopeIndex(zcu),
20211 .generation = zcu.generation,20215 .generation = zcu.generation,
20212 });20216 });
20213 try sema.addTypeReferenceEntry(src, .fromInterned(wip.index));20217 errdefer pt.destroyNamespace(new_namespace_index);
20214 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);20218 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);
2021520219 try sema.addTypeReferenceEntry(src, .fromInterned(wip.index));
20216 return .fromIntern(wip.finish(ip, new_namespace_index));20220 return .fromIntern(wip.finish(ip, new_namespace_index));
20217 },20221 },
20218 }20222 }
...@@ -20393,6 +20397,7 @@ fn zirReifyUnion(...@@ -20393,6 +20397,7 @@ fn zirReifyUnion(
20393 })) {20397 })) {
20394 .existing => |ty| {20398 .existing => |ty| {
20395 try sema.addTypeReferenceEntry(src, .fromInterned(ty));20399 try sema.addTypeReferenceEntry(src, .fromInterned(ty));
20400 // No need for `ensureNamespaceUpToDate` because this type's namespace is always empty.
20396 return .fromIntern(ty);20401 return .fromIntern(ty);
20397 },20402 },
20398 .wip => |wip| {20403 .wip => |wip| {
...@@ -20430,9 +20435,9 @@ fn zirReifyUnion(...@@ -20430,9 +20435,9 @@ fn zirReifyUnion(
20430 .file_scope = block.getFileScopeIndex(zcu),20435 .file_scope = block.getFileScopeIndex(zcu),
20431 .generation = zcu.generation,20436 .generation = zcu.generation,
20432 });20437 });
20438 errdefer pt.destroyNamespace(new_namespace_index);
20433 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);20439 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);
20434 try sema.addTypeReferenceEntry(src, .fromInterned(wip.index));20440 try sema.addTypeReferenceEntry(src, .fromInterned(wip.index));
20435
20436 return .fromIntern(wip.finish(ip, new_namespace_index));20441 return .fromIntern(wip.finish(ip, new_namespace_index));
20437 },20442 },
20438 }20443 }
...@@ -20557,6 +20562,7 @@ fn zirReifyEnum(...@@ -20557,6 +20562,7 @@ fn zirReifyEnum(
20557 })) {20562 })) {
20558 .existing => |ty| {20563 .existing => |ty| {
20559 try sema.addTypeReferenceEntry(src, .fromInterned(ty));20564 try sema.addTypeReferenceEntry(src, .fromInterned(ty));
20565 // No need for `ensureNamespaceUpToDate` because this type's namespace is always empty.
20560 return .fromIntern(ty);20566 return .fromIntern(ty);
20561 },20567 },
20562 .wip => |wip| {20568 .wip => |wip| {
...@@ -20581,10 +20587,9 @@ fn zirReifyEnum(...@@ -20581,10 +20587,9 @@ fn zirReifyEnum(
20581 .file_scope = block.getFileScopeIndex(zcu),20587 .file_scope = block.getFileScopeIndex(zcu),
20582 .generation = zcu.generation,20588 .generation = zcu.generation,
20583 });20589 });
2058420590 errdefer pt.destroyNamespace(new_namespace_index);
20585 try sema.addTypeReferenceEntry(src, .fromInterned(wip.index));
20586 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);20591 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);
2058720592 try sema.addTypeReferenceEntry(src, .fromInterned(wip.index));
20588 return .fromIntern(wip.finish(ip, new_namespace_index));20593 return .fromIntern(wip.finish(ip, new_namespace_index));
20589 },20594 },
20590 }20595 }
...@@ -33861,7 +33866,7 @@ pub fn resolveNavPtrModifiers(...@@ -33861,7 +33866,7 @@ pub fn resolveNavPtrModifiers(
33861 };33866 };
33862}33867}
3386333868
33864pub fn analyzeMemoizedState(sema: *Sema, block: *Block, simple_src: LazySrcLoc, builtin_namespace: InternPool.NamespaceIndex, stage: InternPool.MemoizedStateStage) CompileError!bool {33869pub fn analyzeMemoizedState(sema: *Sema, stage: InternPool.MemoizedStateStage) CompileError!bool {
33865 const pt = sema.pt;33870 const pt = sema.pt;
33866 const zcu = pt.zcu;33871 const zcu = pt.zcu;
33867 const comp = zcu.comp;33872 const comp = zcu.comp;
...@@ -33869,53 +33874,87 @@ pub fn analyzeMemoizedState(sema: *Sema, block: *Block, simple_src: LazySrcLoc,...@@ -33869,53 +33874,87 @@ pub fn analyzeMemoizedState(sema: *Sema, block: *Block, simple_src: LazySrcLoc,
33869 const io = comp.io;33874 const io = comp.io;
33870 const ip = &zcu.intern_pool;33875 const ip = &zcu.intern_pool;
3387133876
33877 // This `Block` acts kind of like it's evaluating a `comptime` declaration in the root source
33878 // file of the standard library. In particular, its namespace is the root std namespace.
33879 var block: Block = block: {
33880 // Get the main struct type of the root source file of `std`. No need for a reference entry
33881 // because `std` is always an analysis root.
33882 const std_file_index = zcu.module_roots.get(zcu.std_mod).?.unwrap().?;
33883 try pt.ensureFileAnalyzed(std_file_index);
33884 const std_type: Type = .fromInterned(zcu.fileRootType(std_file_index));
33885 break :block .{
33886 .parent = null,
33887 .sema = sema,
33888 .namespace = std_type.getNamespaceIndex(zcu),
33889 .instructions = .empty,
33890 .inlining = null,
33891 .comptime_reason = null,
33892 .src_base_inst = std_type.typeDeclInst(zcu).?,
33893 .type_name_ctx = .empty,
33894 };
33895 };
33896 defer block.instructions.deinit(gpa);
33897
33898 const std_builtin_ty: Type = ty: {
33899 const std_src = block.nodeOffset(.zero);
33900 const decl_name = try ip.getOrPutString(gpa, io, pt.tid, "builtin", .no_embedded_nulls);
33901 const nav = try sema.namespaceLookup(&block, std_src, block.namespace, decl_name) orelse {
33902 return sema.fail(&block, std_src, "'std' missing 'builtin'", .{});
33903 };
33904 const uncoerced_val = try sema.analyzeNavVal(&block, std_src, nav);
33905 const decl_src: LazySrcLoc = .{
33906 .base_node_inst = ip.getNav(nav).srcInst(ip),
33907 .offset = .nodeOffset(.zero),
33908 };
33909 break :ty try sema.analyzeAsType(&block, decl_src, .std_builtin_decl, uncoerced_val);
33910 };
33911
33872 var any_changed = false;33912 var any_changed = false;
3387333913
33874 inline for (comptime std.enums.values(Zcu.BuiltinDecl)) |builtin_decl| {33914 inline for (comptime std.enums.values(Zcu.BuiltinDecl)) |builtin_decl| {
33875 if (stage == comptime builtin_decl.stage()) {33915 if (stage == comptime builtin_decl.stage()) {
33876 const parent_ns: Zcu.Namespace.Index, const parent_name: []const u8, const name: []const u8 = switch (comptime builtin_decl.access()) {33916 const parent_ns_ty: Type, const parent_name: []const u8, const name: []const u8 = switch (comptime builtin_decl.access()) {
33877 .direct => |name| .{ builtin_namespace, "std.builtin", name },33917 .direct => |name| .{ std_builtin_ty, "std.builtin", name },
33878 .nested => |nested| access: {33918 .nested => |nested| access: {
33879 const parent_ty: Type = .fromInterned(zcu.builtin_decl_values.get(nested[0]));33919 const parent_decl, const name = nested;
33880 const parent_ns = parent_ty.getNamespace(zcu).unwrap() orelse {33920 const parent_ty: Type = .fromInterned(zcu.builtin_decl_values.get(parent_decl));
33881 return sema.fail(block, simple_src, "std.builtin.{s} is not a container type", .{@tagName(nested[0])});33921 break :access .{ parent_ty, "std.builtin." ++ @tagName(parent_decl), name };
33882 };
33883 break :access .{ parent_ns, "std.builtin." ++ @tagName(nested[0]), nested[1] };
33884 },33922 },
33885 };33923 };
3388633924
33925 const parent_ns = parent_ns_ty.getNamespace(zcu).unwrap() orelse {
33926 return sema.fail(&block, block.nodeOffset(.zero), "'{s}' is not a container type", .{parent_name});
33927 };
33928 const parent_ty_src = parent_ns_ty.srcLoc(zcu);
33887 const name_nts = try ip.getOrPutString(gpa, io, pt.tid, name, .no_embedded_nulls);33929 const name_nts = try ip.getOrPutString(gpa, io, pt.tid, name, .no_embedded_nulls);
33888 const nav = try sema.namespaceLookup(block, simple_src, parent_ns, name_nts) orelse33930 const nav = try sema.namespaceLookup(&block, parent_ty_src, parent_ns, name_nts) orelse {
33889 return sema.fail(block, simple_src, "{s} missing {s}", .{ parent_name, name });33931 return sema.fail(&block, parent_ty_src, "'{s}' missing '{s}'", .{ parent_name, name });
33932 };
33933 const uncoerced_val = try sema.analyzeNavVal(&block, parent_ty_src, nav);
3389033934
33891 const src: LazySrcLoc = .{33935 const decl_src: LazySrcLoc = .{
33892 .base_node_inst = ip.getNav(nav).srcInst(ip),33936 .base_node_inst = ip.getNav(nav).srcInst(ip),
33893 .offset = .nodeOffset(.zero),33937 .offset = .nodeOffset(.zero),
33894 };33938 };
3389533939
33896 const result = try sema.analyzeNavVal(block, src, nav);
33897
33898 const uncoerced_val = try sema.resolveConstDefinedValue(block, src, result, null);
33899 const val: Value = switch (builtin_decl.kind()) {33940 const val: Value = switch (builtin_decl.kind()) {
33900 .type => if (uncoerced_val.typeOf(zcu).zigTypeTag(zcu) != .type) {33941 .type => val: {
33901 return sema.fail(block, src, "{s}.{s} is not a type", .{ parent_name, name });33942 const ty = try sema.analyzeAsType(&block, decl_src, .std_builtin_decl, uncoerced_val);
33902 } else val: {33943 try sema.ensureLayoutResolved(ty, decl_src);
33903 try sema.ensureLayoutResolved(uncoerced_val.toType(), src);33944 break :val ty.toValue();
33904 break :val uncoerced_val;
33905 },33945 },
33906 .func => val: {33946 .func => val: {
33907 const func_ty = try sema.getExpectedBuiltinFnType(builtin_decl);33947 const func_ty = try sema.getExpectedBuiltinFnType(builtin_decl);
33908 const coerced = try sema.coerce(block, func_ty, Air.internedToRef(uncoerced_val.toIntern()), src);33948 const coerced = try sema.coerce(&block, func_ty, uncoerced_val, decl_src);
33909 break :val .fromInterned(coerced.toInterned().?);33949 break :val try sema.resolveConstDefinedValue(&block, decl_src, coerced, .{ .simple = .std_builtin_decl });
33910 },33950 },
33911 .string => val: {33951 .string => val: {
33912 const coerced = try sema.coerce(block, .slice_const_u8, Air.internedToRef(uncoerced_val.toIntern()), src);33952 const coerced = try sema.coerce(&block, .slice_const_u8, uncoerced_val, decl_src);
33913 break :val .fromInterned(coerced.toInterned().?);33953 break :val try sema.resolveConstDefinedValue(&block, decl_src, coerced, .{ .simple = .std_builtin_decl });
33914 },33954 },
33915 };33955 };
3391633956
33917 const prev = zcu.builtin_decl_values.get(builtin_decl);33957 if (zcu.builtin_decl_values.get(builtin_decl) != val.toIntern()) {
33918 if (val.toIntern() != prev) {
33919 zcu.builtin_decl_values.set(builtin_decl, val.toIntern());33958 zcu.builtin_decl_values.set(builtin_decl, val.toIntern());
33920 any_changed = true;33959 any_changed = true;
33921 }33960 }
...@@ -34147,21 +34186,15 @@ fn zirStructDecl(...@@ -34147,21 +34186,15 @@ fn zirStructDecl(
34147 });34186 });
34148 errdefer pt.destroyNamespace(new_namespace_index);34187 errdefer pt.destroyNamespace(new_namespace_index);
34149 try pt.scanNamespace(new_namespace_index, struct_decl.decls);34188 try pt.scanNamespace(new_namespace_index, struct_decl.decls);
34150
34151 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);34189 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);
34152
34153 break :ty .fromInterned(wip.finish(ip, new_namespace_index));34190 break :ty .fromInterned(wip.finish(ip, new_namespace_index));
34154 },34191 },
34155 };34192 };
3415634193
34157 try sema.addTypeReferenceEntry(src, ty);34194 try sema.addTypeReferenceEntry(src, ty);
34158
34159 // Make sure we update the namespace if the declaration is re-analyzed, to pick
34160 // up on e.g. changed comptime decls.
34161 // TODO MLUGG: me no likey, maybe model namespaces less badly idk
34162 try pt.ensureNamespaceUpToDate(ty.getNamespaceIndex(zcu));34195 try pt.ensureNamespaceUpToDate(ty.getNamespaceIndex(zcu));
3416334196
34164 return .fromIntern(ty.toIntern());34197 return .fromType(ty);
34165}34198}
34166fn zirUnionDecl(34199fn zirUnionDecl(
34167 sema: *Sema,34200 sema: *Sema,
...@@ -34218,7 +34251,6 @@ fn zirUnionDecl(...@@ -34218,7 +34251,6 @@ fn zirUnionDecl(
34218 .wip => |wip| ty: {34251 .wip => |wip| ty: {
34219 errdefer wip.cancel(ip, pt.tid);34252 errdefer wip.cancel(ip, pt.tid);
34220 try sema.setTypeName(block, &wip, union_decl.name_strategy, "union", inst);34253 try sema.setTypeName(block, &wip, union_decl.name_strategy, "union", inst);
34221
34222 const new_namespace_index: InternPool.NamespaceIndex = try pt.createNamespace(.{34254 const new_namespace_index: InternPool.NamespaceIndex = try pt.createNamespace(.{
34223 .parent = block.namespace.toOptional(),34255 .parent = block.namespace.toOptional(),
34224 .owner_type = wip.index,34256 .owner_type = wip.index,
...@@ -34226,23 +34258,16 @@ fn zirUnionDecl(...@@ -34226,23 +34258,16 @@ fn zirUnionDecl(
34226 .generation = zcu.generation,34258 .generation = zcu.generation,
34227 });34259 });
34228 errdefer pt.destroyNamespace(new_namespace_index);34260 errdefer pt.destroyNamespace(new_namespace_index);
34229
34230 try pt.scanNamespace(new_namespace_index, union_decl.decls);34261 try pt.scanNamespace(new_namespace_index, union_decl.decls);
34231
34232 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);34262 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);
34233
34234 break :ty .fromInterned(wip.finish(ip, new_namespace_index));34263 break :ty .fromInterned(wip.finish(ip, new_namespace_index));
34235 },34264 },
34236 };34265 };
3423734266
34238 try sema.addTypeReferenceEntry(src, ty);34267 try sema.addTypeReferenceEntry(src, ty);
34239
34240 // Make sure we update the namespace if the declaration is re-analyzed, to pick
34241 // up on e.g. changed comptime decls.
34242 // TODO MLUGG: me no likey, maybe model namespaces less badly idk
34243 try pt.ensureNamespaceUpToDate(ty.getNamespaceIndex(zcu));34268 try pt.ensureNamespaceUpToDate(ty.getNamespaceIndex(zcu));
3424434269
34245 return .fromIntern(ty.toIntern());34270 return .fromType(ty);
34246}34271}
34247fn zirEnumDecl(34272fn zirEnumDecl(
34248 sema: *Sema,34273 sema: *Sema,
...@@ -34277,9 +34302,7 @@ fn zirEnumDecl(...@@ -34277,9 +34302,7 @@ fn zirEnumDecl(
34277 .existing => |ty| .fromInterned(ty),34302 .existing => |ty| .fromInterned(ty),
34278 .wip => |wip| ty: {34303 .wip => |wip| ty: {
34279 errdefer wip.cancel(ip, pt.tid);34304 errdefer wip.cancel(ip, pt.tid);
34280
34281 try sema.setTypeName(block, &wip, enum_decl.name_strategy, "enum", inst);34305 try sema.setTypeName(block, &wip, enum_decl.name_strategy, "enum", inst);
34282
34283 const new_namespace_index: InternPool.NamespaceIndex = try pt.createNamespace(.{34306 const new_namespace_index: InternPool.NamespaceIndex = try pt.createNamespace(.{
34284 .parent = block.namespace.toOptional(),34307 .parent = block.namespace.toOptional(),
34285 .owner_type = wip.index,34308 .owner_type = wip.index,
...@@ -34287,23 +34310,16 @@ fn zirEnumDecl(...@@ -34287,23 +34310,16 @@ fn zirEnumDecl(
34287 .generation = zcu.generation,34310 .generation = zcu.generation,
34288 });34311 });
34289 errdefer pt.destroyNamespace(new_namespace_index);34312 errdefer pt.destroyNamespace(new_namespace_index);
34290
34291 try pt.scanNamespace(new_namespace_index, enum_decl.decls);34313 try pt.scanNamespace(new_namespace_index, enum_decl.decls);
34292
34293 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);34314 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);
34294
34295 break :ty .fromInterned(wip.finish(ip, new_namespace_index));34315 break :ty .fromInterned(wip.finish(ip, new_namespace_index));
34296 },34316 },
34297 };34317 };
3429834318
34299 try sema.addTypeReferenceEntry(src, ty);34319 try sema.addTypeReferenceEntry(src, ty);
34300
34301 // Make sure we update the namespace if the declaration is re-analyzed, to pick
34302 // up on e.g. changed comptime decls.
34303 // TODO MLUGG: me no likey, maybe model namespaces less badly idk
34304 try pt.ensureNamespaceUpToDate(ty.getNamespaceIndex(zcu));34320 try pt.ensureNamespaceUpToDate(ty.getNamespaceIndex(zcu));
3430534321
34306 return .fromIntern(ty.toIntern());34322 return .fromType(ty);
34307}34323}
34308fn zirOpaqueDecl(34324fn zirOpaqueDecl(
34309 sema: *Sema,34325 sema: *Sema,
...@@ -34350,11 +34366,7 @@ fn zirOpaqueDecl(...@@ -34350,11 +34366,7 @@ fn zirOpaqueDecl(
34350 };34366 };
3435134367
34352 try sema.addTypeReferenceEntry(src, ty);34368 try sema.addTypeReferenceEntry(src, ty);
34353
34354 // Make sure we update the namespace if the declaration is re-analyzed, to pick
34355 // up on e.g. changed comptime decls.
34356 // TODO MLUGG: me no likey, maybe model namespaces less badly idk
34357 try pt.ensureNamespaceUpToDate(ty.getNamespaceIndex(zcu));34369 try pt.ensureNamespaceUpToDate(ty.getNamespaceIndex(zcu));
3435834370
34359 return .fromIntern(ty.toIntern());34371 return .fromType(ty);
34360}34372}
src/Zcu/PerThread.zig+4-46
...@@ -688,6 +688,8 @@ pub fn ensureFileAnalyzed(pt: Zcu.PerThread, file_index: Zcu.File.Index) (Alloca...@@ -688,6 +688,8 @@ pub fn ensureFileAnalyzed(pt: Zcu.PerThread, file_index: Zcu.File.Index) (Alloca
688688
689 if (zcu.fileRootType(file_index) != .none) return; // already good689 if (zcu.fileRootType(file_index) != .none) return; // already good
690690
691 if (zcu.comp.time_report) |*tr| tr.stats.n_imported_files += 1;
692
691 const file = zcu.fileByIndex(file_index);693 const file = zcu.fileByIndex(file_index);
692 assert(file.getMode() == .zig);694 assert(file.getMode() == .zig);
693 const struct_decl = file.zir.?.getStructDecl(.main_struct_inst);695 const struct_decl = file.zir.?.getStructDecl(.main_struct_inst);
...@@ -719,13 +721,8 @@ pub fn ensureFileAnalyzed(pt: Zcu.PerThread, file_index: Zcu.File.Index) (Alloca...@@ -719,13 +721,8 @@ pub fn ensureFileAnalyzed(pt: Zcu.PerThread, file_index: Zcu.File.Index) (Alloca
719 });721 });
720 errdefer pt.destroyNamespace(new_namespace_index);722 errdefer pt.destroyNamespace(new_namespace_index);
721 try pt.scanNamespace(new_namespace_index, struct_decl.decls);723 try pt.scanNamespace(new_namespace_index, struct_decl.decls);
722
723 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);724 if (zcu.comp.debugIncremental()) try zcu.incremental_debug_state.newType(zcu, wip.index);
724725 zcu.setFileRootType(file_index, wip.finish(ip, new_namespace_index));
725 const file_root_type: Type = .fromInterned(wip.finish(ip, new_namespace_index));
726
727 zcu.setFileRootType(file_index, file_root_type.toIntern());
728 if (zcu.comp.time_report) |*tr| tr.stats.n_imported_files += 1;
729}726}
730727
731/// Ensures that all memoized state on `Zcu` is up-to-date, performing re-analysis if necessary.728/// Ensures that all memoized state on `Zcu` is up-to-date, performing re-analysis if necessary.
...@@ -810,37 +807,14 @@ pub fn ensureMemoizedStateUpToDate(pt: Zcu.PerThread, stage: InternPool.Memoized...@@ -810,37 +807,14 @@ pub fn ensureMemoizedStateUpToDate(pt: Zcu.PerThread, stage: InternPool.Memoized
810807
811fn analyzeMemoizedState(pt: Zcu.PerThread, stage: InternPool.MemoizedStateStage) Zcu.CompileError!bool {808fn analyzeMemoizedState(pt: Zcu.PerThread, stage: InternPool.MemoizedStateStage) Zcu.CompileError!bool {
812 const zcu = pt.zcu;809 const zcu = pt.zcu;
813 const ip = &zcu.intern_pool;
814 const comp = zcu.comp;810 const comp = zcu.comp;
815 const gpa = comp.gpa;811 const gpa = comp.gpa;
816 const io = comp.io;
817812
818 const unit: AnalUnit = .wrap(.{ .memoized_state = stage });813 const unit: AnalUnit = .wrap(.{ .memoized_state = stage });
819814
820 try zcu.analysis_in_progress.putNoClobber(gpa, unit, {});815 try zcu.analysis_in_progress.putNoClobber(gpa, unit, {});
821 defer assert(zcu.analysis_in_progress.swapRemove(unit));816 defer assert(zcu.analysis_in_progress.swapRemove(unit));
822817
823 // Before we begin, collect:
824 // * The type `std`, and its namespace
825 // * The type `std.builtin`, and its namespace
826 // * A semi-reasonable source location
827 const std_file_index = zcu.module_roots.get(zcu.std_mod).?.unwrap().?;
828 try pt.ensureFileAnalyzed(std_file_index);
829 const std_type: Type = .fromInterned(zcu.fileRootType(std_file_index));
830 const std_namespace = std_type.getNamespaceIndex(zcu);
831 try pt.ensureNamespaceUpToDate(std_namespace);
832 const builtin_str = try ip.getOrPutString(gpa, io, pt.tid, "builtin", .no_embedded_nulls);
833 const builtin_nav = zcu.namespacePtr(std_namespace).pub_decls.getKeyAdapted(builtin_str, Zcu.Namespace.NameAdapter{ .zcu = zcu }) orelse
834 @panic("lib/std.zig is corrupt and missing 'builtin'");
835 try pt.ensureNavValUpToDate(builtin_nav);
836 const builtin_type: Type = .fromInterned(ip.getNav(builtin_nav).status.fully_resolved.val);
837 const builtin_namespace = builtin_type.getNamespaceIndex(zcu);
838 try pt.ensureNamespaceUpToDate(builtin_namespace);
839 const src: Zcu.LazySrcLoc = .{
840 .base_node_inst = builtin_type.typeDeclInst(zcu).?,
841 .offset = .{ .byte_abs = 0 },
842 };
843
844 var analysis_arena: std.heap.ArenaAllocator = .init(gpa);818 var analysis_arena: std.heap.ArenaAllocator = .init(gpa);
845 defer analysis_arena.deinit();819 defer analysis_arena.deinit();
846820
...@@ -861,22 +835,7 @@ fn analyzeMemoizedState(pt: Zcu.PerThread, stage: InternPool.MemoizedStateStage)...@@ -861,22 +835,7 @@ fn analyzeMemoizedState(pt: Zcu.PerThread, stage: InternPool.MemoizedStateStage)
861 };835 };
862 defer sema.deinit();836 defer sema.deinit();
863837
864 var block: Sema.Block = .{838 return sema.analyzeMemoizedState(stage);
865 .parent = null,
866 .sema = &sema,
867 .namespace = std_namespace,
868 .instructions = .empty,
869 .inlining = null,
870 .comptime_reason = .{ .reason = .{
871 .src = src,
872 .r = .{ .simple = .type },
873 } },
874 .src_base_inst = src.base_node_inst,
875 .type_name_ctx = .empty,
876 };
877 defer block.instructions.deinit(gpa);
878
879 return sema.analyzeMemoizedState(&block, src, builtin_namespace, stage);
880}839}
881840
882/// Ensures that the state of the given `ComptimeUnit` is fully up-to-date, performing re-analysis841/// Ensures that the state of the given `ComptimeUnit` is fully up-to-date, performing re-analysis
...@@ -1966,7 +1925,6 @@ fn analyzeFuncBody(...@@ -1966,7 +1925,6 @@ fn analyzeFuncBody(
1966/// If the file's root struct type is not populated (the file is unreferenced), nothing is done.1925/// If the file's root struct type is not populated (the file is unreferenced), nothing is done.
1967/// This is called by `updateZirRefs` for all updated files before the main work loop.1926/// This is called by `updateZirRefs` for all updated files before the main work loop.
1968/// This function does not perform any semantic analysis.1927/// This function does not perform any semantic analysis.
1969/// MLUGG TODO: mmmmm i have no idea if this makes sense... tbhwy i just want to update all *changed* namespaces at the start of an update or something lol
1970fn updateFileNamespace(pt: Zcu.PerThread, file_index: Zcu.File.Index) Allocator.Error!void {1928fn updateFileNamespace(pt: Zcu.PerThread, file_index: Zcu.File.Index) Allocator.Error!void {
1971 const zcu = pt.zcu;1929 const zcu = pt.zcu;
19721930