Skip to content

Commit 742826f

Browse files
committed
stage2: concat/mult of slices should yield slice
1 parent e8813b2 commit 742826f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Sema.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8644,6 +8644,16 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
86448644
const decl = try anon_decl.finish(ty, val, 0);
86458645
if (lhs_single_ptr or rhs_single_ptr) {
86468646
return sema.analyzeDeclRef(decl);
8647+
} else if (lhs_ty.isSlice() or rhs_ty.isSlice()) {
8648+
const slice_ty = try Type.ptr(sema.arena, target, .{
8649+
.pointee_type = decl.ty.childType(),
8650+
.sentinel = decl.ty.sentinel(),
8651+
.@"addrspace" = .generic,
8652+
.mutable = false,
8653+
.size = .Slice,
8654+
});
8655+
const ptr_to_array = try sema.analyzeDeclRef(decl);
8656+
return sema.coerceArrayPtrToSlice(block, slice_ty, ptr_to_array, .unneeded);
86478657
} else {
86488658
return sema.analyzeDeclVal(block, .unneeded, decl);
86498659
}
@@ -8819,6 +8829,16 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
88198829
const decl = try anon_decl.finish(final_ty, val, 0);
88208830
if (is_single_ptr) {
88218831
return sema.analyzeDeclRef(decl);
8832+
} else if (lhs_ty.isSlice()) {
8833+
const slice_ty = try Type.ptr(sema.arena, target, .{
8834+
.pointee_type = decl.ty.childType(),
8835+
.sentinel = decl.ty.sentinel(),
8836+
.@"addrspace" = .generic,
8837+
.mutable = false,
8838+
.size = .Slice,
8839+
});
8840+
const ptr_to_array = try sema.analyzeDeclRef(decl);
8841+
return sema.coerceArrayPtrToSlice(block, slice_ty, ptr_to_array, .unneeded);
88228842
} else {
88238843
return sema.analyzeDeclVal(block, .unneeded, decl);
88248844
}

test/behavior/slice.zig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,6 @@ test "type coercion of pointer to anon struct literal to pointer to slice" {
584584
}
585585

586586
test "array concat of slices gives slice" {
587-
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
588-
589587
comptime {
590588
var a: []const u8 = "aoeu";
591589
var b: []const u8 = "asdf";
@@ -594,6 +592,16 @@ test "array concat of slices gives slice" {
594592
}
595593
}
596594

595+
test "array mult of slice gives slice" {
596+
if (builtin.zig_backend == .stage1) return error.SkipZigTest; // Stage 1 does not support multiplying slices
597+
598+
comptime {
599+
var a: []const u8 = "aoeu";
600+
const c = a ** 2;
601+
try expect(std.mem.eql(u8, c, "aoeuaoeu"));
602+
}
603+
}
604+
597605
test "slice bounds in comptime concatenation" {
598606
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
599607

0 commit comments

Comments
 (0)