From 3264abe3d8f658e1b7275d2be80e43eddfc098dc Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 22 May 2022 21:51:44 -0700 Subject: stage2: fixes for error union semantics * Sema: avoid unnecessary safety checks when an error set is empty. * Sema: make zirErrorToInt handle comptime errors that are represented as integers. * Sema: make empty error sets properly integrate with typeHasOnePossibleValue. * Type: correct the ABI alignment and size of error unions which have both zero-bit error set and zero-bit payload. The previous code did not account for the fact that we still need to store a bit for whether there is an error. * LLVM: lower error unions possibly with the payload first or with the error code first, depending on alignment. Previously it always put the error code first and used a padding array. * LLVM: lower functions which have an empty error set as the return type the same as anyerror, so that they can be used where fn()anyerror function pointers are expected. In such functions, Zig will lower ret to returning zero instead of void. As a result, one more behavior test is passing. --- test/behavior/error.zig | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'test/behavior/error.zig') diff --git a/test/behavior/error.zig b/test/behavior/error.zig index ada0f3bbf1..376d1bdf09 100644 --- a/test/behavior/error.zig +++ b/test/behavior/error.zig @@ -148,18 +148,39 @@ test "implicit cast to optional to error union to return result loc" { //comptime S.entry(); TODO } -test "error: fn returning empty error set can be passed as fn returning any error" { +test "fn returning empty error set can be passed as fn returning any error" { entry(); comptime entry(); } +test "fn returning empty error set can be passed as fn returning any error - pointer" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + + entryPtr(); + comptime entryPtr(); +} + fn entry() void { foo2(bar2); } +fn entryPtr() void { + var ptr = &bar2; + fooPtr(ptr); +} + fn foo2(f: fn () anyerror!void) void { const x = f(); - x catch {}; + x catch { + @panic("fail"); + }; +} + +fn fooPtr(f: *const fn () anyerror!void) void { + const x = f(); + x catch { + @panic("fail"); + }; } fn bar2() (error{}!void) {} @@ -239,7 +260,11 @@ fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) !void { } test "comptime err to int of error set with only 1 possible value" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO testErrToIntWithOnePossibleValue(error.A, @errorToInt(error.A)); comptime testErrToIntWithOnePossibleValue(error.A, @errorToInt(error.A)); -- cgit v1.2.3