Skip to content

Commit 868dca8

Browse files
committed
stage2: concat/mult of slices should yield slice
1 parent cb63646 commit 868dca8

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/Sema.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8499,6 +8499,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
84998499
const tracy = trace(@src());
85008500
defer tracy.end();
85018501

8502+
const target = sema.mod.getTarget();
85028503
const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
85038504
const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
85048505
const lhs = sema.resolveInst(extra.lhs);
@@ -8573,6 +8574,16 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
85738574
const decl = try anon_decl.finish(ty, val, 0);
85748575
if (lhs_single_ptr or rhs_single_ptr) {
85758576
return sema.analyzeDeclRef(decl);
8577+
} else if (lhs_ty.isSlice() or rhs_ty.isSlice()) {
8578+
const slice_ty = try Type.ptr(sema.arena, target, .{
8579+
.pointee_type = decl.ty.childType(),
8580+
.sentinel = decl.ty.sentinel(),
8581+
.@"addrspace" = .generic,
8582+
.mutable = false,
8583+
.size = .Slice,
8584+
});
8585+
const ptr_to_array = try sema.analyzeDeclRef(decl);
8586+
return sema.coerceArrayPtrToSlice(block, slice_ty, ptr_to_array, .unneeded);
85768587
} else {
85778588
return sema.analyzeDeclVal(block, .unneeded, decl);
85788589
}
@@ -8677,6 +8688,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
86778688
const tracy = trace(@src());
86788689
defer tracy.end();
86798690

8691+
const target = sema.mod.getTarget();
86808692
const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
86818693
const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
86828694
const lhs = sema.resolveInst(extra.lhs);
@@ -8746,6 +8758,16 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
87468758
const decl = try anon_decl.finish(final_ty, val, 0);
87478759
if (is_single_ptr) {
87488760
return sema.analyzeDeclRef(decl);
8761+
} else if (lhs_ty.isSlice()) {
8762+
const slice_ty = try Type.ptr(sema.arena, target, .{
8763+
.pointee_type = decl.ty.childType(),
8764+
.sentinel = decl.ty.sentinel(),
8765+
.@"addrspace" = .generic,
8766+
.mutable = false,
8767+
.size = .Slice,
8768+
});
8769+
const ptr_to_array = try sema.analyzeDeclRef(decl);
8770+
return sema.coerceArrayPtrToSlice(block, slice_ty, ptr_to_array, .unneeded);
87498771
} else {
87508772
return sema.analyzeDeclVal(block, .unneeded, decl);
87518773
}

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)