Fix sema errors on flexible array slices.

This commit is contained in:
Christoffer Lerno
2023-03-22 12:56:15 +01:00
parent 316af36723
commit 0f80d985fa
2 changed files with 23 additions and 6 deletions

View File

@@ -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:

View File

@@ -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()