From 9e5cd43e6d2caaaa2226be56c0886d73a200e78b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 21 Sep 2018 15:40:02 -0400 Subject: [PATCH] fix comptime string concatenation ignoring slice bounds closes #1362 --- src/ir.cpp | 5 ++++- test/cases/eval.zig | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ir.cpp b/src/ir.cpp index 5e0f79ec13..c9bc4a48ef 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -12399,7 +12399,8 @@ static ZigType *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruc assert(ptr_val->data.x_ptr.special == ConstPtrSpecialBaseArray); op1_array_val = ptr_val->data.x_ptr.data.base_array.array_val; op1_array_index = ptr_val->data.x_ptr.data.base_array.elem_index; - op1_array_end = op1_array_val->type->data.array.len; + ConstExprValue *len_val = &op1_val->data.x_struct.fields[slice_len_index]; + op1_array_end = bigint_as_unsigned(&len_val->data.x_bigint); } else { ir_add_error(ira, op1, buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name))); @@ -12446,6 +12447,8 @@ static ZigType *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruc op2_array_val = ptr_val->data.x_ptr.data.base_array.array_val; op2_array_index = ptr_val->data.x_ptr.data.base_array.elem_index; op2_array_end = op2_array_val->type->data.array.len; + ConstExprValue *len_val = &op2_val->data.x_struct.fields[slice_len_index]; + op2_array_end = bigint_as_unsigned(&len_val->data.x_bigint); } else { ir_add_error(ira, op2, buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op2->value.type->name))); diff --git a/test/cases/eval.zig b/test/cases/eval.zig index 6e64a64aee..e0c998e961 100644 --- a/test/cases/eval.zig +++ b/test/cases/eval.zig @@ -723,3 +723,17 @@ test "comptime pointer cast array and then slice" { assert(sliceA[1] == 2); assert(sliceB[1] == 2); } + +test "slice bounds in comptime concatenation" { + const bs = comptime blk: { + const b = c"11"; + break :blk b[0..1]; + }; + const str = "" ++ bs; + assert(str.len == 1); + assert(std.mem.eql(u8, str, "1")); + + const str2 = bs ++ ""; + assert(str2.len == 1); + assert(std.mem.eql(u8, str2, "1")); +}