authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-12-28 00:03:03+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-31 14:23:49-07:00
log9a0010b186a281740af812452514b260b6063e1a
tree3bf4ec21a7714f0b7c8295a9869eea962153b1a0
parentb86aadfa385ad2af8df601c0c69fa2e45eacd0ff

stage1: fix access of slice sentinel at comptime


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

src/stage1/ir.cpp+5-1
......@@ -14982,12 +14982,16 @@ static Stage1AirInst *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, Stage1ZirI
1498214982 case ConstPtrSpecialSubArray:
1498314983 case ConstPtrSpecialBaseArray:
1498414984 {
14985 uint64_t array_len = ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.len;
14986 if (ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.sentinel != nullptr) {
14987 array_len += 1;
14988 }
1498514989 size_t offset = ptr_field->data.x_ptr.data.base_array.elem_index;
1498614990 uint64_t new_index = offset + index;
1498714991 if (ptr_field->data.x_ptr.data.base_array.array_val->data.x_array.special !=
1498814992 ConstArraySpecialBuf)
1498914993 {
14990 if (new_index >= ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.len) {
14994 if (new_index >= array_len) {
1499114995 ir_add_error_node(ira, elem_ptr_instruction->base.source_node, buf_sprintf("out of bounds slice"));
1499214996 return ira->codegen->invalid_inst_gen;
1499314997 }
test/behavior/slice_stage1.zig+18
......@@ -339,3 +339,21 @@ test "slice bounds in comptime concatenation" {
339339 try expect(str2.len == 1);
340340 try expect(std.mem.eql(u8, str2, "1"));
341341}
342
343test "slice sentinel access at comptime" {
344 {
345 const str0 = &[_:0]u8{ '1', '2', '3' };
346 const slice0: [:0]const u8 = str0;
347
348 try expect(slice0.len == 3);
349 try expect(slice0[slice0.len] == 0);
350 }
351 {
352 const str0 = "123";
353 _ = &str0[0];
354 const slice0: [:0]const u8 = str0;
355
356 try expect(slice0.len == 3);
357 try expect(slice0[slice0.len] == 0);
358 }
359}