authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-03-22 19:26:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-23 16:34:48-04:00
log6fc07f49a9d723fba6422d83d23384a484b0b0be
treed2d03232347528bea81293a412bf83762e2a0283
parent23bae382c983c7685396e1525216060db06f2e00

stage2: concat/mult of slices yields ptr to array


2 files changed, 18 insertions(+), 5 deletions(-)

src/Sema.zig+2-2
......@@ -8643,7 +8643,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
86438643 });
86448644 const val = try Value.Tag.aggregate.create(anon_decl.arena(), buf);
86458645 const decl = try anon_decl.finish(ty, val, 0);
8646 if (lhs_single_ptr or rhs_single_ptr) {
8646 if (lhs_ty.zigTypeTag() == .Pointer or rhs_ty.zigTypeTag() == .Pointer) {
86478647 return sema.analyzeDeclRef(decl);
86488648 } else {
86498649 return sema.analyzeDeclVal(block, .unneeded, decl);
......@@ -8818,7 +8818,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
88188818 break :blk try Value.Tag.aggregate.create(anon_decl.arena(), buf);
88198819 };
88208820 const decl = try anon_decl.finish(final_ty, val, 0);
8821 if (is_single_ptr) {
8821 if (lhs_ty.zigTypeTag() == .Pointer) {
88228822 return sema.analyzeDeclRef(decl);
88238823 } else {
88248824 return sema.analyzeDeclVal(block, .unneeded, decl);
test/behavior/slice.zig+16-3
......@@ -583,14 +583,27 @@ test "type coercion of pointer to anon struct literal to pointer to slice" {
583583 comptime try S.doTheTest();
584584}
585585
586test "array concat of slices gives slice" {
587 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
588
586test "array concat of slices gives ptr to array" {
589587 comptime {
590588 var a: []const u8 = "aoeu";
591589 var b: []const u8 = "asdf";
592590 const c = a ++ b;
593591 try expect(std.mem.eql(u8, c, "aoeuasdf"));
592 if (builtin.zig_backend != .stage1) {
593 // spec change: array concat now returns pointer-to-array for slices
594 try expect(@TypeOf(c) == *const [8]u8);
595 }
596 }
597}
598
599test "array mult of slice gives ptr to array" {
600 if (builtin.zig_backend == .stage1) return error.SkipZigTest; // Stage 1 does not support multiplying slices
601
602 comptime {
603 var a: []const u8 = "aoeu";
604 const c = a ** 2;
605 try expect(std.mem.eql(u8, c, "aoeuaoeu"));
606 try expect(@TypeOf(c) == *const [8]u8);
594607 }
595608}
596609