diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-05-04 17:02:25 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2025-05-18 17:37:02 +0100 |
| commit | 37a9a4e0f16c1df8de3a4add3a9566b24f024a95 (patch) | |
| tree | 0fc8f1fc15193e8e3c3ccd5e97408cef7372f394 /src/Package/Module.zig | |
| parent | d32829e053af2a0f382d4d692ede85c176c9f803 (diff) | |
| download | zig-37a9a4e0f16c1df8de3a4add3a9566b24f024a95.tar.gz zig-37a9a4e0f16c1df8de3a4add3a9566b24f024a95.zip | |
compiler: refactor `Zcu.File` and path representation
This commit makes some big changes to how we track state for Zig source
files. In particular, it changes:
* How `File` tracks its path on-disk
* How AstGen discovers files
* How file-level errors are tracked
* How `builtin.zig` files and modules are created
The original motivation here was to address incremental compilation bugs
with the handling of files, such as #22696. To fix this, a few changes
are necessary.
Just like declarations may become unreferenced on an incremental update,
meaning we suppress analysis errors associated with them, it is also
possible for all imports of a file to be removed on an incremental
update, in which case file-level errors for that file should be
suppressed. As such, after AstGen, the compiler must traverse files
(starting from analysis roots) and discover the set of "live files" for
this update.
Additionally, the compiler's previous handling of retryable file errors
was not very good; the source location the error was reported as was
based only on the first discovered import of that file. This source
location also disappeared on future incremental updates. So, as a part
of the file traversal above, we also need to figure out the source
locations of imports which errors should be reported against.
Another observation I made is that the "file exists in multiple modules"
error was not implemented in a particularly good way (I get to say that
because I wrote it!). It was subject to races, where the order in which
different imports of a file were discovered affects both how errors are
printed, and which module the file is arbitrarily assigned, with the
latter in turn affecting which other files are considered for import.
The thing I realised here is that while the AstGen worker pool is
running, we cannot know for sure which module(s) a file is in; we could
always discover an import later which changes the answer.
So, here's how the AstGen workers have changed. We initially ensure that
`zcu.import_table` contains the root files for all modules in this Zcu,
even if we don't know any imports for them yet. Then, the AstGen
workers do not need to be aware of modules. Instead, they simply ignore
module imports, and only spin off more workers when they see a by-path
import.
During AstGen, we can't use module-root-relative paths, since we don't
know which modules files are in; but we don't want to unnecessarily use
absolute files either, because those are non-portable and can make
`error.NameTooLong` more likely. As such, I have introduced a new
abstraction, `Compilation.Path`. This type is a way of representing a
filesystem path which has a *canonical form*. The path is represented
relative to one of a few special directories: the lib directory, the
global cache directory, or the local cache directory. As a fallback, we
use absolute (or cwd-relative on WASI) paths. This is kind of similar to
`std.Build.Cache.Path` with a pre-defined list of possible
`std.Build.Cache.Directory`, but has stricter canonicalization rules
based on path resolution to make sure deduplicating files works
properly. A `Compilation.Path` can be trivially converted to a
`std.Build.Cache.Path` from a `Compilation`, but is smaller, has a
canonical form, and has a digest which will be consistent across
different compiler processes with the same lib and cache directories
(important when we serialize incremental compilation state in the
future). `Zcu.File` and `Zcu.EmbedFile` both contain a
`Compilation.Path`, which is used to access the file on-disk;
module-relative sub paths are used quite rarely (`EmbedFile` doesn't
even have one now for simplicity).
After the AstGen workers all complete, we know that any file which might
be imported is definitely in `import_table` and up-to-date. So, we
perform a single-threaded graph traversal; similar to what
`resolveReferences` plays for `AnalUnit`s, but for files instead. We
figure out which files are alive, and which module each file is in. If a
file turns out to be in multiple modules, we set a field on `Zcu` to
indicate this error. If a file is in a different module to a prior
update, we set a flag instructing `updateZirRefs` to invalidate all
dependencies on the file. This traversal also discovers "import errors";
these are errors associated with a specific `@import`. With Zig's
current design, there is only one possible error here: "import outside
of module root". This must be identified during this traversal instead
of during AstGen, because it depends on which module the file is in. I
tried also representing "module not found" errors in this same way, but
it turns out to be much more useful to report those in Sema, because of
use cases like optional dependencies where a module import is behind a
comptime-known build option.
For simplicity, `failed_files` now just maps to `?[]u8`, since the
source location is always the whole file. In fact, this allows removing
`LazySrcLoc.Offset.entire_file` completely, slightly simplifying some
error reporting logic. File-level errors are now directly built in the
`std.zig.ErrorBundle.Wip`. If the payload is not `null`, it is the
message for a retryable error (i.e. an error loading the source file),
and will be reported with a "file imported here" note pointing to the
import site discovered during the single-threaded file traversal.
The last piece of fallout here is how `Builtin` works. Rather than
constructing "builtin" modules when creating `Package.Module`s, they are
now constructed on-the-fly by `Zcu`. The map `Zcu.builtin_modules` maps
from digests to `*Package.Module`s. These digests are abstract hashes of
the `Builtin` value; i.e. all of the options which are placed into
"builtin.zig". During the file traversal, we populate `builtin_modules`
as needed, so that when we see this imports in Sema, we just grab the
relevant entry from this map. This eliminates a bunch of awkward state
tracking during construction of the module graph. It's also now clearer
exactly what options the builtin module has, since previously it
inherited some options arbitrarily from the first-created module with
that "builtin" module!
The user-visible effects of this commit are:
* retryable file errors are now consistently reported against the whole
file, with a note pointing to a live import of that file
* some theoretical bugs where imports are wrongly considered distinct
(when the import path moves out of the cwd and then back in) are fixed
* some consistency issues with how file-level errors are reported are
fixed; these errors will now always be printed in the same order
regardless of how the AstGen pass assigns file indices
* incremental updates do not print retryable file errors differently
between updates or depending on file structure/contents
* incremental updates support files changing modules
* incremental updates support files becoming unreferenced
Resolves: #22696
Diffstat (limited to 'src/Package/Module.zig')
| -rw-r--r-- | src/Package/Module.zig | 222 |
1 files changed, 74 insertions, 148 deletions
diff --git a/src/Package/Module.zig b/src/Package/Module.zig index becce6536e..295b25dd62 100644 --- a/src/Package/Module.zig +++ b/src/Package/Module.zig @@ -1,15 +1,17 @@ //! Corresponds to something that Zig source code can `@import`. -/// Only files inside this directory can be imported. -root: Cache.Path, -/// Relative to `root`. May contain path separators. +/// The root directory of the module. Only files inside this directory can be imported. +root: Compilation.Path, +/// Path to the root source file of this module. Relative to `root`. May contain path separators. root_src_path: []const u8, /// Name used in compile errors. Looks like "root.foo.bar". fully_qualified_name: []const u8, -/// The dependency table of this module. Shared dependencies such as 'std', -/// 'builtin', and 'root' are not specified in every dependency table, but -/// instead only in the table of `main_mod`. `Module.importFile` is -/// responsible for detecting these names and using the correct package. +/// The dependency table of this module. The shared dependencies 'std' and +/// 'root' are not specified in every module dependency table, but are stored +/// separately in `Zcu`. 'builtin' is also not stored here, although it is +/// not necessarily the same between all modules. Handling of `@import` in +/// the rest of the compiler must detect these special names and use the +/// correct module instead of consulting `deps`. deps: Deps = .{}, resolved_target: ResolvedTarget, @@ -33,25 +35,14 @@ cc_argv: []const []const u8, structured_cfg: bool, no_builtin: bool, -/// If the module is an `@import("builtin")` module, this is the `File` that -/// is preallocated for it. Otherwise this field is null. -builtin_file: ?*File, - pub const Deps = std.StringArrayHashMapUnmanaged(*Module); -pub fn isBuiltin(m: Module) bool { - return m.builtin_file != null; -} - pub const Tree = struct { /// Each `Package` exposes a `Module` with build.zig as its root source file. build_module_table: std.AutoArrayHashMapUnmanaged(MultiHashHexDigest, *Module), }; pub const CreateOptions = struct { - /// Where to store builtin.zig. The global cache directory is used because - /// it is a pure function based on CLI flags. - global_cache_directory: Cache.Directory, paths: Paths, fully_qualified_name: []const u8, @@ -61,15 +52,8 @@ pub const CreateOptions = struct { /// If this is null then `resolved_target` must be non-null. parent: ?*Package.Module, - builtin_mod: ?*Package.Module, - - /// Allocated into the given `arena`. Should be shared across all module creations in a Compilation. - /// Ignored if `builtin_mod` is passed or if `!have_zcu`. - /// Otherwise, may be `null` only if this Compilation consists of a single module. - builtin_modules: ?*std.StringHashMapUnmanaged(*Module), - pub const Paths = struct { - root: Cache.Path, + root: Compilation.Path, /// Relative to `root`. May contain path separators. root_src_path: []const u8, }; @@ -401,126 +385,13 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module { .cc_argv = options.cc_argv, .structured_cfg = structured_cfg, .no_builtin = no_builtin, - .builtin_file = null, }; - - const opt_builtin_mod = options.builtin_mod orelse b: { - if (!options.global.have_zcu) break :b null; - - const generated_builtin_source = try Builtin.generate(.{ - .target = target, - .zig_backend = zig_backend, - .output_mode = options.global.output_mode, - .link_mode = options.global.link_mode, - .unwind_tables = unwind_tables, - .is_test = options.global.is_test, - .single_threaded = single_threaded, - .link_libc = options.global.link_libc, - .link_libcpp = options.global.link_libcpp, - .optimize_mode = optimize_mode, - .error_tracing = error_tracing, - .valgrind = valgrind, - .sanitize_thread = sanitize_thread, - .fuzz = fuzz, - .pic = pic, - .pie = options.global.pie, - .strip = strip, - .code_model = code_model, - .omit_frame_pointer = omit_frame_pointer, - .wasi_exec_model = options.global.wasi_exec_model, - }, arena); - - const new = if (options.builtin_modules) |builtins| new: { - const gop = try builtins.getOrPut(arena, generated_builtin_source); - if (gop.found_existing) break :b gop.value_ptr.*; - errdefer builtins.removeByPtr(gop.key_ptr); - const new = try arena.create(Module); - gop.value_ptr.* = new; - break :new new; - } else try arena.create(Module); - errdefer if (options.builtin_modules) |builtins| assert(builtins.remove(generated_builtin_source)); - - const new_file = try arena.create(File); - - const hex_digest = digest: { - var hasher: Cache.Hasher = Cache.hasher_init; - hasher.update(generated_builtin_source); - - var bin_digest: Cache.BinDigest = undefined; - hasher.final(&bin_digest); - - var hex_digest: Cache.HexDigest = undefined; - _ = std.fmt.bufPrint( - &hex_digest, - "{s}", - .{std.fmt.fmtSliceHexLower(&bin_digest)}, - ) catch unreachable; - - break :digest hex_digest; - }; - - const builtin_sub_path = try arena.dupe(u8, "b" ++ std.fs.path.sep_str ++ hex_digest); - - new.* = .{ - .root = .{ - .root_dir = options.global_cache_directory, - .sub_path = builtin_sub_path, - }, - .root_src_path = "builtin.zig", - .fully_qualified_name = if (options.parent == null) - "builtin" - else - try std.fmt.allocPrint(arena, "{s}.builtin", .{options.fully_qualified_name}), - .resolved_target = .{ - .result = target, - .is_native_os = resolved_target.is_native_os, - .is_native_abi = resolved_target.is_native_abi, - .llvm_cpu_features = llvm_cpu_features, - }, - .optimize_mode = optimize_mode, - .single_threaded = single_threaded, - .error_tracing = error_tracing, - .valgrind = valgrind, - .pic = pic, - .strip = strip, - .omit_frame_pointer = omit_frame_pointer, - .stack_check = stack_check, - .stack_protector = stack_protector, - .code_model = code_model, - .red_zone = red_zone, - .sanitize_c = sanitize_c, - .sanitize_thread = sanitize_thread, - .fuzz = fuzz, - .unwind_tables = unwind_tables, - .cc_argv = &.{}, - .structured_cfg = structured_cfg, - .no_builtin = no_builtin, - .builtin_file = new_file, - }; - new_file.* = .{ - .sub_file_path = "builtin.zig", - .stat = undefined, - .source = generated_builtin_source, - .tree = null, - .zir = null, - .zoir = null, - .status = .never_loaded, - .mod = new, - }; - break :b new; - }; - - if (opt_builtin_mod) |builtin_mod| { - try mod.deps.ensureUnusedCapacity(arena, 1); - mod.deps.putAssumeCapacityNoClobber("builtin", builtin_mod); - } - return mod; } /// All fields correspond to `CreateOptions`. pub const LimitedOptions = struct { - root: Cache.Path, + root: Compilation.Path, root_src_path: []const u8, fully_qualified_name: []const u8, }; @@ -553,18 +424,73 @@ pub fn createLimited(gpa: Allocator, options: LimitedOptions) Allocator.Error!*P .cc_argv = undefined, .structured_cfg = undefined, .no_builtin = undefined, - .builtin_file = null, }; return mod; } -/// Asserts that the module has a builtin module, which is not true for non-zig -/// modules such as ones only used for `@embedFile`, or the root module when -/// there is no Zig Compilation Unit. -pub fn getBuiltinDependency(m: Module) *Module { - const result = m.deps.values()[0]; - assert(result.isBuiltin()); - return result; +/// Does not ensure that the module's root directory exists on-disk; see `Builtin.updateFileOnDisk` for that task. +pub fn createBuiltin(arena: Allocator, opts: Builtin, dirs: Compilation.Directories) Allocator.Error!*Module { + const sub_path = "b" ++ Cache.binToHex(opts.hash()); + const new = try arena.create(Module); + new.* = .{ + .root = try .fromRoot(arena, dirs, .global_cache, sub_path), + .root_src_path = "builtin.zig", + .fully_qualified_name = "builtin", + .resolved_target = .{ + .result = opts.target, + // These values are not in `opts`, but do not matter because `builtin.zig` contains no runtime code. + .is_native_os = false, + .is_native_abi = false, + .llvm_cpu_features = null, + }, + .optimize_mode = opts.optimize_mode, + .single_threaded = opts.single_threaded, + .error_tracing = opts.error_tracing, + .valgrind = opts.valgrind, + .pic = opts.pic, + .strip = opts.strip, + .omit_frame_pointer = opts.omit_frame_pointer, + .code_model = opts.code_model, + .sanitize_thread = opts.sanitize_thread, + .fuzz = opts.fuzz, + .unwind_tables = opts.unwind_tables, + .cc_argv = &.{}, + // These values are not in `opts`, but do not matter because `builtin.zig` contains no runtime code. + .stack_check = false, + .stack_protector = 0, + .red_zone = false, + .sanitize_c = .off, + .structured_cfg = false, + .no_builtin = false, + }; + return new; +} + +/// Returns the `Builtin` which forms the contents of `@import("builtin")` for this module. +pub fn getBuiltinOptions(m: Module, global: Compilation.Config) Builtin { + assert(global.have_zcu); + return .{ + .target = m.resolved_target.result, + .zig_backend = target_util.zigBackend(m.resolved_target.result, global.use_llvm), + .output_mode = global.output_mode, + .link_mode = global.link_mode, + .unwind_tables = m.unwind_tables, + .is_test = global.is_test, + .single_threaded = m.single_threaded, + .link_libc = global.link_libc, + .link_libcpp = global.link_libcpp, + .optimize_mode = m.optimize_mode, + .error_tracing = m.error_tracing, + .valgrind = m.valgrind, + .sanitize_thread = m.sanitize_thread, + .fuzz = m.fuzz, + .pic = m.pic, + .pie = global.pie, + .strip = m.strip, + .code_model = m.code_model, + .omit_frame_pointer = m.omit_frame_pointer, + .wasi_exec_model = global.wasi_exec_model, + }; } const Module = @This(); |
