authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-10-21 12:56:05+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-10-22 14:29:26+03:00
log9d9e22e716a4b6de9a26f2553ea5c980efc79d4a
tree7f66f1f1026ab48ca42836ef95a86c9edf2416a0
parentbf61c5c0656e3b2198fb009fe5cc59f55263ceae

remove uses of non-configurable `err_int`


11 files changed, 111 insertions(+), 80 deletions(-)

doc/langref.html.in+2-2
......@@ -8373,7 +8373,7 @@ test "main" {
83738373 {#header_close#}
83748374
83758375 {#header_open|@errorFromInt#}
8376 <pre>{#syntax#}@errorFromInt(value: std.meta.Int(.unsigned, @sizeOf(anyerror) * 8)) anyerror{#endsyntax#}</pre>
8376 <pre>{#syntax#}@errorFromInt(value: std.meta.Int(.unsigned, @bitSizeOf(anyerror))) anyerror{#endsyntax#}</pre>
83778377 <p>
83788378 Converts from the integer representation of an error into {#link|The Global Error Set#} type.
83798379 </p>
......@@ -8694,7 +8694,7 @@ test "integer cast panic" {
86948694 {#header_close#}
86958695
86968696 {#header_open|@intFromError#}
8697 <pre>{#syntax#}@intFromError(err: anytype) std.meta.Int(.unsigned, @sizeOf(anyerror) * 8){#endsyntax#}</pre>
8697 <pre>{#syntax#}@intFromError(err: anytype) std.meta.Int(.unsigned, @bitSizeOf(anyerror)){#endsyntax#}</pre>
86988698 <p>
86998699 Supports the following types:
87008700 </p>
src/AstGen.zig+1-1
......@@ -8430,7 +8430,7 @@ fn builtinCall(
84308430 return rvalue(gz, ri, result, node);
84318431 },
84328432 .error_from_int => {
8433 const operand = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, params[0]);
8433 const operand = try expr(gz, scope, .{ .rl = .none }, params[0]);
84348434 const result = try gz.addExtendedPayload(.error_from_int, Zir.Inst.UnNode{
84358435 .node = gz.nodeIndexToRelative(node),
84368436 .operand = operand,
src/Module.zig+4
......@@ -5906,6 +5906,10 @@ pub fn intType(mod: *Module, signedness: std.builtin.Signedness, bits: u16) Allo
59065906 } })).toType();
59075907}
59085908
5909pub fn errorIntType(mod: *Module) std.mem.Allocator.Error!Type {
5910 return mod.intType(.unsigned, mod.errorSetBits());
5911}
5912
59095913pub fn arrayType(mod: *Module, info: InternPool.Key.ArrayType) Allocator.Error!Type {
59105914 const i = try intern(mod, .{ .array_type = info });
59115915 return i.toType();
src/Sema.zig+12-9
......@@ -8384,14 +8384,15 @@ fn zirIntFromError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
83848384 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
83858385 const uncasted_operand = try sema.resolveInst(extra.operand);
83868386 const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);
8387 const err_int_ty = try mod.errorIntType();
83878388
83888389 if (try sema.resolveMaybeUndefVal(operand)) |val| {
83898390 if (val.isUndef(mod)) {
8390 return mod.undefRef(Type.err_int);
8391 return mod.undefRef(err_int_ty);
83918392 }
83928393 const err_name = ip.indexToKey(val.toIntern()).err.name;
83938394 return Air.internedToRef((try mod.intValue(
8394 Type.err_int,
8395 err_int_ty,
83958396 try mod.getErrorValue(err_name),
83968397 )).toIntern());
83978398 }
......@@ -8402,10 +8403,10 @@ fn zirIntFromError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
84028403 else => |err_set_ty_index| {
84038404 const names = ip.indexToKey(err_set_ty_index).error_set_type.names;
84048405 switch (names.len) {
8405 0 => return Air.internedToRef((try mod.intValue(Type.err_int, 0)).toIntern()),
8406 0 => return Air.internedToRef((try mod.intValue(err_int_ty, 0)).toIntern()),
84068407 1 => {
84078408 const int: Module.ErrorInt = @intCast(mod.global_error_set.getIndex(names.get(ip)[0]).?);
8408 return mod.intRef(Type.err_int, int);
8409 return mod.intRef(err_int_ty, int);
84098410 },
84108411 else => {},
84118412 }
......@@ -8413,7 +8414,7 @@ fn zirIntFromError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
84138414 }
84148415
84158416 try sema.requireRuntimeBlock(block, src, operand_src);
8416 return block.addBitCast(Type.err_int, operand);
8417 return block.addBitCast(err_int_ty, operand);
84178418}
84188419
84198420fn zirErrorFromInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
......@@ -8425,7 +8426,8 @@ fn zirErrorFromInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
84258426 const src = LazySrcLoc.nodeOffset(extra.node);
84268427 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
84278428 const uncasted_operand = try sema.resolveInst(extra.operand);
8428 const operand = try sema.coerce(block, Type.err_int, uncasted_operand, operand_src);
8429 const err_int_ty = try mod.errorIntType();
8430 const operand = try sema.coerce(block, err_int_ty, uncasted_operand, operand_src);
84298431
84308432 if (try sema.resolveDefinedValue(block, operand_src, operand)) |value| {
84318433 const int = try sema.usizeCast(block, operand_src, value.toUnsignedInt(mod));
......@@ -8439,7 +8441,7 @@ fn zirErrorFromInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD
84398441 try sema.requireRuntimeBlock(block, src, operand_src);
84408442 if (block.wantSafety()) {
84418443 const is_lt_len = try block.addUnOp(.cmp_lt_errors_len, operand);
8442 const zero_val = Air.internedToRef((try mod.intValue(Type.err_int, 0)).toIntern());
8444 const zero_val = Air.internedToRef((try mod.intValue(err_int_ty, 0)).toIntern());
84438445 const is_non_zero = try block.addBinOp(.cmp_neq, operand, zero_val);
84448446 const ok = try block.addBinOp(.bool_and, is_lt_len, is_non_zero);
84458447 try sema.addSafetyCheck(block, src, ok, .invalid_error_code);
......@@ -21899,10 +21901,11 @@ fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData
2189921901 }
2190021902
2190121903 try sema.requireRuntimeBlock(block, src, operand_src);
21904 const err_int_ty = try mod.errorIntType();
2190221905 if (block.wantSafety() and !dest_ty.isAnyError(mod) and sema.mod.backendSupportsFeature(.error_set_has_value)) {
2190321906 if (dest_tag == .ErrorUnion) {
2190421907 const err_code = try sema.analyzeErrUnionCode(block, operand_src, operand);
21905 const err_int = try block.addBitCast(Type.err_int, err_code);
21908 const err_int = try block.addBitCast(err_int_ty, err_code);
2190621909 const zero_u16 = Air.internedToRef(try mod.intern(.{
2190721910 .int = .{ .ty = .u16_type, .storage = .{ .u64 = 0 } },
2190821911 }));
......@@ -21918,7 +21921,7 @@ fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData
2191821921 try sema.addSafetyCheck(block, src, ok, .invalid_error_code);
2191921922 }
2192021923 } else {
21921 const err_int_inst = try block.addBitCast(Type.err_int, operand);
21924 const err_int_inst = try block.addBitCast(err_int_ty, operand);
2192221925 const ok = try block.addTyOp(.error_set_has_value, dest_ty, err_int_inst);
2192321926 try sema.addSafetyCheck(block, src, ok, .invalid_error_code);
2192421927 }
src/arch/wasm/CodeGen.zig+7-4
......@@ -3296,6 +3296,7 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
32963296 return WValue{ .imm32 = int };
32973297 },
32983298 .error_union => |error_union| {
3299 const err_int_ty = try mod.errorIntType();
32993300 const err_tv: TypedValue = switch (error_union.val) {
33003301 .err_name => |err_name| .{
33013302 .ty = ty.errorUnionSet(mod),
......@@ -3305,8 +3306,8 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
33053306 } })).toValue(),
33063307 },
33073308 .payload => .{
3308 .ty = Type.err_int,
3309 .val = try mod.intValue(Type.err_int, 0),
3309 .ty = err_int_ty,
3310 .val = try mod.intValue(err_int_ty, 0),
33103311 },
33113312 };
33123313 const payload_type = ty.errorUnionPayload(mod);
......@@ -3705,8 +3706,10 @@ fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
37053706 const errors_len = WValue{ .memory = sym_index };
37063707
37073708 try func.emitWValue(operand);
3708 const errors_len_val = try func.load(errors_len, Type.err_int, 0);
3709 const result = try func.cmp(.stack, errors_len_val, Type.err_int, .lt);
3709 const mod = func.bin_file.base.options.module.?;
3710 const err_int_ty = try mod.errorIntType();
3711 const errors_len_val = try func.load(errors_len, err_int_ty, 0);
3712 const result = try func.cmp(.stack, errors_len_val, err_int_ty, .lt);
37103713
37113714 return func.finishAir(inst, try result.toLocal(func, Type.bool), &.{un_op});
37123715}
src/codegen.zig+3-2
......@@ -1052,6 +1052,7 @@ pub fn genTypedValue(
10521052 const payload_type = typed_value.ty.errorUnionPayload(mod);
10531053 if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) {
10541054 // We use the error type directly as the type.
1055 const err_int_ty = try mod.errorIntType();
10551056 switch (mod.intern_pool.indexToKey(typed_value.val.toIntern()).error_union.val) {
10561057 .err_name => |err_name| return genTypedValue(bin_file, src_loc, .{
10571058 .ty = err_type,
......@@ -1061,8 +1062,8 @@ pub fn genTypedValue(
10611062 } })).toValue(),
10621063 }, owner_decl_index),
10631064 .payload => return genTypedValue(bin_file, src_loc, .{
1064 .ty = Type.err_int,
1065 .val = try mod.intValue(Type.err_int, 0),
1065 .ty = err_int_ty,
1066 .val = try mod.intValue(err_int_ty, 0),
10661067 }, owner_decl_index),
10671068 }
10681069 }
src/codegen/c.zig+20-13
......@@ -1021,6 +1021,7 @@ pub const DeclGen = struct {
10211021 .error_union => |error_union| {
10221022 const payload_ty = ty.errorUnionPayload(mod);
10231023 const error_ty = ty.errorUnionSet(mod);
1024 const err_int_ty = try mod.errorIntType();
10241025 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
10251026 switch (error_union.val) {
10261027 .err_name => |err_name| return dg.renderValue(
......@@ -1034,8 +1035,8 @@ pub const DeclGen = struct {
10341035 ),
10351036 .payload => return dg.renderValue(
10361037 writer,
1037 Type.err_int,
1038 try mod.intValue(Type.err_int, 0),
1038 err_int_ty,
1039 try mod.intValue(err_int_ty, 0),
10391040 location,
10401041 ),
10411042 }
......@@ -1070,8 +1071,8 @@ pub const DeclGen = struct {
10701071 ),
10711072 .payload => try dg.renderValue(
10721073 writer,
1073 Type.err_int,
1074 try mod.intValue(Type.err_int, 0),
1074 err_int_ty,
1075 try mod.intValue(err_int_ty, 0),
10751076 location,
10761077 ),
10771078 }
......@@ -1227,7 +1228,7 @@ pub const DeclGen = struct {
12271228 payload_ty,
12281229 switch (opt.val) {
12291230 .none => switch (payload_ty.zigTypeTag(mod)) {
1230 .ErrorSet => try mod.intValue(Type.err_int, 0),
1231 .ErrorSet => try mod.intValue(try mod.errorIntType(), 0),
12311232 .Pointer => try mod.getCoerced(val, payload_ty),
12321233 else => unreachable,
12331234 },
......@@ -5179,6 +5180,7 @@ fn airIsNull(
51795180 const operand_ty = f.typeOf(un_op);
51805181 const optional_ty = if (is_ptr) operand_ty.childType(mod) else operand_ty;
51815182 const payload_ty = optional_ty.optionalChild(mod);
5183 const err_int_ty = try mod.errorIntType();
51825184
51835185 const rhs = if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod))
51845186 TypedValue{ .ty = Type.bool, .val = Value.true }
......@@ -5186,7 +5188,7 @@ fn airIsNull(
51865188 // operand is a regular pointer, test `operand !=/== NULL`
51875189 TypedValue{ .ty = optional_ty, .val = try mod.getCoerced(Value.null, optional_ty) }
51885190 else if (payload_ty.zigTypeTag(mod) == .ErrorSet)
5189 TypedValue{ .ty = Type.err_int, .val = try mod.intValue(Type.err_int, 0) }
5191 TypedValue{ .ty = err_int_ty, .val = try mod.intValue(err_int_ty, 0) }
51905192 else if (payload_ty.isSlice(mod) and optional_ty.optionalReprIsPayload(mod)) rhs: {
51915193 try writer.writeAll(".ptr");
51925194 const slice_ptr_ty = payload_ty.slicePtrFieldType(mod);
......@@ -5672,8 +5674,10 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
56725674 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" })
56735675 else
56745676 try f.writeCValueMember(writer, operand, .{ .identifier = "error" })
5675 else
5676 try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Initializer);
5677 else {
5678 const err_int_ty = try mod.errorIntType();
5679 try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Initializer);
5680 }
56775681 }
56785682 try writer.writeAll(";\n");
56795683 return local;
......@@ -5794,12 +5798,13 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
57945798 const error_union_ty = f.typeOf(ty_op.operand).childType(mod);
57955799
57965800 const payload_ty = error_union_ty.errorUnionPayload(mod);
5801 const err_int_ty = try mod.errorIntType();
57975802
57985803 // First, set the non-error value.
57995804 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
58005805 try f.writeCValueDeref(writer, operand);
58015806 try writer.writeAll(" = ");
5802 try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Other);
5807 try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other);
58035808 try writer.writeAll(";\n ");
58045809
58055810 return operand;
......@@ -5807,7 +5812,7 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
58075812 try reap(f, inst, &.{ty_op.operand});
58085813 try f.writeCValueDeref(writer, operand);
58095814 try writer.writeAll(".error = ");
5810 try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Other);
5815 try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other);
58115816 try writer.writeAll(";\n");
58125817
58135818 // Then return the payload pointer (only if it is used)
......@@ -5863,7 +5868,8 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
58635868 else
58645869 try f.writeCValueMember(writer, local, .{ .identifier = "error" });
58655870 try a.assign(f, writer);
5866 try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Other);
5871 const err_int_ty = try mod.errorIntType();
5872 try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other);
58675873 try a.end(f, writer);
58685874 }
58695875 return local;
......@@ -5885,6 +5891,7 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
58855891 try f.writeCValue(writer, local, .Other);
58865892 try writer.writeAll(" = ");
58875893
5894 const err_int_ty = try mod.errorIntType();
58885895 if (!error_ty.errorSetIsEmpty(mod))
58895896 if (payload_ty.hasRuntimeBits(mod))
58905897 if (is_ptr)
......@@ -5894,11 +5901,11 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
58945901 else
58955902 try f.writeCValue(writer, operand, .Other)
58965903 else
5897 try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Other);
5904 try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other);
58985905 try writer.writeByte(' ');
58995906 try writer.writeAll(operator);
59005907 try writer.writeByte(' ');
5901 try f.object.dg.renderValue(writer, Type.err_int, try mod.intValue(Type.err_int, 0), .Other);
5908 try f.object.dg.renderValue(writer, err_int_ty, try mod.intValue(err_int_ty, 0), .Other);
59025909 try writer.writeAll(";\n");
59035910 return local;
59045911}
src/codegen/llvm.zig+59-44
......@@ -1111,7 +1111,7 @@ pub const Object = struct {
11111111 // }
11121112
11131113 const lhs = wip.arg(0);
1114 const rhs = try o.builder.intValue(Builder.Type.err_int, errors_len);
1114 const rhs = try o.builder.intValue(try o.errorIntType(), errors_len);
11151115 const is_lt = try wip.icmp(.ult, lhs, rhs, "");
11161116 _ = try wip.ret(is_lt);
11171117 try wip.finish();
......@@ -3111,6 +3111,10 @@ pub const Object = struct {
31113111 return variable_index;
31123112 }
31133113
3114 fn errorIntType(o: *Object) Allocator.Error!Builder.Type {
3115 return o.builder.intType(o.module.errorSetBits());
3116 }
3117
31143118 fn lowerType(o: *Object, t: Type) Allocator.Error!Builder.Type {
31153119 const ty = try o.lowerTypeInner(t);
31163120 const mod = o.module;
......@@ -3182,7 +3186,7 @@ pub const Object = struct {
31823186 .bool_type => .i1,
31833187 .void_type => .void,
31843188 .type_type => unreachable,
3185 .anyerror_type => Builder.Type.err_int,
3189 .anyerror_type => try o.errorIntType(),
31863190 .comptime_int_type,
31873191 .comptime_float_type,
31883192 .noreturn_type,
......@@ -3203,7 +3207,7 @@ pub const Object = struct {
32033207 .optional_noreturn_type => unreachable,
32043208 .anyerror_void_error_union_type,
32053209 .adhoc_inferred_error_set_type,
3206 => Builder.Type.err_int,
3210 => try o.errorIntType(),
32073211 .generic_poison_type,
32083212 .empty_struct_type,
32093213 => unreachable,
......@@ -3272,16 +3276,17 @@ pub const Object = struct {
32723276 },
32733277 .anyframe_type => @panic("TODO implement lowerType for AnyFrame types"),
32743278 .error_union_type => |error_union_type| {
3275 const error_type = Builder.Type.err_int;
3279 const error_type = try o.errorIntType();
32763280 if (!error_union_type.payload_type.toType().hasRuntimeBitsIgnoreComptime(mod))
32773281 return error_type;
32783282 const payload_type = try o.lowerType(error_union_type.payload_type.toType());
3283 const err_int_ty = try mod.errorIntType();
32793284
32803285 const payload_align = error_union_type.payload_type.toType().abiAlignment(mod);
3281 const error_align = Type.err_int.abiAlignment(mod);
3286 const error_align = err_int_ty.abiAlignment(mod);
32823287
32833288 const payload_size = error_union_type.payload_type.toType().abiSize(mod);
3284 const error_size = Type.err_int.abiSize(mod);
3289 const error_size = err_int_ty.abiSize(mod);
32853290
32863291 var fields: [3]Builder.Type = undefined;
32873292 var fields_len: usize = 2;
......@@ -3542,7 +3547,7 @@ pub const Object = struct {
35423547 },
35433548 .enum_type => |enum_type| try o.lowerType(enum_type.tag_ty.toType()),
35443549 .func_type => |func_type| try o.lowerTypeFn(func_type),
3545 .error_set_type, .inferred_error_set_type => Builder.Type.err_int,
3550 .error_set_type, .inferred_error_set_type => try o.errorIntType(),
35463551 // values, not types
35473552 .undef,
35483553 .runtime_value,
......@@ -3725,7 +3730,7 @@ pub const Object = struct {
37253730 },
37263731 .err => |err| {
37273732 const int = try mod.getErrorValue(err.name);
3728 const llvm_int = try o.builder.intConst(Builder.Type.err_int, int);
3733 const llvm_int = try o.builder.intConst(try o.errorIntType(), int);
37293734 return llvm_int;
37303735 },
37313736 .error_union => |error_union| {
......@@ -3734,8 +3739,9 @@ pub const Object = struct {
37343739 .ty = ty.errorUnionSet(mod).toIntern(),
37353740 .name = err_name,
37363741 } }),
3737 .payload => (try mod.intValue(Type.err_int, 0)).toIntern(),
3742 .payload => (try mod.intValue(try mod.errorIntType(), 0)).toIntern(),
37383743 };
3744 const err_int_ty = try mod.errorIntType();
37393745 const payload_type = ty.errorUnionPayload(mod);
37403746 if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) {
37413747 // We use the error type directly as the type.
......@@ -3743,7 +3749,7 @@ pub const Object = struct {
37433749 }
37443750
37453751 const payload_align = payload_type.abiAlignment(mod);
3746 const error_align = Type.err_int.abiAlignment(mod);
3752 const error_align = err_int_ty.abiAlignment(mod);
37473753 const llvm_error_value = try o.lowerValue(err_val);
37483754 const llvm_payload_value = try o.lowerValue(switch (error_union.val) {
37493755 .err_name => try mod.intern(.{ .undef = payload_type.toIntern() }),
......@@ -4285,8 +4291,9 @@ pub const Object = struct {
42854291 return parent_ptr;
42864292 }
42874293
4294 const err_int_ty = try mod.errorIntType();
42884295 const payload_align = payload_ty.abiAlignment(mod);
4289 const err_align = Type.err_int.abiAlignment(mod);
4296 const err_align = err_int_ty.abiAlignment(mod);
42904297 const index: u32 = if (payload_align.compare(.gt, err_align)) 2 else 1;
42914298 return o.builder.gepConst(.inbounds, try o.lowerType(eu_ty), parent_ptr, null, &.{
42924299 try o.builder.intConst(.i32, 0), try o.builder.intConst(.i32, index),
......@@ -5399,7 +5406,7 @@ pub const FuncGen = struct {
53995406 // Functions with an empty error set are emitted with an error code
54005407 // return type and return zero so they can be function pointers coerced
54015408 // to functions that return anyerror.
5402 _ = try self.wip.ret(try o.builder.intValue(Builder.Type.err_int, 0));
5409 _ = try self.wip.ret(try o.builder.intValue(try o.errorIntType(), 0));
54035410 } else {
54045411 _ = try self.wip.retVoid();
54055412 }
......@@ -5441,7 +5448,7 @@ pub const FuncGen = struct {
54415448 // Functions with an empty error set are emitted with an error code
54425449 // return type and return zero so they can be function pointers coerced
54435450 // to functions that return anyerror.
5444 _ = try self.wip.ret(try o.builder.intValue(Builder.Type.err_int, 0));
5451 _ = try self.wip.ret(try o.builder.intValue(try o.errorIntType(), 0));
54455452 } else {
54465453 _ = try self.wip.retVoid();
54475454 }
......@@ -5788,24 +5795,25 @@ pub const FuncGen = struct {
57885795 const payload_ty = err_union_ty.errorUnionPayload(mod);
57895796 const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(mod);
57905797 const err_union_llvm_ty = try o.lowerType(err_union_ty);
5798 const error_type = try o.errorIntType();
57915799
57925800 if (!err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) {
57935801 const loaded = loaded: {
57945802 if (!payload_has_bits) {
57955803 // TODO add alignment to this load
57965804 break :loaded if (operand_is_ptr)
5797 try fg.wip.load(.normal, Builder.Type.err_int, err_union, .default, "")
5805 try fg.wip.load(.normal, error_type, err_union, .default, "")
57985806 else
57995807 err_union;
58005808 }
5801 const err_field_index = errUnionErrorOffset(payload_ty, mod);
5809 const err_field_index = try errUnionErrorOffset(payload_ty, mod);
58025810 if (operand_is_ptr or isByRef(err_union_ty, mod)) {
58035811 const err_field_ptr =
58045812 try fg.wip.gepStruct(err_union_llvm_ty, err_union, err_field_index, "");
58055813 // TODO add alignment to this load
58065814 break :loaded try fg.wip.load(
58075815 .normal,
5808 Builder.Type.err_int,
5816 error_type,
58095817 err_field_ptr,
58105818 .default,
58115819 "",
......@@ -5813,7 +5821,7 @@ pub const FuncGen = struct {
58135821 }
58145822 break :loaded try fg.wip.extractValue(err_union, &.{err_field_index}, "");
58155823 };
5816 const zero = try o.builder.intValue(Builder.Type.err_int, 0);
5824 const zero = try o.builder.intValue(error_type, 0);
58175825 const is_err = try fg.wip.icmp(.ne, loaded, zero, "");
58185826
58195827 const return_block = try fg.wip.block(1, "TryRet");
......@@ -5827,7 +5835,7 @@ pub const FuncGen = struct {
58275835 }
58285836 if (is_unused) return .none;
58295837 if (!payload_has_bits) return if (operand_is_ptr) err_union else .none;
5830 const offset = errUnionPayloadOffset(payload_ty, mod);
5838 const offset = try errUnionPayloadOffset(payload_ty, mod);
58315839 if (operand_is_ptr) {
58325840 return fg.wip.gepStruct(err_union_llvm_ty, err_union, offset, "");
58335841 } else if (isByRef(err_union_ty, mod)) {
......@@ -7053,7 +7061,8 @@ pub const FuncGen = struct {
70537061 const operand_ty = self.typeOf(un_op);
70547062 const err_union_ty = if (operand_is_ptr) operand_ty.childType(mod) else operand_ty;
70557063 const payload_ty = err_union_ty.errorUnionPayload(mod);
7056 const zero = try o.builder.intValue(Builder.Type.err_int, 0);
7064 const error_type = try o.errorIntType();
7065 const zero = try o.builder.intValue(error_type, 0);
70577066
70587067 if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) {
70597068 const val: Builder.Constant = switch (cond) {
......@@ -7072,13 +7081,13 @@ pub const FuncGen = struct {
70727081 return self.wip.icmp(cond, loaded, zero, "");
70737082 }
70747083
7075 const err_field_index = errUnionErrorOffset(payload_ty, mod);
7084 const err_field_index = try errUnionErrorOffset(payload_ty, mod);
70767085
70777086 const loaded = if (operand_is_ptr or isByRef(err_union_ty, mod)) loaded: {
70787087 const err_union_llvm_ty = try o.lowerType(err_union_ty);
70797088 const err_field_ptr =
70807089 try self.wip.gepStruct(err_union_llvm_ty, operand, err_field_index, "");
7081 break :loaded try self.wip.load(.normal, Builder.Type.err_int, err_field_ptr, .default, "");
7090 break :loaded try self.wip.load(.normal, error_type, err_field_ptr, .default, "");
70827091 } else try self.wip.extractValue(operand, &.{err_field_index}, "");
70837092 return self.wip.icmp(cond, loaded, zero, "");
70847093 }
......@@ -7173,7 +7182,7 @@ pub const FuncGen = struct {
71737182 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
71747183 return if (operand_is_ptr) operand else .none;
71757184 }
7176 const offset = errUnionPayloadOffset(payload_ty, mod);
7185 const offset = try errUnionPayloadOffset(payload_ty, mod);
71777186 const err_union_llvm_ty = try o.lowerType(err_union_ty);
71787187 if (operand_is_ptr) {
71797188 return self.wip.gepStruct(err_union_llvm_ty, operand, offset, "");
......@@ -7200,27 +7209,28 @@ pub const FuncGen = struct {
72007209 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
72017210 const operand = try self.resolveInst(ty_op.operand);
72027211 const operand_ty = self.typeOf(ty_op.operand);
7212 const error_type = try o.errorIntType();
72037213 const err_union_ty = if (operand_is_ptr) operand_ty.childType(mod) else operand_ty;
72047214 if (err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) {
72057215 if (operand_is_ptr) {
72067216 return operand;
72077217 } else {
7208 return o.builder.intValue(Builder.Type.err_int, 0);
7218 return o.builder.intValue(error_type, 0);
72097219 }
72107220 }
72117221
72127222 const payload_ty = err_union_ty.errorUnionPayload(mod);
72137223 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
72147224 if (!operand_is_ptr) return operand;
7215 return self.wip.load(.normal, Builder.Type.err_int, operand, .default, "");
7225 return self.wip.load(.normal, error_type, operand, .default, "");
72167226 }
72177227
7218 const offset = errUnionErrorOffset(payload_ty, mod);
7228 const offset = try errUnionErrorOffset(payload_ty, mod);
72197229
72207230 if (operand_is_ptr or isByRef(err_union_ty, mod)) {
72217231 const err_union_llvm_ty = try o.lowerType(err_union_ty);
72227232 const err_field_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, offset, "");
7223 return self.wip.load(.normal, Builder.Type.err_int, err_field_ptr, .default, "");
7233 return self.wip.load(.normal, error_type, err_field_ptr, .default, "");
72247234 }
72257235
72267236 return self.wip.extractValue(operand, &.{offset}, "");
......@@ -7234,15 +7244,16 @@ pub const FuncGen = struct {
72347244 const err_union_ty = self.typeOf(ty_op.operand).childType(mod);
72357245
72367246 const payload_ty = err_union_ty.errorUnionPayload(mod);
7237 const non_error_val = try o.builder.intValue(Builder.Type.err_int, 0);
7247 const non_error_val = try o.builder.intValue(try o.errorIntType(), 0);
72387248 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
72397249 _ = try self.wip.store(.normal, non_error_val, operand, .default);
72407250 return operand;
72417251 }
72427252 const err_union_llvm_ty = try o.lowerType(err_union_ty);
72437253 {
7244 const error_alignment = Type.err_int.abiAlignment(mod).toLlvm();
7245 const error_offset = errUnionErrorOffset(payload_ty, mod);
7254 const err_int_ty = try mod.errorIntType();
7255 const error_alignment = err_int_ty.abiAlignment(mod).toLlvm();
7256 const error_offset = try errUnionErrorOffset(payload_ty, mod);
72467257 // First set the non-error value.
72477258 const non_null_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, error_offset, "");
72487259 _ = try self.wip.store(.normal, non_error_val, non_null_ptr, error_alignment);
......@@ -7250,7 +7261,7 @@ pub const FuncGen = struct {
72507261 // Then return the payload pointer (only if it is used).
72517262 if (self.liveness.isUnused(inst)) return .none;
72527263
7253 const payload_offset = errUnionPayloadOffset(payload_ty, mod);
7264 const payload_offset = try errUnionPayloadOffset(payload_ty, mod);
72547265 return self.wip.gepStruct(err_union_llvm_ty, operand, payload_offset, "");
72557266 }
72567267
......@@ -7353,11 +7364,11 @@ pub const FuncGen = struct {
73537364 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) {
73547365 return operand;
73557366 }
7356 const ok_err_code = try o.builder.intValue(Builder.Type.err_int, 0);
7367 const ok_err_code = try o.builder.intValue(try o.errorIntType(), 0);
73577368 const err_un_llvm_ty = try o.lowerType(err_un_ty);
73587369
7359 const payload_offset = errUnionPayloadOffset(payload_ty, mod);
7360 const error_offset = errUnionErrorOffset(payload_ty, mod);
7370 const payload_offset = try errUnionPayloadOffset(payload_ty, mod);
7371 const error_offset = try errUnionErrorOffset(payload_ty, mod);
73617372 if (isByRef(err_un_ty, mod)) {
73627373 const directReturn = self.isNextRet(body_tail);
73637374 const result_ptr = if (directReturn)
......@@ -7369,7 +7380,8 @@ pub const FuncGen = struct {
73697380 };
73707381
73717382 const err_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, error_offset, "");
7372 const error_alignment = Type.err_int.abiAlignment(mod).toLlvm();
7383 const err_int_ty = try mod.errorIntType();
7384 const error_alignment = err_int_ty.abiAlignment(mod).toLlvm();
73737385 _ = try self.wip.store(.normal, ok_err_code, err_ptr, error_alignment);
73747386 const payload_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, payload_offset, "");
73757387 const payload_ptr_ty = try mod.singleMutPtrType(payload_ty);
......@@ -7393,8 +7405,8 @@ pub const FuncGen = struct {
73937405 if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return operand;
73947406 const err_un_llvm_ty = try o.lowerType(err_un_ty);
73957407
7396 const payload_offset = errUnionPayloadOffset(payload_ty, mod);
7397 const error_offset = errUnionErrorOffset(payload_ty, mod);
7408 const payload_offset = try errUnionPayloadOffset(payload_ty, mod);
7409 const error_offset = try errUnionErrorOffset(payload_ty, mod);
73987410 if (isByRef(err_un_ty, mod)) {
73997411 const directReturn = self.isNextRet(body_tail);
74007412 const result_ptr = if (directReturn)
......@@ -7406,7 +7418,8 @@ pub const FuncGen = struct {
74067418 };
74077419
74087420 const err_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, error_offset, "");
7409 const error_alignment = Type.err_int.abiAlignment(mod).toLlvm();
7421 const err_int_ty = try mod.errorIntType();
7422 const error_alignment = err_int_ty.abiAlignment(mod).toLlvm();
74107423 _ = try self.wip.store(.normal, operand, err_ptr, error_alignment);
74117424 const payload_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, payload_offset, "");
74127425 const payload_ptr_ty = try mod.singleMutPtrType(payload_ty);
......@@ -9363,7 +9376,7 @@ pub const FuncGen = struct {
93639376
93649377 for (names) |name| {
93659378 const err_int = mod.global_error_set.getIndex(name).?;
9366 const this_tag_int_value = try o.builder.intConst(Builder.Type.err_int, err_int);
9379 const this_tag_int_value = try o.builder.intConst(try o.errorIntType(), err_int);
93679380 try wip_switch.addCase(this_tag_int_value, valid_block, &self.wip);
93689381 }
93699382 self.wip.cursor = .{ .block = valid_block };
......@@ -9545,7 +9558,7 @@ pub const FuncGen = struct {
95459558 if (o.builder.getGlobal(name)) |llvm_fn| return llvm_fn.ptrConst(&o.builder).kind.function;
95469559
95479560 const function_index = try o.builder.addFunction(
9548 try o.builder.fnType(.i1, &.{Builder.Type.err_int}, .normal),
9561 try o.builder.fnType(.i1, &.{try o.errorIntType()}, .normal),
95499562 name,
95509563 toLlvmAddressSpace(.generic, o.module.getTarget()),
95519564 );
......@@ -10880,7 +10893,7 @@ fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Bu
1088010893 // If the return type is an error set or an error union, then we make this
1088110894 // anyerror return type instead, so that it can be coerced into a function
1088210895 // pointer type which has anyerror as the return type.
10883 return if (return_type.isError(mod)) Builder.Type.err_int else .void;
10896 return if (return_type.isError(mod)) try o.errorIntType() else .void;
1088410897 }
1088510898 const target = mod.getTarget();
1088610899 switch (fn_info.cc) {
......@@ -11633,12 +11646,14 @@ fn buildAllocaInner(
1163311646 return wip.conv(.unneeded, alloca, .ptr, "");
1163411647}
1163511648
11636fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) u1 {
11637 return @intFromBool(Type.err_int.abiAlignment(mod).compare(.gt, payload_ty.abiAlignment(mod)));
11649fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) !u1 {
11650 const err_int_ty = try mod.errorIntType();
11651 return @intFromBool(err_int_ty.abiAlignment(mod).compare(.gt, payload_ty.abiAlignment(mod)));
1163811652}
1163911653
11640fn errUnionErrorOffset(payload_ty: Type, mod: *Module) u1 {
11641 return @intFromBool(Type.err_int.abiAlignment(mod).compare(.lte, payload_ty.abiAlignment(mod)));
11654fn errUnionErrorOffset(payload_ty: Type, mod: *Module) !u1 {
11655 const err_int_ty = try mod.errorIntType();
11656 return @intFromBool(err_int_ty.abiAlignment(mod).compare(.lte, payload_ty.abiAlignment(mod)));
1164211657}
1164311658
1164411659/// Returns true for asm constraint (e.g. "=*m", "=r") if it accepts a memory location
src/codegen/llvm/Builder.zig-1
......@@ -159,7 +159,6 @@ pub const Type = enum(u32) {
159159 none = std.math.maxInt(u32),
160160 _,
161161
162 pub const err_int = Type.i16;
163162 pub const ptr_amdgpu_constant =
164163 @field(Type, std.fmt.comptimePrint("ptr{ }", .{AddrSpace.amdgpu.constant}));
165164
src/codegen/spirv.zig+3-2
......@@ -742,16 +742,17 @@ const DeclGen = struct {
742742 .error_union => |error_union| {
743743 // TODO: Error unions may be constructed with constant instructions if the payload type
744744 // allows it. For now, just generate it here regardless.
745 const err_int_ty = try mod.errorIntType();
745746 const err_ty = switch (error_union.val) {
746747 .err_name => ty.errorUnionSet(mod),
747 .payload => Type.err_int,
748 .payload => err_int_ty,
748749 };
749750 const err_val = switch (error_union.val) {
750751 .err_name => |err_name| (try mod.intern(.{ .err = .{
751752 .ty = ty.errorUnionSet(mod).toIntern(),
752753 .name = err_name,
753754 } })).toValue(),
754 .payload => try mod.intValue(Type.err_int, 0),
755 .payload => try mod.intValue(err_int_ty, 0),
755756 };
756757 const payload_ty = ty.errorUnionPayload(mod);
757758 const eu_layout = self.errorUnionLayout(payload_ty);
src/type.zig-2
......@@ -3309,8 +3309,6 @@ pub const Type = struct {
33093309
33103310 pub const generic_poison: Type = .{ .ip_index = .generic_poison_type };
33113311
3312 pub const err_int = Type.u16;
3313
33143312 pub fn smallestUnsignedBits(max: u64) u16 {
33153313 if (max == 0) return 0;
33163314 const base = std.math.log2(max);