authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-12-24 02:33:06-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-12-24 02:40:33-05:00
log6cd80042133c28d37bb30eba49d022a4fb23c058
tree145473c2257fc2d9ed6d8538e9304b286d64ccdf
parent0559cdb5542a2acb50ce49363c1973f3ca70365e

Sema: relax undefined checks for concat

Closes #14037

2 files changed, 23 insertions(+), 2 deletions(-)

src/Sema.zig+10-2
...@@ -12262,8 +12262,16 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -12262,8 +12262,16 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
12262 break :p null;12262 break :p null;
12263 };12263 };
1226412264
12265 const runtime_src = if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| rs: {12265 const runtime_src = if (switch (lhs_ty.zigTypeTag()) {
12266 if (try sema.resolveDefinedValue(block, rhs_src, rhs)) |rhs_val| {12266 .Array, .Struct => try sema.resolveMaybeUndefVal(lhs),
12267 .Pointer => try sema.resolveDefinedValue(block, lhs_src, lhs),
12268 else => unreachable,
12269 }) |lhs_val| rs: {
12270 if (switch (rhs_ty.zigTypeTag()) {
12271 .Array, .Struct => try sema.resolveMaybeUndefVal(rhs),
12272 .Pointer => try sema.resolveDefinedValue(block, rhs_src, rhs),
12273 else => unreachable,
12274 }) |rhs_val| {
12267 const lhs_sub_val = if (lhs_ty.isSinglePointer())12275 const lhs_sub_val = if (lhs_ty.isSinglePointer())
12268 (try sema.pointerDeref(block, lhs_src, lhs_val, lhs_ty)).?12276 (try sema.pointerDeref(block, lhs_src, lhs_val, lhs_ty)).?
12269 else12277 else
test/behavior/array.zig+13
...@@ -45,6 +45,19 @@ fn getArrayLen(a: []const u32) usize {...@@ -45,6 +45,19 @@ fn getArrayLen(a: []const u32) usize {
45 return a.len;45 return a.len;
46}46}
4747
48test "array concat with undefined" {
49 {
50 var array = "hello".* ++ @as([5]u8, undefined);
51 array[5..10].* = "world".*;
52 try std.testing.expect(std.mem.eql(u8, &array, "helloworld"));
53 }
54 {
55 var array = @as([5]u8, undefined) ++ "world".*;
56 array[0..5].* = "hello".*;
57 try std.testing.expect(std.mem.eql(u8, &array, "helloworld"));
58 }
59}
60
48test "array concat with tuple" {61test "array concat with tuple" {
49 const array: [2]u8 = .{ 1, 2 };62 const array: [2]u8 = .{ 1, 2 };
50 {63 {