commit b59428e9f78b1f9f265c0ffdba79b128d77644d2 (tree)
parent 12e1304805cbe132d17b31bac2e8123869007e4d
Author: Andrew Kelley <andrew@ziglang.org>
Date: Tue, 29 Mar 2022 16:56:12 -0700
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.
Diffstat:
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/src/Sema.zig b/src/Sema.zig
@@ -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);
},
diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig
@@ -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);
+}