authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-10 14:11:59+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-11 23:49:33+03:00
log0333ff4476d0132a2397122dcab964de7fc0f2d3
tree8032dc588ab76313580feec71e07077ab1209960
parent488e1e5f51905485f9db37038e74bdea31ebd16e

stage2: make `error{}` the same size as `anyerror`

Having `error{}` be a zero bit type causes issues when it interracts with empty inferred error sets which are the same size as `anyerror`.

11 files changed, 123 insertions(+), 476 deletions(-)

lib/std/compress/deflate/compressor_test.zig-2
......@@ -179,7 +179,6 @@ test "deflate/inflate" {
179179}
180180
181181test "very long sparse chunk" {
182 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
183182 // A SparseReader returns a stream consisting of 0s ending with 65,536 (1<<16) 1s.
184183 // This tests missing hash references in a very large input.
185184 const SparseReader = struct {
......@@ -377,7 +376,6 @@ test "compressor dictionary" {
377376// Update the hash for best_speed only if d.index < d.maxInsertIndex
378377// See https://golang.org/issue/2508
379378test "Go non-regression test for 2508" {
380 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
381379 var comp = try compressor(
382380 testing.allocator,
383381 io.null_writer,
src/Sema.zig+17-42
......@@ -6182,6 +6182,17 @@ fn zirErrorToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
61826182 }
61836183 }
61846184
6185 const op_ty = sema.typeOf(op);
6186 try sema.resolveInferredErrorSetTy(block, src, op_ty);
6187 if (!op_ty.isAnyError()) {
6188 const names = op_ty.errorSetNames();
6189 switch (names.len) {
6190 0 => return sema.addConstant(result_ty, Value.zero),
6191 1 => return sema.addIntUnsigned(result_ty, sema.mod.global_error_set.get(names[0]).?),
6192 else => {},
6193 }
6194 }
6195
61856196 try sema.requireRuntimeBlock(block, src);
61866197 return block.addBitCast(result_ty, op_coerced);
61876198}
......@@ -6560,7 +6571,7 @@ fn analyzeErrUnionPayload(
65606571
65616572 // If the error set has no fields then no safety check is needed.
65626573 if (safety_check and block.wantSafety() and
6563 err_union_ty.errorUnionSet().errorSetCardinality() != .zero)
6574 !err_union_ty.errorUnionSet().errorSetIsEmpty())
65646575 {
65656576 try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err);
65666577 }
......@@ -6646,7 +6657,7 @@ fn analyzeErrUnionPayloadPtr(
66466657
66476658 // If the error set has no fields then no safety check is needed.
66486659 if (safety_check and block.wantSafety() and
6649 err_union_ty.errorUnionSet().errorSetCardinality() != .zero)
6660 !err_union_ty.errorUnionSet().errorSetIsEmpty())
66506661 {
66516662 try sema.panicUnwrapError(block, src, operand, .unwrap_errunion_err_ptr, .is_non_err_ptr);
66526663 }
......@@ -24231,6 +24242,10 @@ pub fn typeHasOnePossibleValue(
2423124242 .bool,
2423224243 .type,
2423324244 .anyerror,
24245 .error_set_single,
24246 .error_set,
24247 .error_set_merged,
24248 .error_union,
2423424249 .fn_noreturn_no_args,
2423524250 .fn_void_no_args,
2423624251 .fn_naked_noreturn_no_args,
......@@ -24287,46 +24302,6 @@ pub fn typeHasOnePossibleValue(
2428724302 }
2428824303 },
2428924304
24290 .error_union => {
24291 const error_ty = ty.errorUnionSet();
24292 switch (error_ty.errorSetCardinality()) {
24293 .zero => {
24294 const payload_ty = ty.errorUnionPayload();
24295 if (try typeHasOnePossibleValue(sema, block, src, payload_ty)) |payload_val| {
24296 return try Value.Tag.eu_payload.create(sema.arena, payload_val);
24297 } else {
24298 return null;
24299 }
24300 },
24301 .one => {
24302 if (ty.errorUnionPayload().isNoReturn()) {
24303 const error_val = (try typeHasOnePossibleValue(sema, block, src, error_ty)).?;
24304 return error_val;
24305 } else {
24306 return null;
24307 }
24308 },
24309 .many => return null,
24310 }
24311 },
24312
24313 .error_set_single => {
24314 const name = ty.castTag(.error_set_single).?.data;
24315 return try Value.Tag.@"error".create(sema.arena, .{ .name = name });
24316 },
24317 .error_set => {
24318 const err_set_obj = ty.castTag(.error_set).?.data;
24319 const names = err_set_obj.names.keys();
24320 if (names.len > 1) return null;
24321 return try Value.Tag.@"error".create(sema.arena, .{ .name = names[0] });
24322 },
24323 .error_set_merged => {
24324 const name_map = ty.castTag(.error_set_merged).?.data;
24325 const names = name_map.keys();
24326 if (names.len > 1) return null;
24327 return try Value.Tag.@"error".create(sema.arena, .{ .name = names[0] });
24328 },
24329
2433024305 .@"struct" => {
2433124306 const resolved_ty = try sema.resolveTypeFields(block, src, ty);
2433224307 const s = resolved_ty.castTag(.@"struct").?.data;
src/arch/aarch64/CodeGen.zig+3-8
......@@ -2277,7 +2277,7 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void {
22772277fn errUnionErr(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue {
22782278 const err_ty = error_union_ty.errorUnionSet();
22792279 const payload_ty = error_union_ty.errorUnionPayload();
2280 if (err_ty.errorSetCardinality() == .zero) {
2280 if (err_ty.errorSetIsEmpty()) {
22812281 return MCValue{ .immediate = 0 };
22822282 }
22832283 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -2311,7 +2311,7 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
23112311fn errUnionPayload(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue {
23122312 const err_ty = error_union_ty.errorUnionSet();
23132313 const payload_ty = error_union_ty.errorUnionPayload();
2314 if (err_ty.errorSetCardinality() == .zero) {
2314 if (err_ty.errorSetIsEmpty()) {
23152315 return error_union_mcv;
23162316 }
23172317 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -3590,7 +3590,7 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
35903590 const error_type = ty.errorUnionSet();
35913591 const payload_type = ty.errorUnionPayload();
35923592
3593 if (error_type.errorSetCardinality() == .zero) {
3593 if (error_type.errorSetIsEmpty()) {
35943594 return MCValue{ .immediate = 0 }; // always false
35953595 }
35963596
......@@ -4687,11 +4687,6 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
46874687 const error_type = typed_value.ty.errorUnionSet();
46884688 const payload_type = typed_value.ty.errorUnionPayload();
46894689
4690 if (error_type.errorSetCardinality() == .zero) {
4691 const payload_val = typed_value.val.castTag(.eu_payload).?.data;
4692 return self.genTypedValue(.{ .ty = payload_type, .val = payload_val });
4693 }
4694
46954690 const is_pl = typed_value.val.errorUnionIsPayload();
46964691
46974692 if (!payload_type.hasRuntimeBitsIgnoreComptime()) {
src/arch/arm/CodeGen.zig+3-9
......@@ -1773,7 +1773,7 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void {
17731773fn errUnionErr(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue {
17741774 const err_ty = error_union_ty.errorUnionSet();
17751775 const payload_ty = error_union_ty.errorUnionPayload();
1776 if (err_ty.errorSetCardinality() == .zero) {
1776 if (err_ty.errorSetIsEmpty()) {
17771777 return MCValue{ .immediate = 0 };
17781778 }
17791779 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -1810,7 +1810,7 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
18101810fn errUnionPayload(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue {
18111811 const err_ty = error_union_ty.errorUnionSet();
18121812 const payload_ty = error_union_ty.errorUnionPayload();
1813 if (err_ty.errorSetCardinality() == .zero) {
1813 if (err_ty.errorSetIsEmpty()) {
18141814 return error_union_mcv;
18151815 }
18161816 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -3922,7 +3922,7 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
39223922 const error_type = ty.errorUnionSet();
39233923 const error_int_type = Type.initTag(.u16);
39243924
3925 if (error_type.errorSetCardinality() == .zero) {
3925 if (error_type.errorSetIsEmpty()) {
39263926 return MCValue{ .immediate = 0 }; // always false
39273927 }
39283928
......@@ -5368,12 +5368,6 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
53685368 .ErrorUnion => {
53695369 const error_type = typed_value.ty.errorUnionSet();
53705370 const payload_type = typed_value.ty.errorUnionPayload();
5371
5372 if (error_type.errorSetCardinality() == .zero) {
5373 const payload_val = typed_value.val.castTag(.eu_payload).?.data;
5374 return self.genTypedValue(.{ .ty = payload_type, .val = payload_val });
5375 }
5376
53775371 const is_pl = typed_value.val.errorUnionIsPayload();
53785372
53795373 if (!payload_type.hasRuntimeBitsIgnoreComptime()) {
src/arch/wasm/CodeGen.zig+19-41
......@@ -1377,11 +1377,7 @@ fn isByRef(ty: Type, target: std.Target) bool {
13771377 .Int => return ty.intInfo(target).bits > 64,
13781378 .Float => return ty.floatBits(target) > 64,
13791379 .ErrorUnion => {
1380 const err_ty = ty.errorUnionSet();
13811380 const pl_ty = ty.errorUnionPayload();
1382 if (err_ty.errorSetCardinality() == .zero) {
1383 return isByRef(pl_ty, target);
1384 }
13851381 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
13861382 return false;
13871383 }
......@@ -1816,11 +1812,7 @@ fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
18161812fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerError!void {
18171813 switch (ty.zigTypeTag()) {
18181814 .ErrorUnion => {
1819 const err_ty = ty.errorUnionSet();
18201815 const pl_ty = ty.errorUnionPayload();
1821 if (err_ty.errorSetCardinality() == .zero) {
1822 return self.store(lhs, rhs, pl_ty, 0);
1823 }
18241816 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
18251817 return self.store(lhs, rhs, Type.anyerror, 0);
18261818 }
......@@ -2353,10 +2345,6 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue {
23532345 },
23542346 .ErrorUnion => {
23552347 const error_type = ty.errorUnionSet();
2356 if (error_type.errorSetCardinality() == .zero) {
2357 const pl_val = if (val.castTag(.eu_payload)) |pl| pl.data else Value.initTag(.undef);
2358 return self.lowerConstant(pl_val, ty.errorUnionPayload());
2359 }
23602348 const is_pl = val.errorUnionIsPayload();
23612349 const err_val = if (!is_pl) val else Value.initTag(.zero);
23622350 return self.lowerConstant(err_val, error_type);
......@@ -2925,7 +2913,7 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!W
29252913 const err_union_ty = self.air.typeOf(un_op);
29262914 const pl_ty = err_union_ty.errorUnionPayload();
29272915
2928 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
2916 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
29292917 switch (opcode) {
29302918 .i32_ne => return WValue{ .imm32 = 0 },
29312919 .i32_eq => return WValue{ .imm32 = 1 },
......@@ -2958,10 +2946,6 @@ fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool)
29582946 const err_ty = if (op_is_ptr) op_ty.childType() else op_ty;
29592947 const payload_ty = err_ty.errorUnionPayload();
29602948
2961 if (err_ty.errorUnionSet().errorSetCardinality() == .zero) {
2962 return operand;
2963 }
2964
29652949 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return WValue{ .none = {} };
29662950
29672951 const pl_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, self.target));
......@@ -2980,7 +2964,7 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) In
29802964 const err_ty = if (op_is_ptr) op_ty.childType() else op_ty;
29812965 const payload_ty = err_ty.errorUnionPayload();
29822966
2983 if (err_ty.errorUnionSet().errorSetCardinality() == .zero) {
2967 if (err_ty.errorUnionSet().errorSetIsEmpty()) {
29842968 return WValue{ .imm32 = 0 };
29852969 }
29862970
......@@ -2998,10 +2982,6 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
29982982 const operand = try self.resolveInst(ty_op.operand);
29992983 const err_ty = self.air.typeOfIndex(inst);
30002984
3001 if (err_ty.errorUnionSet().errorSetCardinality() == .zero) {
3002 return operand;
3003 }
3004
30052985 const pl_ty = self.air.typeOf(ty_op.operand);
30062986 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
30072987 return operand;
......@@ -4656,29 +4636,27 @@ fn lowerTry(
46564636 return self.fail("TODO: lowerTry for pointers", .{});
46574637 }
46584638
4659 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
4660 return err_union;
4661 }
4662
46634639 const pl_ty = err_union_ty.errorUnionPayload();
46644640 const pl_has_bits = pl_ty.hasRuntimeBitsIgnoreComptime();
46654641
4666 // Block we can jump out of when error is not set
4667 try self.startBlock(.block, wasm.block_empty);
4668
4669 // check if the error tag is set for the error union.
4670 try self.emitWValue(err_union);
4671 if (pl_has_bits) {
4672 const err_offset = @intCast(u32, errUnionErrorOffset(pl_ty, self.target));
4673 try self.addMemArg(.i32_load16_u, .{
4674 .offset = err_union.offset() + err_offset,
4675 .alignment = Type.anyerror.abiAlignment(self.target),
4676 });
4642 if (!err_union_ty.errorUnionSet().errorSetIsEmpty()) {
4643 // Block we can jump out of when error is not set
4644 try self.startBlock(.block, wasm.block_empty);
4645
4646 // check if the error tag is set for the error union.
4647 try self.emitWValue(err_union);
4648 if (pl_has_bits) {
4649 const err_offset = @intCast(u32, errUnionErrorOffset(pl_ty, self.target));
4650 try self.addMemArg(.i32_load16_u, .{
4651 .offset = err_union.offset() + err_offset,
4652 .alignment = Type.anyerror.abiAlignment(self.target),
4653 });
4654 }
4655 try self.addTag(.i32_eqz);
4656 try self.addLabel(.br_if, 0); // jump out of block when error is '0'
4657 try self.genBody(body);
4658 try self.endBlock();
46774659 }
4678 try self.addTag(.i32_eqz);
4679 try self.addLabel(.br_if, 0); // jump out of block when error is '0'
4680 try self.genBody(body);
4681 try self.endBlock();
46824660
46834661 // if we reach here it means error was not set, and we want the payload
46844662 if (!pl_has_bits) {
src/arch/x86_64/CodeGen.zig+2-19
......@@ -1806,7 +1806,7 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void {
18061806 const operand = try self.resolveInst(ty_op.operand);
18071807
18081808 const result: MCValue = result: {
1809 if (err_ty.errorSetCardinality() == .zero) {
1809 if (err_ty.errorSetIsEmpty()) {
18101810 break :result MCValue{ .immediate = 0 };
18111811 }
18121812
......@@ -1857,14 +1857,8 @@ fn genUnwrapErrorUnionPayloadMir(
18571857 err_union: MCValue,
18581858) !MCValue {
18591859 const payload_ty = err_union_ty.errorUnionPayload();
1860 const err_ty = err_union_ty.errorUnionSet();
18611860
18621861 const result: MCValue = result: {
1863 if (err_ty.errorSetCardinality() == .zero) {
1864 // TODO check if we can reuse
1865 break :result err_union;
1866 }
1867
18681862 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
18691863 break :result MCValue.none;
18701864 }
......@@ -1991,15 +1985,10 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void {
19911985 }
19921986
19931987 const error_union_ty = self.air.getRefType(ty_op.ty);
1994 const error_ty = error_union_ty.errorUnionSet();
19951988 const payload_ty = error_union_ty.errorUnionPayload();
19961989 const operand = try self.resolveInst(ty_op.operand);
19971990
19981991 const result: MCValue = result: {
1999 if (error_ty.errorSetCardinality() == .zero) {
2000 break :result operand;
2001 }
2002
20031992 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
20041993 break :result operand;
20051994 }
......@@ -4651,7 +4640,7 @@ fn isNonNull(self: *Self, inst: Air.Inst.Index, ty: Type, operand: MCValue) !MCV
46514640fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, ty: Type, operand: MCValue) !MCValue {
46524641 const err_type = ty.errorUnionSet();
46534642
4654 if (err_type.errorSetCardinality() == .zero) {
4643 if (err_type.errorSetIsEmpty()) {
46554644 return MCValue{ .immediate = 0 }; // always false
46564645 }
46574646
......@@ -6909,12 +6898,6 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
69096898 .ErrorUnion => {
69106899 const error_type = typed_value.ty.errorUnionSet();
69116900 const payload_type = typed_value.ty.errorUnionPayload();
6912
6913 if (error_type.errorSetCardinality() == .zero) {
6914 const payload_val = typed_value.val.castTag(.eu_payload).?.data;
6915 return self.genTypedValue(.{ .ty = payload_type, .val = payload_val });
6916 }
6917
69186901 const is_pl = typed_value.val.errorUnionIsPayload();
69196902
69206903 if (!payload_type.hasRuntimeBitsIgnoreComptime()) {
src/codegen.zig-9
......@@ -705,15 +705,6 @@ pub fn generateSymbol(
705705 .ErrorUnion => {
706706 const error_ty = typed_value.ty.errorUnionSet();
707707 const payload_ty = typed_value.ty.errorUnionPayload();
708
709 if (error_ty.errorSetCardinality() == .zero) {
710 const payload_val = typed_value.val.castTag(.eu_payload).?.data;
711 return generateSymbol(bin_file, src_loc, .{
712 .ty = payload_ty,
713 .val = payload_val,
714 }, code, debug_output, reloc_info);
715 }
716
717708 const is_payload = typed_value.val.errorUnionIsPayload();
718709
719710 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
src/codegen/c.zig+24-52
......@@ -752,12 +752,6 @@ pub const DeclGen = struct {
752752 const error_type = ty.errorUnionSet();
753753 const payload_type = ty.errorUnionPayload();
754754
755 if (error_type.errorSetCardinality() == .zero) {
756 // We use the payload directly as the type.
757 const payload_val = val.castTag(.eu_payload).?.data;
758 return dg.renderValue(writer, payload_type, payload_val, location);
759 }
760
761755 if (!payload_type.hasRuntimeBits()) {
762756 // We use the error type directly as the type.
763757 const err_val = if (val.errorUnionIsPayload()) Value.initTag(.zero) else val;
......@@ -1381,13 +1375,8 @@ pub const DeclGen = struct {
13811375 return w.writeAll("uint16_t");
13821376 },
13831377 .ErrorUnion => {
1384 const error_ty = t.errorUnionSet();
13851378 const payload_ty = t.errorUnionPayload();
13861379
1387 if (error_ty.errorSetCardinality() == .zero) {
1388 return dg.renderType(w, payload_ty);
1389 }
1390
13911380 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
13921381 return dg.renderType(w, Type.anyerror);
13931382 }
......@@ -2892,41 +2881,36 @@ fn lowerTry(
28922881 operand_is_ptr: bool,
28932882 result_ty: Type,
28942883) !CValue {
2895 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
2896 // If the error set has no fields, then the payload and the error
2897 // union are the same value.
2898 return err_union;
2899 }
2900
2884 const writer = f.object.writer();
29012885 const payload_ty = err_union_ty.errorUnionPayload();
29022886 const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime();
29032887
2904 const writer = f.object.writer();
2905
2906 err: {
2907 if (!payload_has_bits) {
2908 if (operand_is_ptr) {
2909 try writer.writeAll("if(*");
2910 } else {
2888 if (!err_union_ty.errorUnionSet().errorSetIsEmpty()) {
2889 err: {
2890 if (!payload_has_bits) {
2891 if (operand_is_ptr) {
2892 try writer.writeAll("if(*");
2893 } else {
2894 try writer.writeAll("if(");
2895 }
2896 try f.writeCValue(writer, err_union);
2897 try writer.writeAll(")");
2898 break :err;
2899 }
2900 if (operand_is_ptr or isByRef(err_union_ty)) {
29112901 try writer.writeAll("if(");
2902 try f.writeCValue(writer, err_union);
2903 try writer.writeAll("->error)");
2904 break :err;
29122905 }
2913 try f.writeCValue(writer, err_union);
2914 try writer.writeAll(")");
2915 break :err;
2916 }
2917 if (operand_is_ptr or isByRef(err_union_ty)) {
29182906 try writer.writeAll("if(");
29192907 try f.writeCValue(writer, err_union);
2920 try writer.writeAll("->error)");
2921 break :err;
2908 try writer.writeAll(".error)");
29222909 }
2923 try writer.writeAll("if(");
2924 try f.writeCValue(writer, err_union);
2925 try writer.writeAll(".error)");
2926 }
29272910
2928 try genBody(f, body);
2929 try f.object.indent_writer.insertNewline();
2911 try genBody(f, body);
2912 try f.object.indent_writer.insertNewline();
2913 }
29302914
29312915 if (!payload_has_bits) {
29322916 if (!operand_is_ptr) {
......@@ -3466,7 +3450,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
34663450
34673451 if (operand_ty.zigTypeTag() == .Pointer) {
34683452 const err_union_ty = operand_ty.childType();
3469 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
3453 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
34703454 return CValue{ .bytes = "0" };
34713455 }
34723456 if (!err_union_ty.errorUnionPayload().hasRuntimeBits()) {
......@@ -3478,7 +3462,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
34783462 try writer.writeAll(";\n");
34793463 return local;
34803464 }
3481 if (operand_ty.errorUnionSet().errorSetCardinality() == .zero) {
3465 if (operand_ty.errorUnionSet().errorSetIsEmpty()) {
34823466 return CValue{ .bytes = "0" };
34833467 }
34843468 if (!operand_ty.errorUnionPayload().hasRuntimeBits()) {
......@@ -3507,10 +3491,6 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, maybe_addrof: [*:0]c
35073491 const operand_is_ptr = operand_ty.zigTypeTag() == .Pointer;
35083492 const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty;
35093493
3510 if (error_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
3511 return operand;
3512 }
3513
35143494 if (!error_union_ty.errorUnionPayload().hasRuntimeBits()) {
35153495 return CValue.none;
35163496 }
......@@ -3575,11 +3555,6 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
35753555 const error_ty = error_union_ty.errorUnionSet();
35763556 const payload_ty = error_union_ty.errorUnionPayload();
35773557
3578 if (error_ty.errorSetCardinality() == .zero) {
3579 // TODO: write undefined bytes through the pointer here
3580 return operand;
3581 }
3582
35833558 // First, set the non-error value.
35843559 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
35853560 try f.writeCValueDeref(writer, operand);
......@@ -3623,9 +3598,6 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
36233598 const operand = try f.resolveInst(ty_op.operand);
36243599
36253600 const inst_ty = f.air.typeOfIndex(inst);
3626 if (inst_ty.errorUnionSet().errorSetCardinality() == .zero) {
3627 return operand;
3628 }
36293601 const local = try f.allocLocal(inst_ty, .Const);
36303602 try writer.writeAll(" = { .error = 0, .payload = ");
36313603 try f.writeCValue(writer, operand);
......@@ -3652,7 +3624,7 @@ fn airIsErr(
36523624
36533625 try writer.writeAll(" = ");
36543626
3655 if (error_ty.errorSetCardinality() == .zero) {
3627 if (error_ty.errorSetIsEmpty()) {
36563628 try writer.print("0 {s} 0;\n", .{op_str});
36573629 } else {
36583630 if (is_ptr) {
src/codegen/llvm.zig+26-72
......@@ -1571,22 +1571,6 @@ pub const Object = struct {
15711571 },
15721572 .ErrorUnion => {
15731573 const payload_ty = ty.errorUnionPayload();
1574 switch (ty.errorUnionSet().errorSetCardinality()) {
1575 .zero => {
1576 const payload_di_ty = try o.lowerDebugType(payload_ty, .full);
1577 // The recursive call to `lowerDebugType` means we can't use `gop` anymore.
1578 try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(payload_di_ty), .{ .mod = o.module });
1579 return payload_di_ty;
1580 },
1581 .one => {
1582 if (payload_ty.isNoReturn()) {
1583 const di_type = dib.createBasicType("void", 0, DW.ATE.signed);
1584 gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_type);
1585 return di_type;
1586 }
1587 },
1588 .many => {},
1589 }
15901574 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
15911575 const err_set_di_ty = try o.lowerDebugType(Type.anyerror, .full);
15921576 // The recursive call to `lowerDebugType` means we can't use `gop` anymore.
......@@ -2554,15 +2538,6 @@ pub const DeclGen = struct {
25542538 },
25552539 .ErrorUnion => {
25562540 const payload_ty = t.errorUnionPayload();
2557 switch (t.errorUnionSet().errorSetCardinality()) {
2558 .zero => return dg.lowerType(payload_ty),
2559 .one => {
2560 if (payload_ty.isNoReturn()) {
2561 return dg.context.voidType();
2562 }
2563 },
2564 .many => {},
2565 }
25662541 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
25672542 return try dg.lowerType(Type.anyerror);
25682543 }
......@@ -3222,10 +3197,6 @@ pub const DeclGen = struct {
32223197 },
32233198 .ErrorUnion => {
32243199 const payload_type = tv.ty.errorUnionPayload();
3225 if (tv.ty.errorUnionSet().errorSetCardinality() == .zero) {
3226 const payload_val = tv.val.castTag(.eu_payload).?.data;
3227 return dg.lowerValue(.{ .ty = payload_type, .val = payload_val });
3228 }
32293200 const is_pl = tv.val.errorUnionIsPayload();
32303201
32313202 if (!payload_type.hasRuntimeBitsIgnoreComptime()) {
......@@ -4795,40 +4766,37 @@ pub const FuncGen = struct {
47954766 }
47964767
47974768 fn lowerTry(fg: *FuncGen, err_union: *const llvm.Value, body: []const Air.Inst.Index, err_union_ty: Type, operand_is_ptr: bool, result_ty: Type) !?*const llvm.Value {
4798 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
4799 // If the error set has no fields, then the payload and the error
4800 // union are the same value.
4801 return err_union;
4802 }
4803
48044769 const payload_ty = err_union_ty.errorUnionPayload();
48054770 const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime();
48064771 const target = fg.dg.module.getTarget();
4807 const is_err = err: {
4808 const err_set_ty = try fg.dg.lowerType(Type.anyerror);
4809 const zero = err_set_ty.constNull();
4810 if (!payload_has_bits) {
4811 const loaded = if (operand_is_ptr) fg.builder.buildLoad(err_union, "") else err_union;
4812 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4813 }
4814 const err_field_index = errUnionErrorOffset(payload_ty, target);
4815 if (operand_is_ptr or isByRef(err_union_ty)) {
4816 const err_field_ptr = fg.builder.buildStructGEP(err_union, err_field_index, "");
4817 const loaded = fg.builder.buildLoad(err_field_ptr, "");
4772
4773 if (!err_union_ty.errorUnionSet().errorSetIsEmpty()) {
4774 const is_err = err: {
4775 const err_set_ty = try fg.dg.lowerType(Type.anyerror);
4776 const zero = err_set_ty.constNull();
4777 if (!payload_has_bits) {
4778 const loaded = if (operand_is_ptr) fg.builder.buildLoad(err_union, "") else err_union;
4779 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4780 }
4781 const err_field_index = errUnionErrorOffset(payload_ty, target);
4782 if (operand_is_ptr or isByRef(err_union_ty)) {
4783 const err_field_ptr = fg.builder.buildStructGEP(err_union, err_field_index, "");
4784 const loaded = fg.builder.buildLoad(err_field_ptr, "");
4785 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4786 }
4787 const loaded = fg.builder.buildExtractValue(err_union, err_field_index, "");
48184788 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4819 }
4820 const loaded = fg.builder.buildExtractValue(err_union, err_field_index, "");
4821 break :err fg.builder.buildICmp(.NE, loaded, zero, "");
4822 };
4789 };
48234790
4824 const return_block = fg.context.appendBasicBlock(fg.llvm_func, "TryRet");
4825 const continue_block = fg.context.appendBasicBlock(fg.llvm_func, "TryCont");
4826 _ = fg.builder.buildCondBr(is_err, return_block, continue_block);
4791 const return_block = fg.context.appendBasicBlock(fg.llvm_func, "TryRet");
4792 const continue_block = fg.context.appendBasicBlock(fg.llvm_func, "TryCont");
4793 _ = fg.builder.buildCondBr(is_err, return_block, continue_block);
48274794
4828 fg.builder.positionBuilderAtEnd(return_block);
4829 try fg.genBody(body);
4795 fg.builder.positionBuilderAtEnd(return_block);
4796 try fg.genBody(body);
48304797
4831 fg.builder.positionBuilderAtEnd(continue_block);
4798 fg.builder.positionBuilderAtEnd(continue_block);
4799 }
48324800 if (!payload_has_bits) {
48334801 if (!operand_is_ptr) return null;
48344802
......@@ -5665,7 +5633,7 @@ pub const FuncGen = struct {
56655633 const err_set_ty = try self.dg.lowerType(Type.initTag(.anyerror));
56665634 const zero = err_set_ty.constNull();
56675635
5668 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
5636 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
56695637 const llvm_i1 = self.context.intType(1);
56705638 switch (op) {
56715639 .EQ => return llvm_i1.constInt(1, .False), // 0 == 0
......@@ -5788,13 +5756,6 @@ pub const FuncGen = struct {
57885756
57895757 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
57905758 const operand = try self.resolveInst(ty_op.operand);
5791 const operand_ty = self.air.typeOf(ty_op.operand);
5792 const error_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty;
5793 if (error_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
5794 // If the error set has no fields, then the payload and the error
5795 // union are the same value.
5796 return operand;
5797 }
57985759 const result_ty = self.air.typeOfIndex(inst);
57995760 const payload_ty = if (operand_is_ptr) result_ty.childType() else result_ty;
58005761 const target = self.dg.module.getTarget();
......@@ -5825,7 +5786,7 @@ pub const FuncGen = struct {
58255786 const operand = try self.resolveInst(ty_op.operand);
58265787 const operand_ty = self.air.typeOf(ty_op.operand);
58275788 const err_union_ty = if (operand_is_ptr) operand_ty.childType() else operand_ty;
5828 if (err_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
5789 if (err_union_ty.errorUnionSet().errorSetIsEmpty()) {
58295790 const err_llvm_ty = try self.dg.lowerType(Type.anyerror);
58305791 if (operand_is_ptr) {
58315792 return self.builder.buildBitCast(operand, err_llvm_ty.pointerType(0), "");
......@@ -5856,10 +5817,6 @@ pub const FuncGen = struct {
58565817 const operand = try self.resolveInst(ty_op.operand);
58575818 const error_union_ty = self.air.typeOf(ty_op.operand).childType();
58585819
5859 if (error_union_ty.errorUnionSet().errorSetCardinality() == .zero) {
5860 // TODO: write undefined bytes through the pointer here
5861 return operand;
5862 }
58635820 const payload_ty = error_union_ty.errorUnionPayload();
58645821 const non_error_val = try self.dg.lowerValue(.{ .ty = Type.anyerror, .val = Value.zero });
58655822 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
......@@ -5938,9 +5895,6 @@ pub const FuncGen = struct {
59385895 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
59395896 const inst_ty = self.air.typeOfIndex(inst);
59405897 const operand = try self.resolveInst(ty_op.operand);
5941 if (inst_ty.errorUnionSet().errorSetCardinality() == .zero) {
5942 return operand;
5943 }
59445898 const payload_ty = self.air.typeOf(ty_op.operand);
59455899 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
59465900 return operand;
src/type.zig+29-163
......@@ -2366,6 +2366,10 @@ pub const Type = extern union {
23662366 .anyopaque,
23672367 .@"opaque",
23682368 .type_info,
2369 .error_set_single,
2370 .error_union,
2371 .error_set,
2372 .error_set_merged,
23692373 => return true,
23702374
23712375 // These are false because they are comptime-only types.
......@@ -2389,20 +2393,8 @@ pub const Type = extern union {
23892393 .fn_void_no_args,
23902394 .fn_naked_noreturn_no_args,
23912395 .fn_ccc_void_no_args,
2392 .error_set_single,
23932396 => return false,
23942397
2395 .error_set => {
2396 const err_set_obj = ty.castTag(.error_set).?.data;
2397 const names = err_set_obj.names.keys();
2398 return names.len > 1;
2399 },
2400 .error_set_merged => {
2401 const name_map = ty.castTag(.error_set_merged).?.data;
2402 const names = name_map.keys();
2403 return names.len > 1;
2404 },
2405
24062398 // These types have more than one possible value, so the result is the same as
24072399 // asking whether they are comptime-only types.
24082400 .anyframe_T,
......@@ -2443,25 +2435,6 @@ pub const Type = extern union {
24432435 }
24442436 },
24452437
2446 .error_union => {
2447 // This code needs to be kept in sync with the equivalent switch prong
2448 // in abiSizeAdvanced.
2449 const data = ty.castTag(.error_union).?.data;
2450 switch (data.error_set.errorSetCardinality()) {
2451 .zero => return hasRuntimeBitsAdvanced(data.payload, ignore_comptime_only, sema_kit),
2452 .one => return !data.payload.isNoReturn(),
2453 .many => {
2454 if (ignore_comptime_only) {
2455 return true;
2456 } else if (sema_kit) |sk| {
2457 return !(try sk.sema.typeRequiresComptime(sk.block, sk.src, ty));
2458 } else {
2459 return !comptimeOnly(ty);
2460 }
2461 },
2462 }
2463 },
2464
24652438 .@"struct" => {
24662439 const struct_obj = ty.castTag(.@"struct").?.data;
24672440 if (struct_obj.status == .field_types_wip) {
......@@ -2926,27 +2899,11 @@ pub const Type = extern union {
29262899 .anyerror_void_error_union,
29272900 .anyerror,
29282901 .error_set_inferred,
2902 .error_set_single,
2903 .error_set,
2904 .error_set_merged,
29292905 => return AbiAlignmentAdvanced{ .scalar = 2 },
29302906
2931 .error_set => {
2932 const err_set_obj = ty.castTag(.error_set).?.data;
2933 const names = err_set_obj.names.keys();
2934 if (names.len <= 1) {
2935 return AbiAlignmentAdvanced{ .scalar = 0 };
2936 } else {
2937 return AbiAlignmentAdvanced{ .scalar = 2 };
2938 }
2939 },
2940 .error_set_merged => {
2941 const name_map = ty.castTag(.error_set_merged).?.data;
2942 const names = name_map.keys();
2943 if (names.len <= 1) {
2944 return AbiAlignmentAdvanced{ .scalar = 0 };
2945 } else {
2946 return AbiAlignmentAdvanced{ .scalar = 2 };
2947 }
2948 },
2949
29502907 .array, .array_sentinel => return ty.elemType().abiAlignmentAdvanced(target, strat),
29512908
29522909 // TODO audit this - is there any more complicated logic to determine
......@@ -2971,12 +2928,7 @@ pub const Type = extern union {
29712928
29722929 switch (child_type.zigTypeTag()) {
29732930 .Pointer => return AbiAlignmentAdvanced{ .scalar = @divExact(target.cpu.arch.ptrBitWidth(), 8) },
2974 .ErrorSet => switch (child_type.errorSetCardinality()) {
2975 // `?error{}` is comptime-known to be null.
2976 .zero => return AbiAlignmentAdvanced{ .scalar = 0 },
2977 .one => return AbiAlignmentAdvanced{ .scalar = 1 },
2978 .many => return abiAlignmentAdvanced(Type.anyerror, target, strat),
2979 },
2931 .ErrorSet => return abiAlignmentAdvanced(Type.anyerror, target, strat),
29802932 .NoReturn => return AbiAlignmentAdvanced{ .scalar = 0 },
29812933 else => {},
29822934 }
......@@ -2999,15 +2951,6 @@ pub const Type = extern union {
29992951 // This code needs to be kept in sync with the equivalent switch prong
30002952 // in abiSizeAdvanced.
30012953 const data = ty.castTag(.error_union).?.data;
3002 switch (data.error_set.errorSetCardinality()) {
3003 .zero => return abiAlignmentAdvanced(data.payload, target, strat),
3004 .one => {
3005 if (data.payload.isNoReturn()) {
3006 return AbiAlignmentAdvanced{ .scalar = 0 };
3007 }
3008 },
3009 .many => {},
3010 }
30112954 const code_align = abiAlignment(Type.anyerror, target);
30122955 switch (strat) {
30132956 .eager, .sema_kit => {
......@@ -3118,7 +3061,6 @@ pub const Type = extern union {
31183061 .@"undefined",
31193062 .enum_literal,
31203063 .type_info,
3121 .error_set_single,
31223064 => return AbiAlignmentAdvanced{ .scalar = 0 },
31233065
31243066 .noreturn,
......@@ -3237,7 +3179,6 @@ pub const Type = extern union {
32373179 .empty_struct_literal,
32383180 .empty_struct,
32393181 .void,
3240 .error_set_single,
32413182 => return AbiSizeAdvanced{ .scalar = 0 },
32423183
32433184 .@"struct", .tuple, .anon_struct => switch (ty.containerLayout()) {
......@@ -3396,27 +3337,11 @@ pub const Type = extern union {
33963337 .anyerror_void_error_union,
33973338 .anyerror,
33983339 .error_set_inferred,
3340 .error_set,
3341 .error_set_merged,
3342 .error_set_single,
33993343 => return AbiSizeAdvanced{ .scalar = 2 },
34003344
3401 .error_set => {
3402 const err_set_obj = ty.castTag(.error_set).?.data;
3403 const names = err_set_obj.names.keys();
3404 if (names.len <= 1) {
3405 return AbiSizeAdvanced{ .scalar = 0 };
3406 } else {
3407 return AbiSizeAdvanced{ .scalar = 2 };
3408 }
3409 },
3410 .error_set_merged => {
3411 const name_map = ty.castTag(.error_set_merged).?.data;
3412 const names = name_map.keys();
3413 if (names.len <= 1) {
3414 return AbiSizeAdvanced{ .scalar = 0 };
3415 } else {
3416 return AbiSizeAdvanced{ .scalar = 2 };
3417 }
3418 },
3419
34203345 .i16, .u16 => return AbiSizeAdvanced{ .scalar = intAbiSize(16, target) },
34213346 .u29 => return AbiSizeAdvanced{ .scalar = intAbiSize(29, target) },
34223347 .i32, .u32 => return AbiSizeAdvanced{ .scalar = intAbiSize(32, target) },
......@@ -3467,24 +3392,6 @@ pub const Type = extern union {
34673392 // This code needs to be kept in sync with the equivalent switch prong
34683393 // in abiAlignmentAdvanced.
34693394 const data = ty.castTag(.error_union).?.data;
3470 // Here we need to care whether or not the error set is *empty* or whether
3471 // it only has *one possible value*. In the former case, it means there
3472 // cannot possibly be an error, meaning the ABI size is equivalent to the
3473 // payload ABI size. In the latter case, we need to account for the "tag"
3474 // because even if both the payload type and the error set type of an
3475 // error union have no runtime bits, an error union still has
3476 // 1 bit of data which is whether or not the value is an error.
3477 // Zig still uses the error code encoding at runtime, even when only 1 bit
3478 // would suffice. This prevents coercions from needing to branch.
3479 switch (data.error_set.errorSetCardinality()) {
3480 .zero => return abiSizeAdvanced(data.payload, target, strat),
3481 .one => {
3482 if (data.payload.isNoReturn()) {
3483 return AbiSizeAdvanced{ .scalar = 0 };
3484 }
3485 },
3486 .many => {},
3487 }
34883395 const code_size = abiSize(Type.anyerror, target);
34893396 if (!data.payload.hasRuntimeBits()) {
34903397 // Same as anyerror.
......@@ -3727,11 +3634,7 @@ pub const Type = extern union {
37273634
37283635 .error_union => {
37293636 const payload = ty.castTag(.error_union).?.data;
3730 if (!payload.error_set.hasRuntimeBits() and !payload.payload.hasRuntimeBits()) {
3731 return 0;
3732 } else if (!payload.error_set.hasRuntimeBits()) {
3733 return payload.payload.bitSizeAdvanced(target, sema_kit);
3734 } else if (!payload.payload.hasRuntimeBits()) {
3637 if (!payload.payload.hasRuntimeBits()) {
37353638 return payload.error_set.bitSizeAdvanced(target, sema_kit);
37363639 }
37373640 @panic("TODO bitSize error union");
......@@ -4351,30 +4254,25 @@ pub const Type = extern union {
43514254 };
43524255 }
43534256
4354 const ErrorSetCardinality = enum { zero, one, many };
4355
4356 pub fn errorSetCardinality(ty: Type) ErrorSetCardinality {
4257 /// Returns false for unresolved inferred error sets.
4258 pub fn errorSetIsEmpty(ty: Type) bool {
43574259 switch (ty.tag()) {
4358 .anyerror => return .many,
4359 .error_set_inferred => return .many,
4360 .error_set_single => return .one,
4260 .anyerror => return false,
4261 .error_set_inferred => {
4262 const inferred_error_set = ty.castTag(.error_set_inferred).?.data;
4263 // Can't know for sure.
4264 if (!inferred_error_set.is_resolved) return false;
4265 if (inferred_error_set.is_anyerror) return false;
4266 return inferred_error_set.errors.count() == 0;
4267 },
4268 .error_set_single => return false,
43614269 .error_set => {
43624270 const err_set_obj = ty.castTag(.error_set).?.data;
4363 const names = err_set_obj.names.keys();
4364 switch (names.len) {
4365 0 => return .zero,
4366 1 => return .one,
4367 else => return .many,
4368 }
4271 return err_set_obj.names.count() == 0;
43694272 },
43704273 .error_set_merged => {
43714274 const name_map = ty.castTag(.error_set_merged).?.data;
4372 const names = name_map.keys();
4373 switch (names.len) {
4374 0 => return .zero,
4375 1 => return .one,
4376 else => return .many,
4377 }
4275 return name_map.count() == 0;
43784276 },
43794277 else => unreachable,
43804278 }
......@@ -4883,6 +4781,10 @@ pub const Type = extern union {
48834781 .bool,
48844782 .type,
48854783 .anyerror,
4784 .error_union,
4785 .error_set_single,
4786 .error_set,
4787 .error_set_merged,
48864788 .fn_noreturn_no_args,
48874789 .fn_void_no_args,
48884790 .fn_naked_noreturn_no_args,
......@@ -4939,42 +4841,6 @@ pub const Type = extern union {
49394841 }
49404842 },
49414843
4942 .error_union => {
4943 const error_ty = ty.errorUnionSet();
4944 switch (error_ty.errorSetCardinality()) {
4945 .zero => {
4946 const payload_ty = ty.errorUnionPayload();
4947 if (onePossibleValue(payload_ty)) |payload_val| {
4948 _ = payload_val;
4949 return Value.initTag(.the_only_possible_value);
4950 } else {
4951 return null;
4952 }
4953 },
4954 .one => {
4955 if (ty.errorUnionPayload().isNoReturn()) {
4956 const error_val = onePossibleValue(error_ty).?;
4957 return error_val;
4958 } else {
4959 return null;
4960 }
4961 },
4962 .many => return null,
4963 }
4964 },
4965
4966 .error_set_single => return Value.initTag(.the_only_possible_value),
4967 .error_set => {
4968 const err_set_obj = ty.castTag(.error_set).?.data;
4969 if (err_set_obj.names.count() > 1) return null;
4970 return Value.initTag(.the_only_possible_value);
4971 },
4972 .error_set_merged => {
4973 const name_map = ty.castTag(.error_set_merged).?.data;
4974 if (name_map.count() > 1) return null;
4975 return Value.initTag(.the_only_possible_value);
4976 },
4977
49784844 .@"struct" => {
49794845 const s = ty.castTag(.@"struct").?.data;
49804846 assert(s.haveFieldTypes());
test/behavior/error.zig-59
......@@ -453,65 +453,6 @@ test "optional error set is the same size as error set" {
453453 comptime try expect(S.returnsOptErrSet() == null);
454454}
455455
456test "optional error set with only one error is the same size as bool" {
457 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
458 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
459 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
460
461 const E = error{only};
462 comptime try expect(@sizeOf(?E) == @sizeOf(bool));
463 comptime try expect(@alignOf(?E) == @alignOf(bool));
464 const S = struct {
465 fn gimmeNull() ?E {
466 return null;
467 }
468 fn gimmeErr() ?E {
469 return error.only;
470 }
471 };
472 try expect(S.gimmeNull() == null);
473 try expect(error.only == S.gimmeErr().?);
474 comptime try expect(S.gimmeNull() == null);
475 comptime try expect(error.only == S.gimmeErr().?);
476}
477
478test "optional empty error set" {
479 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
480
481 comptime try expect(@sizeOf(error{}!void) == @sizeOf(void));
482 comptime try expect(@alignOf(error{}!void) == @alignOf(void));
483
484 var x: ?error{} = undefined;
485 if (x != null) {
486 @compileError("test failed");
487 }
488}
489
490test "empty error set plus zero-bit payload" {
491 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
492 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
493 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
494
495 comptime try expect(@sizeOf(error{}!void) == @sizeOf(void));
496 comptime try expect(@alignOf(error{}!void) == @alignOf(void));
497
498 var x: error{}!void = undefined;
499 if (x) |payload| {
500 if (payload != {}) {
501 @compileError("test failed");
502 }
503 } else |_| {
504 @compileError("test failed");
505 }
506 const S = struct {
507 fn empty() error{}!void {}
508 fn inferred() !void {
509 return empty();
510 }
511 };
512 try S.inferred();
513}
514
515456test "nested catch" {
516457 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
517458 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO