zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit b117b5907c50f495c53770bfc351e0431e6474b3 (tree)
parent 0683bd8bf66c00d38f79d49319b5d80ac1f9a470
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sun, 10 Apr 2016 13:18:42 -0700

add error for accessing empty array

closes #134

Diffstat:
Msrc/analyze.cpp | 3+++
Mtest/run_tests.cpp | 7+++++++
2 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/analyze.cpp b/src/analyze.cpp @@ -2445,6 +2445,9 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i if (array_type->id == TypeTableEntryIdInvalid) { return_type = g->builtin_types.entry_invalid; } else if (array_type->id == TypeTableEntryIdArray) { + if (array_type->data.array.len == 0) { + add_node_error(g, node, buf_sprintf("out of bounds array access")); + } return_type = array_type->data.array.child_type; } else if (array_type->id == TypeTableEntryIdPointer) { return_type = array_type->data.pointer.child_type; diff --git a/test/run_tests.cpp b/test/run_tests.cpp @@ -1814,6 +1814,13 @@ fn derp(){} add_compile_fail_case("assign null to non-nullable pointer", R"SOURCE( const a: &u8 = null; )SOURCE", 1, ".tmp_source.zig:2:16: error: expected maybe type, got '&u8'"); + + add_compile_fail_case("indexing an array of size zero", R"SOURCE( +const array = []u8{}; +fn foo() { + const pointer = &array[0]; +} + )SOURCE", 1, ".tmp_source.zig:4:27: error: out of bounds array access"); } //////////////////////////////////////////////////////////////////////////////