fix(diagnostics): locate import parse errors in the imported file

A parse error raised while resolving an `#import` was rendered against the
ROOT file's source — the caret landed on an unrelated line (often a comment)
even though the message named the correct imported file.

Two compounding causes:
- core.zig wired `diagnostics.import_sources` only AFTER import resolution
  returned, but a parse error aborts mid-resolution (before that wiring), so
  the renderer had no imported sources and fell back to the root file. Wire it
  (and seed the main-file source) BEFORE resolving.
- imports.zig emitted the diagnostic at the importer's `#import` span instead
  of the parser's actual error offset inside the imported file, and didn't pin
  the diagnostic's source_file to that file.

parser.zig now records `err_end` alongside `err_offset` for a proper caret
width. New `DiagnosticList.addFmtInFile` renders against an explicit source
file; imports.zig uses it with `importErrSpan(&p)`.

Regression test: examples/1176-diagnostics-import-parse-error-location
(importer + deliberately-broken companion; caret must land in the companion).
This commit is contained in:
agra
2026-06-15 15:09:40 +03:00
parent fe9bd75e09
commit d6a9c4f0c4
9 changed files with 66 additions and 7 deletions

View File

@@ -15,6 +15,7 @@ pub const Parser = struct {
allocator: std.mem.Allocator,
err_msg: ?[]const u8,
err_offset: ?u32 = null,
err_end: ?u32 = null,
prev_end: u32 = 0,
diagnostics: ?*errors.DiagnosticList = null,
/// Type param names from enclosing generic struct (set while parsing methods)
@@ -3976,6 +3977,7 @@ pub const Parser = struct {
fn fail(self: *Parser, msg: []const u8) error{ParseError} {
self.err_msg = msg;
self.err_offset = self.current.loc.start;
self.err_end = self.current.loc.end;
if (self.diagnostics) |diags| {
diags.add(.err, msg, .{ .start = self.current.loc.start, .end = self.current.loc.end });
}
@@ -3988,6 +3990,7 @@ pub const Parser = struct {
fn failAt(self: *Parser, loc: anytype, msg: []const u8) error{ParseError} {
self.err_msg = msg;
self.err_offset = loc.start;
self.err_end = loc.end;
if (self.diagnostics) |diags| {
diags.add(.err, msg, .{ .start = loc.start, .end = loc.end });
}