authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-28 23:25:12-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-28 23:25:12-04:00
log032fccf6151ff201ce4b8c7ab28ca460fed794c0
treec784d6f1359e765f640dea70bdc71b77eaec0756
parent5627347bab01fa767c29d1434fcd6a600c98811a

fix compile time array concatenation for slices

closes #866

2 files changed, 29 insertions(+), 1 deletions(-)

src/ir.cpp+18
......@@ -11058,6 +11058,24 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
1105811058 result_type = get_array_type(ira->codegen, child_type, new_len);
1105911059
1106011060 out_array_val = out_val;
11061 } else if (is_slice(op1_type) || is_slice(op2_type)) {
11062 TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, child_type, true);
11063 result_type = get_slice_type(ira->codegen, ptr_type);
11064 out_array_val = create_const_vals(1);
11065 out_array_val->special = ConstValSpecialStatic;
11066 out_array_val->type = get_array_type(ira->codegen, child_type, new_len);
11067
11068 out_val->data.x_struct.fields = create_const_vals(2);
11069
11070 out_val->data.x_struct.fields[slice_ptr_index].type = ptr_type;
11071 out_val->data.x_struct.fields[slice_ptr_index].special = ConstValSpecialStatic;
11072 out_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.special = ConstPtrSpecialBaseArray;
11073 out_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.data.base_array.array_val = out_array_val;
11074 out_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.data.base_array.elem_index = 0;
11075
11076 out_val->data.x_struct.fields[slice_len_index].type = ira->codegen->builtin_types.entry_usize;
11077 out_val->data.x_struct.fields[slice_len_index].special = ConstValSpecialStatic;
11078 bigint_init_unsigned(&out_val->data.x_struct.fields[slice_len_index].data.x_bigint, new_len);
1106111079 } else {
1106211080 new_len += 1; // null byte
1106311081
test/cases/eval.zig+11-1
......@@ -1,4 +1,5 @@
1const assert = @import("std").debug.assert;
1const std = @import("std");
2const assert = std.debug.assert;
23const builtin = @import("builtin");
34
45test "compile time recursion" {
......@@ -503,3 +504,12 @@ test "const ptr to comptime mutable data is not memoized" {
503504 assert(foo.read_x() == 2);
504505 }
505506}
507
508test "array concat of slices gives slice" {
509 comptime {
510 var a: []const u8 = "aoeu";
511 var b: []const u8 = "asdf";
512 const c = a ++ b;
513 assert(std.mem.eql(u8, c, "aoeuasdf"));
514 }
515}