Skip to content

Commit

Permalink
Merge pull request #22754 from mlugg/files-and-stuff
Browse files Browse the repository at this point in the history
ZON and incremental bits
  • Loading branch information
mlugg authored Feb 5, 2025
2 parents cf059ee + bebfa03 commit f01f1e3
Show file tree
Hide file tree
Showing 16 changed files with 788 additions and 587 deletions.
25 changes: 25 additions & 0 deletions lib/std/zig/Zoir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ string_bytes: []u8,
compile_errors: []Zoir.CompileError,
error_notes: []Zoir.CompileError.Note,

/// The data stored at byte offset 0 when ZOIR is stored in a file.
pub const Header = extern struct {
nodes_len: u32,
extra_len: u32,
limbs_len: u32,
string_bytes_len: u32,
compile_errors_len: u32,
error_notes_len: u32,

/// We could leave this as padding, however it triggers a Valgrind warning because
/// we read and write undefined bytes to the file system. This is harmless, but
/// it's essentially free to have a zero field here and makes the warning go away,
/// making it more likely that following Valgrind warnings will be taken seriously.
unused: u64 = 0,

stat_inode: std.fs.File.INode,
stat_size: u64,
stat_mtime: i128,

comptime {
// Check that `unused` is working as expected
assert(std.meta.hasUniqueRepresentation(Header));
}
};

pub fn hasCompileErrors(zoir: Zoir) bool {
if (zoir.compile_errors.len > 0) {
assert(zoir.nodes.len == 0);
Expand Down
24 changes: 10 additions & 14 deletions src/Builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,12 @@ pub fn append(opts: @This(), buffer: *std.ArrayList(u8)) Allocator.Error!void {
}

pub fn populateFile(comp: *Compilation, mod: *Module, file: *File) !void {
assert(file.source_loaded == true);

if (mod.root.statFile(mod.root_src_path)) |stat| {
if (stat.size != file.source.len) {
if (stat.size != file.source.?.len) {
std.log.warn(
"the cached file '{}{s}' had the wrong size. Expected {d}, found {d}. " ++
"Overwriting with correct file contents now",
.{ mod.root, mod.root_src_path, file.source.len, stat.size },
.{ mod.root, mod.root_src_path, file.source.?.len, stat.size },
);

try writeFile(file, mod);
Expand All @@ -296,23 +294,21 @@ pub fn populateFile(comp: *Compilation, mod: *Module, file: *File) !void {

log.debug("parsing and generating '{s}'", .{mod.root_src_path});

file.tree = try std.zig.Ast.parse(comp.gpa, file.source, .zig);
assert(file.tree.errors.len == 0); // builtin.zig must parse
file.tree_loaded = true;
file.tree = try std.zig.Ast.parse(comp.gpa, file.source.?, .zig);
assert(file.tree.?.errors.len == 0); // builtin.zig must parse

file.zir = try AstGen.generate(comp.gpa, file.tree);
assert(!file.zir.hasCompileErrors()); // builtin.zig must not have astgen errors
file.zir_loaded = true;
file.status = .success_zir;
// Note that whilst we set `zir_loaded` here, we populated `path_digest`
file.zir = try AstGen.generate(comp.gpa, file.tree.?);
assert(!file.zir.?.hasCompileErrors()); // builtin.zig must not have astgen errors
file.status = .success;
// Note that whilst we set `zir` here, we populated `path_digest`
// all the way back in `Package.Module.create`.
}

fn writeFile(file: *File, mod: *Module) !void {
var buf: [std.fs.max_path_bytes]u8 = undefined;
var af = try mod.root.atomicFile(mod.root_src_path, .{ .make_path = true }, &buf);
defer af.deinit();
try af.file.writeAll(file.source);
try af.file.writeAll(file.source.?);
af.finish() catch |err| switch (err) {
error.AccessDenied => switch (builtin.os.tag) {
.windows => {
Expand All @@ -326,7 +322,7 @@ fn writeFile(file: *File, mod: *Module) !void {
};

file.stat = .{
.size = file.source.len,
.size = file.source.?.len,
.inode = 0, // dummy value
.mtime = 0, // dummy value
};
Expand Down
Loading

0 comments on commit f01f1e3

Please sign in to comment.