From bf61c5c0656e3b2198fb009fe5cc59f55263ceae Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Tue, 24 Jan 2023 13:35:10 +0200 Subject: make distinct error limit configurable Closes #786 --- src/value.zig | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src/value.zig') diff --git a/src/value.zig b/src/value.zig index 2c4a88da17..15289de6b2 100644 --- a/src/value.zig +++ b/src/value.zig @@ -701,15 +701,20 @@ pub const Value = struct { } }, .ErrorSet => { - // TODO revisit this when we have the concept of the error tag type - const Int = u16; + const bits = mod.errorSetBits(); + const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8); + const name = switch (ip.indexToKey(val.toIntern())) { .err => |err| err.name, .error_union => |error_union| error_union.val.err_name, else => unreachable, }; - const int = @as(Module.ErrorInt, @intCast(mod.global_error_set.getIndex(name).?)); - std.mem.writeInt(Int, buffer[0..@sizeOf(Int)], @as(Int, @intCast(int)), endian); + var bigint_buffer: BigIntSpace = undefined; + const bigint = BigIntMutable.init( + &bigint_buffer.limbs, + mod.global_error_set.getIndex(name).?, + ).toConst(); + bigint.writeTwosComplement(buffer[0..byte_count], endian); }, .Union => switch (ty.containerLayout(mod)) { .Auto => return error.IllDefinedMemoryLayout, // Sema is supposed to have emitted a compile error already @@ -987,10 +992,12 @@ pub const Value = struct { } }, .ErrorSet => { - // TODO revisit this when we have the concept of the error tag type - const Int = u16; - const int = std.mem.readInt(Int, buffer[0..@sizeOf(Int)], endian); - const name = mod.global_error_set.keys()[@as(usize, @intCast(int))]; + const bits = mod.errorSetBits(); + const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8); + const int = std.mem.readVarInt(u64, buffer[0..byte_count], endian); + const index = (int << @as(u6, @intCast(64 - bits))) >> @as(u6, @intCast(64 - bits)); + const name = mod.global_error_set.keys()[@intCast(index)]; + return (try mod.intern(.{ .err = .{ .ty = ty.toIntern(), .name = name, -- cgit v1.2.3