authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-11-22 02:52:26+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-11-22 03:21:31+01:00
logcb248898ab41378c2e9bcf94d05c7c42577a7bab
tree97d571db34045fe9a5dd1fe9e98d678e5b7f31cd
parent83a0329c92dc8302b7098f718ceed2ed00e9fda2

sema: error union in-memory coercion


3 files changed, 25 insertions(+), 17 deletions(-)

src/Sema.zig+9-1
......@@ -12415,6 +12415,15 @@ fn coerceInMemoryAllowed(dest_ty: Type, src_ty: Type, dest_is_mut: bool, target:
1241512415 return coerceInMemoryAllowedFns(dest_ty, src_ty, target);
1241612416 }
1241712417
12418 // Error Unions
12419 if (dest_ty.zigTypeTag() == .ErrorUnion and src_ty.zigTypeTag() == .ErrorUnion) {
12420 const child = coerceInMemoryAllowed(dest_ty.errorUnionPayload(), src_ty.errorUnionPayload(), dest_is_mut, target);
12421 if (child == .no_match) {
12422 return child;
12423 }
12424 return coerceInMemoryAllowed(dest_ty.errorUnionSet(), src_ty.errorUnionSet(), dest_is_mut, target);
12425 }
12426
1241812427 // Error Sets
1241912428 if (dest_ty.zigTypeTag() == .ErrorSet and src_ty.zigTypeTag() == .ErrorSet) {
1242012429 return coerceInMemoryAllowedErrorSets(dest_ty, src_ty);
......@@ -12422,7 +12431,6 @@ fn coerceInMemoryAllowed(dest_ty: Type, src_ty: Type, dest_is_mut: bool, target:
1242212431
1242312432 // TODO: arrays
1242412433 // TODO: non-pointer-like optionals
12425 // TODO: error unions
1242612434 // TODO: vectors
1242712435
1242812436 return .no_match;
test/behavior/error.zig+16
......@@ -115,3 +115,19 @@ test "implicit cast to optional to error union to return result loc" {
115115 try S.entry();
116116 //comptime S.entry(); TODO
117117}
118
119test "error: fn returning empty error set can be passed as fn returning any error" {
120 entry();
121 comptime entry();
122}
123
124fn entry() void {
125 foo2(bar2);
126}
127
128fn foo2(f: fn () anyerror!void) void {
129 const x = f();
130 x catch {};
131}
132
133fn bar2() (error{}!void) {}
test/behavior/error_stage1.zig-16
......@@ -120,22 +120,6 @@ fn quux_1() !i32 {
120120 return error.C;
121121}
122122
123test "error: fn returning empty error set can be passed as fn returning any error" {
124 entry();
125 comptime entry();
126}
127
128fn entry() void {
129 foo2(bar2);
130}
131
132fn foo2(f: fn () anyerror!void) void {
133 const x = f();
134 x catch {};
135}
136
137fn bar2() (error{}!void) {}
138
139123test "error: Zero sized error set returned with value payload crash" {
140124 _ = foo3(0) catch {};
141125 _ = comptime foo3(0) catch {};