Skip to content

stage2: concat/mult of slices should yield a ptr to an array #11270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8642,7 +8642,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
});
const val = try Value.Tag.aggregate.create(anon_decl.arena(), buf);
const decl = try anon_decl.finish(ty, val, 0);
if (lhs_single_ptr or rhs_single_ptr) {
if (lhs_ty.zigTypeTag() == .Pointer or rhs_ty.zigTypeTag() == .Pointer) {
return sema.analyzeDeclRef(decl);
} else {
return sema.analyzeDeclVal(block, .unneeded, decl);
Expand Down Expand Up @@ -8817,7 +8817,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
break :blk try Value.Tag.aggregate.create(anon_decl.arena(), buf);
};
const decl = try anon_decl.finish(final_ty, val, 0);
if (is_single_ptr) {
if (lhs_ty.zigTypeTag() == .Pointer) {
return sema.analyzeDeclRef(decl);
} else {
return sema.analyzeDeclVal(block, .unneeded, decl);
Expand Down
19 changes: 16 additions & 3 deletions test/behavior/slice.zig
Original file line number Diff line number Diff line change
Expand Up @@ -583,14 +583,27 @@ test "type coercion of pointer to anon struct literal to pointer to slice" {
comptime try S.doTheTest();
}

test "array concat of slices gives slice" {
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO

test "array concat of slices gives ptr to array" {
comptime {
var a: []const u8 = "aoeu";
var b: []const u8 = "asdf";
const c = a ++ b;
try expect(std.mem.eql(u8, c, "aoeuasdf"));
if (builtin.zig_backend != .stage1) {
// spec change: array concat now returns pointer-to-array for slices
try expect(@TypeOf(c) == *const [8]u8);
}
}
}

test "array mult of slice gives ptr to array" {
if (builtin.zig_backend == .stage1) return error.SkipZigTest; // Stage 1 does not support multiplying slices

comptime {
var a: []const u8 = "aoeu";
const c = a ** 2;
try expect(std.mem.eql(u8, c, "aoeuaoeu"));
try expect(@TypeOf(c) == *const [8]u8);
}
}

Expand Down