zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 6cd80042133c28d37bb30eba49d022a4fb23c058 (tree)
parent 0559cdb5542a2acb50ce49363c1973f3ca70365e
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date:   Sat, 24 Dec 2022 02:33:06 -0500

Sema: relax undefined checks for concat

Closes #14037

Diffstat:
Msrc/Sema.zig | 12++++++++++--
Mtest/behavior/array.zig | 13+++++++++++++
2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -12262,8 +12262,16 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai break :p null; }; - const runtime_src = if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| rs: { - if (try sema.resolveDefinedValue(block, rhs_src, rhs)) |rhs_val| { + const runtime_src = if (switch (lhs_ty.zigTypeTag()) { + .Array, .Struct => try sema.resolveMaybeUndefVal(lhs), + .Pointer => try sema.resolveDefinedValue(block, lhs_src, lhs), + else => unreachable, + }) |lhs_val| rs: { + if (switch (rhs_ty.zigTypeTag()) { + .Array, .Struct => try sema.resolveMaybeUndefVal(rhs), + .Pointer => try sema.resolveDefinedValue(block, rhs_src, rhs), + else => unreachable, + }) |rhs_val| { const lhs_sub_val = if (lhs_ty.isSinglePointer()) (try sema.pointerDeref(block, lhs_src, lhs_val, lhs_ty)).? else diff --git a/test/behavior/array.zig b/test/behavior/array.zig @@ -45,6 +45,19 @@ fn getArrayLen(a: []const u32) usize { return a.len; } +test "array concat with undefined" { + { + var array = "hello".* ++ @as([5]u8, undefined); + array[5..10].* = "world".*; + try std.testing.expect(std.mem.eql(u8, &array, "helloworld")); + } + { + var array = @as([5]u8, undefined) ++ "world".*; + array[0..5].* = "hello".*; + try std.testing.expect(std.mem.eql(u8, &array, "helloworld")); + } +} + test "array concat with tuple" { const array: [2]u8 = .{ 1, 2 }; {