zig

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

commit 6fc07f49a9d723fba6422d83d23384a484b0b0be (tree)
parent 23bae382c983c7685396e1525216060db06f2e00
Author: Cody Tapscott <topolarity@tapscott.me>
Date:   Tue, 22 Mar 2022 19:26:25 -0700

stage2: concat/mult of slices yields ptr to array

Diffstat:
Msrc/Sema.zig | 4++--
Mtest/behavior/slice.zig | 19++++++++++++++++---
2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -8643,7 +8643,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai }); const val = try Value.Tag.aggregate.create(anon_decl.arena(), buf); const decl = try anon_decl.finish(ty, val, 0); - if (lhs_single_ptr or rhs_single_ptr) { + if (lhs_ty.zigTypeTag() == .Pointer or rhs_ty.zigTypeTag() == .Pointer) { return sema.analyzeDeclRef(decl); } else { return sema.analyzeDeclVal(block, .unneeded, decl); @@ -8818,7 +8818,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai break :blk try Value.Tag.aggregate.create(anon_decl.arena(), buf); }; const decl = try anon_decl.finish(final_ty, val, 0); - if (is_single_ptr) { + if (lhs_ty.zigTypeTag() == .Pointer) { return sema.analyzeDeclRef(decl); } else { return sema.analyzeDeclVal(block, .unneeded, decl); diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig @@ -583,14 +583,27 @@ test "type coercion of pointer to anon struct literal to pointer to slice" { comptime try S.doTheTest(); } -test "array concat of slices gives slice" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - +test "array concat of slices gives ptr to array" { comptime { var a: []const u8 = "aoeu"; var b: []const u8 = "asdf"; const c = a ++ b; try expect(std.mem.eql(u8, c, "aoeuasdf")); + if (builtin.zig_backend != .stage1) { + // spec change: array concat now returns pointer-to-array for slices + try expect(@TypeOf(c) == *const [8]u8); + } + } +} + +test "array mult of slice gives ptr to array" { + if (builtin.zig_backend == .stage1) return error.SkipZigTest; // Stage 1 does not support multiplying slices + + comptime { + var a: []const u8 = "aoeu"; + const c = a ** 2; + try expect(std.mem.eql(u8, c, "aoeuaoeu")); + try expect(@TypeOf(c) == *const [8]u8); } }