authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-30 13:22:48-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:36-08:00
log4fccb5ae7a3c3ad0f0ec79bf5eb628807c10eb62
tree1e0d758a607bfce81eb9e285d5ef255f32b42f5b
parent7d224516c4414015186bc1497ca6641b7390bf92

wasm linker: improve error messages by making source locations more lazy


5 files changed, 92 insertions(+), 47 deletions(-)

lib/std/zig/ErrorBundle.zig+15-13
......@@ -11,6 +11,11 @@ string_bytes: []const u8,
1111/// The first thing in this array is an `ErrorMessageList`.
1212extra: []const u32,
1313
14/// Index into `string_bytes`.
15pub const String = u32;
16/// Index into `string_bytes`, or null.
17pub const OptionalString = u32;
18
1419/// Special encoding when there are no errors.
1520pub const empty: ErrorBundle = .{
1621 .string_bytes = &.{},
......@@ -33,14 +38,13 @@ pub const ErrorMessageList = struct {
3338 len: u32,
3439 start: u32,
3540 /// null-terminated string index. 0 means no compile log text.
36 compile_log_text: u32,
41 compile_log_text: OptionalString,
3742};
3843
3944/// Trailing:
4045/// * ReferenceTrace for each reference_trace_len
4146pub const SourceLocation = struct {
42 /// null terminated string index
43 src_path: u32,
47 src_path: String,
4448 line: u32,
4549 column: u32,
4650 /// byte offset of starting token
......@@ -49,17 +53,15 @@ pub const SourceLocation = struct {
4953 span_main: u32,
5054 /// byte offset of end of last token
5155 span_end: u32,
52 /// null terminated string index, possibly null.
5356 /// Does not include the trailing newline.
54 source_line: u32 = 0,
57 source_line: OptionalString = 0,
5558 reference_trace_len: u32 = 0,
5659};
5760
5861/// Trailing:
5962/// * MessageIndex for each notes_len.
6063pub const ErrorMessage = struct {
61 /// null terminated string index
62 msg: u32,
64 msg: String,
6365 /// Usually one, but incremented for redundant messages.
6466 count: u32 = 1,
6567 src_loc: SourceLocationIndex = .none,
......@@ -71,7 +73,7 @@ pub const ReferenceTrace = struct {
7173 /// Except for the sentinel ReferenceTrace element, in which case:
7274 /// * 0 means remaining references hidden
7375 /// * >0 means N references hidden
74 decl_name: u32,
76 decl_name: String,
7577 /// Index into extra of a SourceLocation
7678 /// If this is 0, this is the sentinel ReferenceTrace element.
7779 src_loc: SourceLocationIndex,
......@@ -138,7 +140,7 @@ fn extraData(eb: ErrorBundle, comptime T: type, index: usize) struct { data: T,
138140}
139141
140142/// Given an index into `string_bytes` returns the null-terminated string found there.
141pub fn nullTerminatedString(eb: ErrorBundle, index: usize) [:0]const u8 {
143pub fn nullTerminatedString(eb: ErrorBundle, index: String) [:0]const u8 {
142144 const string_bytes = eb.string_bytes;
143145 var end: usize = index;
144146 while (string_bytes[end] != 0) {
......@@ -384,18 +386,18 @@ pub const Wip = struct {
384386 };
385387 }
386388
387 pub fn addString(wip: *Wip, s: []const u8) Allocator.Error!u32 {
389 pub fn addString(wip: *Wip, s: []const u8) Allocator.Error!String {
388390 const gpa = wip.gpa;
389 const index: u32 = @intCast(wip.string_bytes.items.len);
391 const index: String = @intCast(wip.string_bytes.items.len);
390392 try wip.string_bytes.ensureUnusedCapacity(gpa, s.len + 1);
391393 wip.string_bytes.appendSliceAssumeCapacity(s);
392394 wip.string_bytes.appendAssumeCapacity(0);
393395 return index;
394396 }
395397
396 pub fn printString(wip: *Wip, comptime fmt: []const u8, args: anytype) Allocator.Error!u32 {
398 pub fn printString(wip: *Wip, comptime fmt: []const u8, args: anytype) Allocator.Error!String {
397399 const gpa = wip.gpa;
398 const index: u32 = @intCast(wip.string_bytes.items.len);
400 const index: String = @intCast(wip.string_bytes.items.len);
399401 try wip.string_bytes.writer(gpa).print(fmt, args);
400402 try wip.string_bytes.append(gpa, 0);
401403 return index;
src/Compilation.zig+1-1
......@@ -3291,7 +3291,7 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
32913291 }));
32923292 }
32933293
3294 try comp.link_diags.addMessagesToBundle(&bundle);
3294 try comp.link_diags.addMessagesToBundle(&bundle, comp.bin_file);
32953295
32963296 if (comp.zcu) |zcu| {
32973297 if (bundle.root_list.items.len == 0 and zcu.compile_log_sources.count() != 0) {
src/link.zig+25-4
......@@ -38,6 +38,11 @@ pub const Diags = struct {
3838 flags: Flags,
3939 lld: std.ArrayListUnmanaged(Lld),
4040
41 pub const SourceLocation = union(enum) {
42 none,
43 wasm: File.Wasm.SourceLocation,
44 };
45
4146 pub const Flags = packed struct {
4247 no_entry_point_found: bool = false,
4348 missing_libc: bool = false,
......@@ -70,9 +75,25 @@ pub const Diags = struct {
7075 };
7176
7277 pub const Msg = struct {
78 source_location: SourceLocation = .none,
7379 msg: []const u8,
7480 notes: []Msg = &.{},
7581
82 fn string(
83 msg: *const Msg,
84 bundle: *std.zig.ErrorBundle.Wip,
85 base: ?*File,
86 ) Allocator.Error!std.zig.ErrorBundle.String {
87 return switch (msg.source_location) {
88 .none => try bundle.addString(msg.msg),
89 .wasm => |sl| {
90 dev.check(.wasm_linker);
91 const wasm = base.?.cast(.wasm).?;
92 return sl.string(msg.msg, bundle, wasm);
93 },
94 };
95 }
96
7697 pub fn deinit(self: *Msg, gpa: Allocator) void {
7798 for (self.notes) |*note| note.deinit(gpa);
7899 gpa.free(self.notes);
......@@ -326,16 +347,16 @@ pub const Diags = struct {
326347 diags.flags.alloc_failure_occurred = true;
327348 }
328349
329 pub fn addMessagesToBundle(diags: *const Diags, bundle: *std.zig.ErrorBundle.Wip) Allocator.Error!void {
350 pub fn addMessagesToBundle(diags: *const Diags, bundle: *std.zig.ErrorBundle.Wip, base: ?*File) Allocator.Error!void {
330351 for (diags.msgs.items) |link_err| {
331352 try bundle.addRootErrorMessage(.{
332 .msg = try bundle.addString(link_err.msg),
353 .msg = try link_err.string(bundle, base),
333354 .notes_len = @intCast(link_err.notes.len),
334355 });
335356 const notes_start = try bundle.reserveNotes(@intCast(link_err.notes.len));
336357 for (link_err.notes, 0..) |note, i| {
337358 bundle.extra.items[notes_start + i] = @intFromEnum(try bundle.addErrorMessage(.{
338 .msg = try bundle.addString(note.msg),
359 .msg = try note.string(bundle, base),
339360 }));
340361 }
341362 }
......@@ -2224,7 +2245,7 @@ fn resolvePathInputLib(
22242245 try wip_errors.init(gpa);
22252246 defer wip_errors.deinit();
22262247
2227 try diags.addMessagesToBundle(&wip_errors);
2248 try diags.addMessagesToBundle(&wip_errors, null);
22282249
22292250 var error_bundle = try wip_errors.toOwnedBundle("");
22302251 defer error_bundle.deinit(gpa);
src/link/Wasm.zig+28-6
......@@ -465,17 +465,33 @@ pub const SourceLocation = enum(u32) {
465465
466466 pub fn addNote(
467467 sl: SourceLocation,
468 wasm: *Wasm,
469468 err: *link.Diags.ErrorWithNotes,
470469 comptime f: []const u8,
471470 args: anytype,
472471 ) void {
473 switch (sl.unpack(wasm)) {
474 .none => err.addNote(f, args),
475 .zig_object_nofile => err.addNote("zig compilation unit: " ++ f, args),
476 .object_index => |i| err.addNote("{}: " ++ f, .{i.ptr(wasm).path} ++ args),
472 err.addNote(f, args);
473 const err_msg = &err.diags.msgs.items[err.index];
474 err_msg.notes[err.note_slot - 1].source_location = .{ .wasm = sl };
475 }
476
477 pub fn string(
478 sl: SourceLocation,
479 msg: []const u8,
480 bundle: *std.zig.ErrorBundle.Wip,
481 wasm: *const Wasm,
482 ) Allocator.Error!std.zig.ErrorBundle.String {
483 return switch (sl.unpack(wasm)) {
484 .none => try bundle.addString(msg),
485 .zig_object_nofile => try bundle.printString("zig compilation unit: {s}", .{msg}),
486 .object_index => |i| {
487 const obj = i.ptr(wasm);
488 return if (obj.archive_member_name.slice(wasm)) |obj_name|
489 try bundle.printString("{} ({s}): {s}", .{ obj.path, std.fs.path.basename(obj_name), msg })
490 else
491 try bundle.printString("{}: {s}", .{ obj.path, msg });
492 },
477493 .source_location_index => @panic("TODO"),
478 }
494 };
479495 }
480496};
481497
......@@ -3679,6 +3695,12 @@ fn defaultEntrySymbolName(
36793695 };
36803696}
36813697
3698pub fn internOptionalString(wasm: *Wasm, optional_bytes: ?[]const u8) Allocator.Error!OptionalString {
3699 const bytes = optional_bytes orelse return .none;
3700 const string = try internString(wasm, bytes);
3701 return string.toOptional();
3702}
3703
36823704pub fn internString(wasm: *Wasm, bytes: []const u8) Allocator.Error!String {
36833705 assert(mem.indexOfScalar(u8, bytes, 0) == null);
36843706 wasm.string_bytes_lock.lock();
src/link/Wasm/Object.zig+23-23
......@@ -17,7 +17,7 @@ path: Path,
1717/// For error reporting purposes only.
1818/// If this represents an object in an archive, it's the basename of the
1919/// object, and path refers to the archive.
20archive_member_name: ?[]const u8,
20archive_member_name: Wasm.OptionalString,
2121/// Represents the function ID that must be called on startup.
2222/// This is `null` by default as runtimes may determine the startup
2323/// function themselves. This is essentially legacy.
......@@ -965,21 +965,21 @@ pub fn parse(
965965 if (gop.value_ptr.type != fn_ty_index) {
966966 var err = try diags.addErrorWithNotes(2);
967967 try err.addMsg("symbol '{s}' mismatching function signatures", .{name.slice(wasm)});
968 gop.value_ptr.source_location.addNote(wasm, &err, "imported as {} here", .{
968 gop.value_ptr.source_location.addNote(&err, "imported as {} here", .{
969969 gop.value_ptr.type.fmt(wasm),
970970 });
971 err.addNote("{}: imported as {} here", .{ path, fn_ty_index.fmt(wasm) });
971 source_location.addNote(&err, "imported as {} here", .{fn_ty_index.fmt(wasm)});
972972 continue;
973973 }
974974 if (gop.value_ptr.module_name != ptr.module_name.toOptional()) {
975975 var err = try diags.addErrorWithNotes(2);
976976 try err.addMsg("symbol '{s}' mismatching module names", .{name.slice(wasm)});
977977 if (gop.value_ptr.module_name.slice(wasm)) |module_name| {
978 gop.value_ptr.source_location.addNote(wasm, &err, "module '{s}' here", .{module_name});
978 gop.value_ptr.source_location.addNote(&err, "module '{s}' here", .{module_name});
979979 } else {
980 gop.value_ptr.source_location.addNote(wasm, &err, "no module here", .{});
980 gop.value_ptr.source_location.addNote(&err, "no module here", .{});
981981 }
982 err.addNote("{}: module '{s}' here", .{ path, ptr.module_name.slice(wasm) });
982 source_location.addNote(&err, "module '{s}' here", .{ptr.module_name.slice(wasm)});
983983 continue;
984984 }
985985 if (symbol.flags.binding == .strong) gop.value_ptr.flags.binding = .strong;
......@@ -1008,18 +1008,18 @@ pub fn parse(
10081008 if (ptr.valtype != existing_ty.valtype) {
10091009 var err = try diags.addErrorWithNotes(2);
10101010 try err.addMsg("symbol '{s}' mismatching global types", .{name.slice(wasm)});
1011 gop.value_ptr.source_location.addNote(wasm, &err, "type {s} here", .{@tagName(existing_ty.valtype)});
1012 err.addNote("{}: type {s} here", .{ path, @tagName(ptr.valtype) });
1011 gop.value_ptr.source_location.addNote(&err, "type {s} here", .{@tagName(existing_ty.valtype)});
1012 source_location.addNote(&err, "type {s} here", .{@tagName(ptr.valtype)});
10131013 continue;
10141014 }
10151015 if (ptr.mutable != existing_ty.mutable) {
10161016 var err = try diags.addErrorWithNotes(2);
10171017 try err.addMsg("symbol '{s}' mismatching global mutability", .{name.slice(wasm)});
1018 gop.value_ptr.source_location.addNote(wasm, &err, "{s} here", .{
1018 gop.value_ptr.source_location.addNote(&err, "{s} here", .{
10191019 if (existing_ty.mutable) "mutable" else "not mutable",
10201020 });
1021 err.addNote("{}: {s} here", .{
1022 path, if (ptr.mutable) "mutable" else "not mutable",
1021 source_location.addNote(&err, "{s} here", .{
1022 if (ptr.mutable) "mutable" else "not mutable",
10231023 });
10241024 continue;
10251025 }
......@@ -1027,11 +1027,11 @@ pub fn parse(
10271027 var err = try diags.addErrorWithNotes(2);
10281028 try err.addMsg("symbol '{s}' mismatching module names", .{name.slice(wasm)});
10291029 if (gop.value_ptr.module_name.slice(wasm)) |module_name| {
1030 gop.value_ptr.source_location.addNote(wasm, &err, "module '{s}' here", .{module_name});
1030 gop.value_ptr.source_location.addNote(&err, "module '{s}' here", .{module_name});
10311031 } else {
1032 gop.value_ptr.source_location.addNote(wasm, &err, "no module here", .{});
1032 gop.value_ptr.source_location.addNote(&err, "no module here", .{});
10331033 }
1034 err.addNote("{}: module '{s}' here", .{ path, ptr.module_name.slice(wasm) });
1034 source_location.addNote(&err, "module '{s}' here", .{ptr.module_name.slice(wasm)});
10351035 continue;
10361036 }
10371037 if (symbol.flags.binding == .strong) gop.value_ptr.flags.binding = .strong;
......@@ -1063,17 +1063,17 @@ pub fn parse(
10631063 if (ptr.ref_type != existing_reftype) {
10641064 var err = try diags.addErrorWithNotes(2);
10651065 try err.addMsg("symbol '{s}' mismatching table reftypes", .{name.slice(wasm)});
1066 gop.value_ptr.source_location.addNote(wasm, &err, "{s} here", .{@tagName(existing_reftype)});
1067 err.addNote("{}: {s} here", .{ path, @tagName(ptr.ref_type) });
1066 gop.value_ptr.source_location.addNote(&err, "{s} here", .{@tagName(existing_reftype)});
1067 source_location.addNote(&err, "{s} here", .{@tagName(ptr.ref_type)});
10681068 continue;
10691069 }
10701070 if (gop.value_ptr.module_name != ptr.module_name) {
10711071 var err = try diags.addErrorWithNotes(2);
10721072 try err.addMsg("symbol '{s}' mismatching module names", .{name.slice(wasm)});
1073 gop.value_ptr.source_location.addNote(wasm, &err, "module '{s}' here", .{
1073 gop.value_ptr.source_location.addNote(&err, "module '{s}' here", .{
10741074 gop.value_ptr.module_name.slice(wasm),
10751075 });
1076 err.addNote("{}: module '{s}' here", .{ path, ptr.module_name.slice(wasm) });
1076 source_location.addNote(&err, "module '{s}' here", .{ptr.module_name.slice(wasm)});
10771077 continue;
10781078 }
10791079 if (symbol.flags.binding == .strong) gop.value_ptr.flags.binding = .strong;
......@@ -1105,11 +1105,11 @@ pub fn parse(
11051105 if (gop.value_ptr.type != ptr.type_index) {
11061106 var err = try diags.addErrorWithNotes(2);
11071107 try err.addMsg("function signature mismatch: {s}", .{name.slice(wasm)});
1108 gop.value_ptr.source_location.addNote(wasm, &err, "exported as {} here", .{
1108 gop.value_ptr.source_location.addNote(&err, "exported as {} here", .{
11091109 ptr.type_index.fmt(wasm),
11101110 });
11111111 const word = if (gop.value_ptr.resolution == .unresolved) "imported" else "exported";
1112 err.addNote("{}: {s} as {} here", .{ path, word, gop.value_ptr.type.fmt(wasm) });
1112 source_location.addNote(&err, "{s} as {} here", .{ word, gop.value_ptr.type.fmt(wasm) });
11131113 continue;
11141114 }
11151115 if (gop.value_ptr.resolution == .unresolved or gop.value_ptr.flags.binding == .weak) {
......@@ -1121,8 +1121,8 @@ pub fn parse(
11211121 }
11221122 var err = try diags.addErrorWithNotes(2);
11231123 try err.addMsg("symbol collision: {s}", .{name.slice(wasm)});
1124 gop.value_ptr.source_location.addNote(wasm, &err, "exported as {} here", .{ptr.type_index.fmt(wasm)});
1125 err.addNote("{}: exported as {} here", .{ path, gop.value_ptr.type.fmt(wasm) });
1124 gop.value_ptr.source_location.addNote(&err, "exported as {} here", .{ptr.type_index.fmt(wasm)});
1125 source_location.addNote(&err, "exported as {} here", .{gop.value_ptr.type.fmt(wasm)});
11261126 continue;
11271127 } else {
11281128 gop.value_ptr.* = .{
......@@ -1242,7 +1242,7 @@ pub fn parse(
12421242 return .{
12431243 .version = version,
12441244 .path = path,
1245 .archive_member_name = archive_member_name,
1245 .archive_member_name = try wasm.internOptionalString(archive_member_name),
12461246 .start_function = start_function,
12471247 .features = features,
12481248 .functions = .{