stage1: fix access of slice sentinel at comptime

This commit is contained in:
Veikka Tuominen
2021-12-28 00:03:03 +02:00
parent 4b9b9e7257
commit 4f4f0bc6f0
2 changed files with 23 additions and 1 deletions

View File

@@ -14982,12 +14982,16 @@ static Stage1AirInst *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, Stage1ZirI
case ConstPtrSpecialSubArray:
case ConstPtrSpecialBaseArray:
{
uint64_t array_len = ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.len;
if (ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.sentinel != nullptr) {
array_len += 1;
}
size_t offset = ptr_field->data.x_ptr.data.base_array.elem_index;
uint64_t new_index = offset + index;
if (ptr_field->data.x_ptr.data.base_array.array_val->data.x_array.special !=
ConstArraySpecialBuf)
{
if (new_index >= ptr_field->data.x_ptr.data.base_array.array_val->type->data.array.len) {
if (new_index >= array_len) {
ir_add_error_node(ira, elem_ptr_instruction->base.source_node, buf_sprintf("out of bounds slice"));
return ira->codegen->invalid_inst_gen;
}

View File

@@ -339,3 +339,21 @@ test "slice bounds in comptime concatenation" {
try expect(str2.len == 1);
try expect(std.mem.eql(u8, str2, "1"));
}
test "slice sentinel access at comptime" {
{
const str0 = &[_:0]u8{ '1', '2', '3' };
const slice0: [:0]const u8 = str0;
try expect(slice0.len == 3);
try expect(slice0[slice0.len] == 0);
}
{
const str0 = "123";
_ = &str0[0];
const slice0: [:0]const u8 = str0;
try expect(slice0.len == 3);
try expect(slice0[slice0.len] == 0);
}
}