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:...@@ -12415,6 +12415,15 @@ fn coerceInMemoryAllowed(dest_ty: Type, src_ty: Type, dest_is_mut: bool, target:
12415 return coerceInMemoryAllowedFns(dest_ty, src_ty, target);12415 return coerceInMemoryAllowedFns(dest_ty, src_ty, target);
12416 }12416 }
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
12418 // Error Sets12427 // Error Sets
12419 if (dest_ty.zigTypeTag() == .ErrorSet and src_ty.zigTypeTag() == .ErrorSet) {12428 if (dest_ty.zigTypeTag() == .ErrorSet and src_ty.zigTypeTag() == .ErrorSet) {
12420 return coerceInMemoryAllowedErrorSets(dest_ty, src_ty);12429 return coerceInMemoryAllowedErrorSets(dest_ty, src_ty);
...@@ -12422,7 +12431,6 @@ fn coerceInMemoryAllowed(dest_ty: Type, src_ty: Type, dest_is_mut: bool, target:...@@ -12422,7 +12431,6 @@ fn coerceInMemoryAllowed(dest_ty: Type, src_ty: Type, dest_is_mut: bool, target:
1242212431
12423 // TODO: arrays12432 // TODO: arrays
12424 // TODO: non-pointer-like optionals12433 // TODO: non-pointer-like optionals
12425 // TODO: error unions
12426 // TODO: vectors12434 // TODO: vectors
1242712435
12428 return .no_match;12436 return .no_match;
test/behavior/error.zig+16
...@@ -115,3 +115,19 @@ test "implicit cast to optional to error union to return result loc" {...@@ -115,3 +115,19 @@ test "implicit cast to optional to error union to return result loc" {
115 try S.entry();115 try S.entry();
116 //comptime S.entry(); TODO116 //comptime S.entry(); TODO
117}117}
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 {...@@ -120,22 +120,6 @@ fn quux_1() !i32 {
120 return error.C;120 return error.C;
121}121}
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
139test "error: Zero sized error set returned with value payload crash" {123test "error: Zero sized error set returned with value payload crash" {
140 _ = foo3(0) catch {};124 _ = foo3(0) catch {};
141 _ = comptime foo3(0) catch {};125 _ = comptime foo3(0) catch {};