authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-10 13:18:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-10 13:18:42-07:00
logb117b5907c50f495c53770bfc351e0431e6474b3
tree38565c201e7dd562fc10db66f9f15b56d424a2d1
parent0683bd8bf66c00d38f79d49319b5d80ac1f9a470

add error for accessing empty array

closes #134

2 files changed, 10 insertions(+), 0 deletions(-)

src/analyze.cpp+3
...@@ -2445,6 +2445,9 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i...@@ -2445,6 +2445,9 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i
2445 if (array_type->id == TypeTableEntryIdInvalid) {2445 if (array_type->id == TypeTableEntryIdInvalid) {
2446 return_type = g->builtin_types.entry_invalid;2446 return_type = g->builtin_types.entry_invalid;
2447 } else if (array_type->id == TypeTableEntryIdArray) {2447 } else if (array_type->id == TypeTableEntryIdArray) {
2448 if (array_type->data.array.len == 0) {
2449 add_node_error(g, node, buf_sprintf("out of bounds array access"));
2450 }
2448 return_type = array_type->data.array.child_type;2451 return_type = array_type->data.array.child_type;
2449 } else if (array_type->id == TypeTableEntryIdPointer) {2452 } else if (array_type->id == TypeTableEntryIdPointer) {
2450 return_type = array_type->data.pointer.child_type;2453 return_type = array_type->data.pointer.child_type;
test/run_tests.cpp+7
...@@ -1814,6 +1814,13 @@ fn derp(){}...@@ -1814,6 +1814,13 @@ fn derp(){}
1814 add_compile_fail_case("assign null to non-nullable pointer", R"SOURCE(1814 add_compile_fail_case("assign null to non-nullable pointer", R"SOURCE(
1815const a: &u8 = null;1815const a: &u8 = null;
1816 )SOURCE", 1, ".tmp_source.zig:2:16: error: expected maybe type, got '&u8'");1816 )SOURCE", 1, ".tmp_source.zig:2:16: error: expected maybe type, got '&u8'");
1817
1818 add_compile_fail_case("indexing an array of size zero", R"SOURCE(
1819const array = []u8{};
1820fn foo() {
1821 const pointer = &array[0];
1822}
1823 )SOURCE", 1, ".tmp_source.zig:4:27: error: out of bounds array access");
1817}1824}
18181825
1819//////////////////////////////////////////////////////////////////////////////1826//////////////////////////////////////////////////////////////////////////////