diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2024-06-14 23:05:39 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2024-06-15 00:57:52 +0100 |
| commit | 1eaeb4a0a838a783d2060f4e5b3b26b483b26009 (patch) | |
| tree | 16fc6b1e9279a6c19e9d41574e4c304e2f387d42 /src/Compilation.zig | |
| parent | 07a24bec9a578857920f4c5508f1d7eea65177b8 (diff) | |
| download | zig-1eaeb4a0a838a783d2060f4e5b3b26b483b26009.tar.gz zig-1eaeb4a0a838a783d2060f4e5b3b26b483b26009.zip | |
Zcu: rework source locations
`LazySrcLoc` now stores a reference to the "base AST node" to which it
is relative. The previous tagged union is `LazySrcLoc.Offset`. To make
working with this structure convenient, `Sema.Block` contains a
convenience `src` method which takes an `Offset` and returns a
`LazySrcLoc`.
The "base node" of a source location is no longer given by a `Decl`, but
rather a `TrackedInst` representing either a `declaration`,
`struct_decl`, `union_decl`, `enum_decl`, or `opaque_decl`. This is a
more appropriate model, and removes an unnecessary responsibility from
`Decl` in preparation for the upcoming refactor which will split it into
`Nav` and `Cau`.
As a part of these `Decl` reworks, the `src_node` field is eliminated.
This change aids incremental compilation, and simplifies `Decl`. In some
cases -- particularly in backends -- the source location of a
declaration is desired. This was previously `Decl.srcLoc` and worked for
any `Decl`. Now, it is `Decl.navSrcLoc` in reference to the upcoming
refactor, since the set of `Decl`s this works for precisely corresponds
to what will in future become a `Nav` -- that is, source-level
declarations and generic function instantiations, but *not* type owner
Decls.
This commit introduces more tags to `LazySrcLoc.Offset` so as to
eliminate the concept of `error.NeededSourceLocation`. Now, `.unneeded`
should only be used to assert that an error path is unreachable. In the
future, uses of `.unneeded` can probably be replaced with `undefined`.
The `src_decl` field of `Sema.Block` no longer has a role in type
resolution. Its main remaining purpose is to handle namespacing of type
names. It will be eliminated entirely in a future commit to remove
another undue responsibility from `Decl`.
It is worth noting that in future, the `Zcu.SrcLoc` type should probably
be eliminated entirely in favour of storing `Zcu.LazySrcLoc` values.
This is because `Zcu.SrcLoc` is not valid across incremental updates,
and we want to be able to reuse error messages from previous updates
even if the source file in question changed. The error reporting logic
should instead simply resolve the location from the `LazySrcLoc` on the
fly.
Diffstat (limited to 'src/Compilation.zig')
| -rw-r--r-- | src/Compilation.zig | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index 507cbfc6d5..32243daf69 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -2639,7 +2639,7 @@ fn reportMultiModuleErrors(mod: *Module) !void { .root => |pkg| blk: { break :blk try Module.ErrorMsg.init( mod.gpa, - .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file }, + .{ .file_scope = file, .base_node = 0, .lazy = .entire_file }, "root of module {s}", .{pkg.fully_qualified_name}, ); @@ -2651,7 +2651,7 @@ fn reportMultiModuleErrors(mod: *Module) !void { if (omitted > 0) { notes[num_notes] = try Module.ErrorMsg.init( mod.gpa, - .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file }, + .{ .file_scope = file, .base_node = 0, .lazy = .entire_file }, "{} more references omitted", .{omitted}, ); @@ -2660,7 +2660,7 @@ fn reportMultiModuleErrors(mod: *Module) !void { const err = try Module.ErrorMsg.create( mod.gpa, - .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file }, + .{ .file_scope = file, .base_node = 0, .lazy = .entire_file }, "file exists in multiple modules", .{}, ); @@ -3040,29 +3040,26 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle { } } - if (comp.module) |module| { - if (bundle.root_list.items.len == 0 and module.compile_log_decls.count() != 0) { - const keys = module.compile_log_decls.keys(); - const values = module.compile_log_decls.values(); + if (comp.module) |zcu| { + if (bundle.root_list.items.len == 0 and zcu.compile_log_decls.count() != 0) { + const values = zcu.compile_log_decls.values(); // First one will be the error; subsequent ones will be notes. - const err_decl = module.declPtr(keys[0]); - const src_loc = err_decl.nodeOffsetSrcLoc(values[0], module); - const err_msg = Module.ErrorMsg{ + const src_loc = values[0].src().upgrade(zcu); + const err_msg: Module.ErrorMsg = .{ .src_loc = src_loc, .msg = "found compile log statement", - .notes = try gpa.alloc(Module.ErrorMsg, module.compile_log_decls.count() - 1), + .notes = try gpa.alloc(Module.ErrorMsg, zcu.compile_log_decls.count() - 1), }; defer gpa.free(err_msg.notes); - for (keys[1..], 0..) |key, i| { - const note_decl = module.declPtr(key); - err_msg.notes[i] = .{ - .src_loc = note_decl.nodeOffsetSrcLoc(values[i + 1], module), + for (values[1..], err_msg.notes) |src_info, *note| { + note.* = .{ + .src_loc = src_info.src().upgrade(zcu), .msg = "also here", }; } - try addModuleErrorMsg(module, &bundle, err_msg); + try addModuleErrorMsg(zcu, &bundle, err_msg); } } @@ -3492,7 +3489,7 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo try module.failed_decls.ensureUnusedCapacity(gpa, 1); module.failed_decls.putAssumeCapacityNoClobber(decl_index, try Module.ErrorMsg.create( gpa, - decl.srcLoc(module), + decl.navSrcLoc(module).upgrade(module), "unable to update line number: {s}", .{@errorName(err)}, )); @@ -3993,7 +3990,7 @@ fn workerAstGenFile( if (!res.is_pkg) { res.file.addReference(mod.*, .{ .import = .{ .file_scope = file, - .parent_decl_node = 0, + .base_node = 0, .lazy = .{ .token_abs = item.data.token }, } }) catch continue; } @@ -4370,7 +4367,7 @@ fn reportRetryableAstGenError( const src_loc: Module.SrcLoc = switch (src) { .root => .{ .file_scope = file, - .parent_decl_node = 0, + .base_node = 0, .lazy = .entire_file, }, .import => |info| blk: { @@ -4378,7 +4375,7 @@ fn reportRetryableAstGenError( break :blk .{ .file_scope = importing_file, - .parent_decl_node = 0, + .base_node = 0, .lazy = .{ .token_abs = info.import_tok }, }; }, |
