authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-29 16:56:12-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-29 16:56:12-07:00
logb59428e9f78b1f9f265c0ffdba79b128d77644d2
treec6a9a066eee45fc5ca3009e3b8bcf1f219adbd83
parent12e1304805cbe132d17b31bac2e8123869007e4d

Sema: adjust coercion of undefined error union payload

To no longer set the error code to undefined. This fixes the problem where an undefined single-item pointer coerced to an error union of a slice set the whole thing to undefined even though the sub-coercion to the slice would have produced a defined value.

2 files changed, 10 insertions(+), 5 deletions(-)

src/Sema.zig-5
...@@ -18140,11 +18140,6 @@ fn coerce(...@@ -18140,11 +18140,6 @@ fn coerce(
18140 return sema.addConstUndef(dest_ty);18140 return sema.addConstUndef(dest_ty);
18141 },18141 },
18142 else => {18142 else => {
18143 // undefined sets the error code also to undefined.
18144 if (is_undef) {
18145 return sema.addConstUndef(dest_ty);
18146 }
18147
18148 // T to E!T18143 // T to E!T
18149 return sema.wrapErrorUnionPayload(block, dest_ty, inst, inst_src);18144 return sema.wrapErrorUnionPayload(block, dest_ty, inst, inst_src);
18150 },18145 },
test/behavior/cast.zig+10
...@@ -1372,3 +1372,13 @@ test "cast compatible optional types" {...@@ -1372,3 +1372,13 @@ test "cast compatible optional types" {
1372 var b: ?[]const u8 = a;1372 var b: ?[]const u8 = a;
1373 try expect(b == null);1373 try expect(b == null);
1374}1374}
1375
1376test "coerce undefined single-item pointer of array to error union of slice" {
1377 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1378 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1379
1380 const a = @as([*]u8, undefined)[0..0];
1381 var b: error{a}![]const u8 = a;
1382 const s = try b;
1383 try expect(s.len == 0);
1384}