From 6f9f83e7f2e01a9236d62563ed3691e222f55f5d Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Tue, 22 Mar 2022 19:26:25 -0700 Subject: [PATCH] stage2: concat/mult of slices yields ptr to array --- src/Sema.zig | 4 ++-- test/behavior/slice.zig | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index e6447ea2ef51..69572ac5b725 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -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); @@ -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); diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig index 6b72e169fa7e..432524ebf736 100644 --- a/test/behavior/slice.zig +++ b/test/behavior/slice.zig @@ -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); } }