Skip to content

Commit 6fc07f4

Browse files
topolarityandrewrk
authored andcommitted
stage2: concat/mult of slices yields ptr to array
1 parent 23bae38 commit 6fc07f4

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/Sema.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -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
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)