Skip to content

Commit

Permalink
fix compile time array concatenation for slices
Browse files Browse the repository at this point in the history
closes #866
  • Loading branch information
andrewrk committed Mar 29, 2018
1 parent 5627347 commit 032fccf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/ir.cpp
Expand Up @@ -11058,6 +11058,24 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
result_type = get_array_type(ira->codegen, child_type, new_len);

out_array_val = out_val;
} else if (is_slice(op1_type) || is_slice(op2_type)) {
TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, child_type, true);
result_type = get_slice_type(ira->codegen, ptr_type);
out_array_val = create_const_vals(1);
out_array_val->special = ConstValSpecialStatic;
out_array_val->type = get_array_type(ira->codegen, child_type, new_len);

out_val->data.x_struct.fields = create_const_vals(2);

out_val->data.x_struct.fields[slice_ptr_index].type = ptr_type;
out_val->data.x_struct.fields[slice_ptr_index].special = ConstValSpecialStatic;
out_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.special = ConstPtrSpecialBaseArray;
out_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.data.base_array.array_val = out_array_val;
out_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.data.base_array.elem_index = 0;

out_val->data.x_struct.fields[slice_len_index].type = ira->codegen->builtin_types.entry_usize;
out_val->data.x_struct.fields[slice_len_index].special = ConstValSpecialStatic;
bigint_init_unsigned(&out_val->data.x_struct.fields[slice_len_index].data.x_bigint, new_len);
} else {
new_len += 1; // null byte

Expand Down
12 changes: 11 additions & 1 deletion test/cases/eval.zig
@@ -1,4 +1,5 @@
const assert = @import("std").debug.assert;
const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");

test "compile time recursion" {
Expand Down Expand Up @@ -503,3 +504,12 @@ test "const ptr to comptime mutable data is not memoized" {
assert(foo.read_x() == 2);
}
}

test "array concat of slices gives slice" {
comptime {
var a: []const u8 = "aoeu";
var b: []const u8 = "asdf";
const c = a ++ b;
assert(std.mem.eql(u8, c, "aoeuasdf"));
}
}

0 comments on commit 032fccf

Please sign in to comment.