Commit b59428e9f7

Andrew Kelley <andrew@ziglang.org>
2022-03-30 01:56:12
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.
1 parent 12e1304
Changed files (2)
src
test
behavior
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);
             },
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);
+}