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.
This commit is contained in:
Andrew Kelley 2022-03-29 16:56:12 -07:00
parent 12e1304805
commit b59428e9f7
2 changed files with 10 additions and 5 deletions

View File

@ -18140,11 +18140,6 @@ fn coerce(
return sema.addConstUndef(dest_ty);
},
else => {
// undefined sets the error code also to undefined.
if (is_undef) {
return sema.addConstUndef(dest_ty);
}
// T to E!T
return sema.wrapErrorUnionPayload(block, dest_ty, inst, inst_src);
},

View File

@ -1372,3 +1372,13 @@ test "cast compatible optional types" {
var b: ?[]const u8 = a;
try expect(b == null);
}
test "coerce undefined single-item pointer of array to error union of slice" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
const a = @as([*]u8, undefined)[0..0];
var b: error{a}![]const u8 = a;
const s = try b;
try expect(s.len == 0);
}