authorgravatar for will.lillis24@gmail.comWill Lillis <will.lillis24@gmail.com> 2025-08-10 20:14:51-04:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-08-15 07:43:46+01:00
loge9eee8dace5af05f0c47f29443542783ed5fa080
treeab44521cae34e65c3ee166a9e5bc2248246cba47
parente395c24c6dc4b8ad7688c1ad7754697166551bcb

fix: print error set members in a consistent order

Co-authored-by: Matthew Lugg <mlugg@mlugg.co.uk>

4 files changed, 33 insertions(+), 8 deletions(-)

src/Type.zig+16-2
......@@ -254,9 +254,23 @@ pub fn print(ty: Type, writer: *std.io.Writer, pt: Zcu.PerThread) std.io.Writer.
254254 });
255255 },
256256 .error_set_type => |error_set_type| {
257 const names = error_set_type.names;
257 const NullTerminatedString = InternPool.NullTerminatedString;
258 const sorted_names = zcu.gpa.dupe(NullTerminatedString, error_set_type.names.get(ip)) catch {
259 zcu.comp.setAllocFailure();
260 return writer.writeAll("error{...}");
261 };
262 defer zcu.gpa.free(sorted_names);
263
264 std.mem.sortUnstable(NullTerminatedString, sorted_names, ip, struct {
265 fn lessThan(ip_: *InternPool, lhs: NullTerminatedString, rhs: NullTerminatedString) bool {
266 const lhs_slice = lhs.toSlice(ip_);
267 const rhs_slice = rhs.toSlice(ip_);
268 return std.mem.lessThan(u8, lhs_slice, rhs_slice);
269 }
270 }.lessThan);
271
258272 try writer.writeAll("error{");
259 for (names.get(ip), 0..) |name, i| {
273 for (sorted_names, 0..) |name, i| {
260274 if (i != 0) try writer.writeByte(',');
261275 try writer.print("{f}", .{name.fmt(ip)});
262276 }
test/cases/compile_errors/error_set_display.zig created+11
......@@ -0,0 +1,11 @@
1const Set0 = error{ A, B, C, D, E, F };
2const Set1 = error{ F, E, D, C, A };
3comptime {
4 const x = Set0.B;
5 const y: Set1 = @errorCast(x);
6 _ = y;
7}
8
9// error
10//
11// :5:21: error: 'error.B' not a member of error set 'error{A,C,D,E,F}'
test/cases/compile_errors/invalid_pointer_coercions.zig+2-2
......@@ -54,8 +54,8 @@ export fn ptr_to_underaligned_ptr() void {
5454
5555// error
5656//
57// :16:33: error: expected type '*error{Foo,Bar}', found '*error{Foo}'
58// :16:33: note: pointer type child 'error{Foo}' cannot cast into pointer type child 'error{Foo,Bar}'
57// :16:33: error: expected type '*error{Bar,Foo}', found '*error{Foo}'
58// :16:33: note: pointer type child 'error{Foo}' cannot cast into pointer type child 'error{Bar,Foo}'
5959// :16:33: note: 'error.Bar' not a member of destination error set
6060// :22:24: error: expected type '*anyerror', found '*error{Foo}'
6161// :22:24: note: pointer type child 'error{Foo}' cannot cast into pointer type child 'anyerror'
test/src/Debugger.zig+4-4
......@@ -491,16 +491,16 @@ pub fn addTestsForTarget(db: *Debugger, target: *const Target) void {
491491 \\ (error{One,Two}) Two = error.Two
492492 \\ }
493493 \\ (type) Three = error {
494 \\ (error{One,Two,Three}) One = error.One
495 \\ (error{One,Two,Three}) Two = error.Two
496 \\ (error{One,Two,Three}) Three = error.Three
494 \\ (error{One,Three,Two}) One = error.One
495 \\ (error{One,Three,Two}) Two = error.Two
496 \\ (error{One,Three,Two}) Three = error.Three
497497 \\ }
498498 \\}
499499 \\(lldb) frame variable --show-types -- errors
500500 \\(root.errors.Errors) errors = {
501501 \\ (error{One}) .one = error.One
502502 \\ (error{One,Two}) .two = error.Two
503 \\ (error{One,Two,Three}) .three = error.Three
503 \\ (error{One,Three,Two}) .three = error.Three
504504 \\ (anyerror) .any = error.Any
505505 \\ (anyerror!void) .any_void = {
506506 \\ (anyerror) .error = error.NotVoid