Commit c1cc1ebc35
src/ir.cpp
@@ -20880,13 +20880,8 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
if (instr_is_comptime(casted_elem_index)) {
uint64_t index = bigint_as_u64(&casted_elem_index->value->data.x_bigint);
if (array_type->id == ZigTypeIdArray) {
- uint64_t array_len = array_type->data.array.len;
- if (index == array_len && array_type->data.array.sentinel != nullptr) {
- ZigType *elem_type = array_type->data.array.child_type;
- IrInstGen *sentinel_elem = ir_const(ira, &elem_ptr_instruction->base.base, elem_type);
- copy_const_val(ira->codegen, sentinel_elem->value, array_type->data.array.sentinel);
- return ir_get_ref(ira, &elem_ptr_instruction->base.base, sentinel_elem, true, false);
- }
+ uint64_t array_len = array_type->data.array.len +
+ (array_type->data.array.sentinel != nullptr);
if (index >= array_len) {
ir_add_error_node(ira, elem_ptr_instruction->base.base.source_node,
buf_sprintf("index %" ZIG_PRI_u64 " outside array of size %" ZIG_PRI_u64,
test/stage1/behavior/array.zig
@@ -31,14 +31,23 @@ fn getArrayLen(a: []const u32) usize {
test "array with sentinels" {
const S = struct {
fn doTheTest(is_ct: bool) void {
- var zero_sized: [0:0xde]u8 = [_:0xde]u8{};
- expectEqual(@as(u8, 0xde), zero_sized[0]);
- // Disabled at runtime because of
- // https://github.com/ziglang/zig/issues/4372
if (is_ct) {
+ var zero_sized: [0:0xde]u8 = [_:0xde]u8{};
+ // Disabled at runtime because of
+ // https://github.com/ziglang/zig/issues/4372
+ expectEqual(@as(u8, 0xde), zero_sized[0]);
var reinterpreted = @ptrCast(*[1]u8, &zero_sized);
expectEqual(@as(u8, 0xde), reinterpreted[0]);
}
+ var arr: [3:0x55]u8 = undefined;
+ // Make sure the sentinel pointer is pointing after the last element
+ if (!is_ct) {
+ const sentinel_ptr = @ptrToInt(&arr[3]);
+ const last_elem_ptr = @ptrToInt(&arr[2]);
+ expectEqual(@as(usize, 1), sentinel_ptr - last_elem_ptr);
+ }
+ // Make sure the sentinel is writeable
+ arr[3] = 0x55;
}
};
@@ -372,7 +381,7 @@ test "access the null element of a null terminated array" {
const S = struct {
fn doTheTest() void {
var array: [4:0]u8 = .{ 'a', 'o', 'e', 'u' };
- comptime expect(array[4] == 0);
+ expect(array[4] == 0);
var len: usize = 4;
expect(array[len] == 0);
}