Skip to content

Commit 6f9f83e

Browse files
committed
stage2: concat/mult of slices yields ptr to array
1 parent e8813b2 commit 6f9f83e

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/Sema.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8642,7 +8642,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
86428642
});
86438643
const val = try Value.Tag.aggregate.create(anon_decl.arena(), buf);
86448644
const decl = try anon_decl.finish(ty, val, 0);
8645-
if (lhs_single_ptr or rhs_single_ptr) {
8645+
if (lhs_ty.zigTypeTag() == .Pointer or rhs_ty.zigTypeTag() == .Pointer) {
86468646
return sema.analyzeDeclRef(decl);
86478647
} else {
86488648
return sema.analyzeDeclVal(block, .unneeded, decl);
@@ -8817,7 +8817,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
88178817
break :blk try Value.Tag.aggregate.create(anon_decl.arena(), buf);
88188818
};
88198819
const decl = try anon_decl.finish(final_ty, val, 0);
8820-
if (is_single_ptr) {
8820+
if (lhs_ty.zigTypeTag() == .Pointer) {
88218821
return sema.analyzeDeclRef(decl);
88228822
} else {
88238823
return sema.analyzeDeclVal(block, .unneeded, decl);

test/behavior/slice.zig

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,27 @@ test "type coercion of pointer to anon struct literal to pointer to slice" {
583583
comptime try S.doTheTest();
584584
}
585585

586-
test "array concat of slices gives slice" {
587-
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
588-
586+
test "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+
599+
test "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

0 commit comments

Comments
 (0)