From 0f80d985fa9ba927e7a27a9c73353120f17ea0cc Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Wed, 22 Mar 2023 12:56:15 +0100 Subject: [PATCH] Fix sema errors on flexible array slices. --- src/compiler/sema_expr.c | 4 ++- .../subarrays/slice_negative_len.c3 | 25 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 8c52e6907..313382e35 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -2223,9 +2223,11 @@ static bool sema_slice_index_is_in_range(SemaContext *context, Type *type, Expr switch (type->type_kind) { case TYPE_POINTER: - case TYPE_FLEXIBLE_ARRAY: assert(!from_end); return true; + case TYPE_FLEXIBLE_ARRAY: + assert(!from_end); + break; case TYPE_UNTYPED_LIST: case TYPE_ARRAY: case TYPE_VECTOR: diff --git a/test/test_suite/subarrays/slice_negative_len.c3 b/test/test_suite/subarrays/slice_negative_len.c3 index e9e44f2f7..58e1ae00a 100644 --- a/test/test_suite/subarrays/slice_negative_len.c3 +++ b/test/test_suite/subarrays/slice_negative_len.c3 @@ -41,13 +41,13 @@ fn void test6() fn void test7() { int[3] x = { 1, 2, 3 }; - int[] z = x[-1..]; // #error: Index out of bounds, using a negative index is only allowed for pointers. + int[] z = x[-1..]; // #error: Index out of bounds, using a negative index is only allowed for pointers } fn void test8() { int[3] x = { 1, 2, 3 }; - int[] z = x[^4..]; // #error: Index out of bounds, using a negative index is only allowed for pointers. + int[] z = x[^4..]; // #error: Index out of bounds, using a negative index is only allowed for pointers } fn void test9() @@ -60,9 +60,24 @@ fn void test10() { int* x = null; x[-10..-3]; - int[] w = x[0..]; // #error: Omitting end index is not allowed for pointers. - int[] z = x[^2..]; // #error: Indexing from the end is not allowed for pointers. - int[] y = x[..^2]; // #error: Indexing from the end is not allowed for pointers. + int[] w = x[0..]; // #error: Omitting end index is not allowed for pointers + int[] z = x[^2..]; // #error: Indexing from the end is not allowed for pointers + int[] y = x[..^2]; // #error: Indexing from the end is not allowed for pointers +} + +struct Abc +{ + int a; + char[*] z; +} + +fn void test105() +{ + Abc a; + int[] w = a.z[0..]; // #error: Omitting end index is not allowed for pointers + int[] z = a.z[^2..]; // #error: Indexing from the end is not allowed for pointers + int[] y = a.z[..^2]; // #error: Indexing from the end is not allowed for pointers + a.z[-10..-3]; // #error: Index out of bounds, using a negative index is only allowed for pointers } fn void test11()