Confusing error message when type has [] overloaded but not []= #2453

This commit is contained in:
Christoffer Lerno
2025-09-02 23:56:15 +02:00
parent 02d1486af9
commit 14a929588a
4 changed files with 23 additions and 4 deletions

View File

@@ -6,6 +6,7 @@
### Fixes
- Compiler assert with var x @noinit = 0 #2452
- Confusing error message when type has [] overloaded but not []= #2453
### Stdlib changes

View File

@@ -3807,11 +3807,15 @@ static inline bool sema_expr_resolve_subscript_index(SemaContext *context, Expr
if (!subscript_type)
{
if (check_valid) return false;
if (overload_type == OVERLOAD_ELEMENT_REF)
switch (overload_type)
{
RETURN_SEMA_ERROR(expr, "Getting a reference to a subscript of %s is not possible.", type_quoted_error_string(subscripted->type));
case OVERLOAD_ELEMENT_REF:
RETURN_SEMA_ERROR(expr, "Getting a reference to a subscript of %s is not possible.", type_quoted_error_string(subscripted->type));
case OVERLOAD_ELEMENT_SET:
RETURN_SEMA_ERROR(expr, "Assigning to a subscript of %s is not possible.", type_quoted_error_string(subscripted->type));
default:
RETURN_SEMA_ERROR(expr, "Indexing a value of type %s is not possible.", type_quoted_error_string(subscripted->type));
}
RETURN_SEMA_ERROR(expr, "Indexing a value of type %s is not possible.", type_quoted_error_string(subscripted->type));
}
if (!overload) current_type = type_flatten(current_expr->type);
}

View File

@@ -1,5 +1,5 @@
fn void test(int* array, usz n)
{
array[n] = 33;
n[array] = 33; // #error: Indexing a value of type
n[array] = 33; // #error: Assigning to a subscript of
}

View File

@@ -0,0 +1,14 @@
fn int main(String[] args)
{
MyArray a = {{1, 4, 3, 6, 5}};
a[0] = a[1]; // #error: Assigning to a subscript of 'MyArray' is not possible
return 0;
}
struct MyArray
{
int[] val;
}
fn int MyArray.get(self, usz idx) @operator([]) => self.val[idx];
fn usz MyArray.len(self) @operator(len) => self.val.len;