Commit 9a0010b186
2021-12-27 23:03:03
1 parent
b86aadfChanged files (2)
src
stage1
test
behavior
src/stage1/ir.cpp
@@ -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;
}
test/behavior/slice_stage1.zig
@@ -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);
+ }
+}