diff --git a/releasenotes.md b/releasenotes.md index f143390e9..deb0074c0 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -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 diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 4c01626a3..502258653 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -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); } diff --git a/test/test_suite/expressions/fail_index_usize.c3 b/test/test_suite/expressions/fail_index_usize.c3 index 438d83cde..f1f113d5b 100644 --- a/test/test_suite/expressions/fail_index_usize.c3 +++ b/test/test_suite/expressions/fail_index_usize.c3 @@ -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 } diff --git a/test/test_suite/methods/subscript_set_error.c3 b/test/test_suite/methods/subscript_set_error.c3 new file mode 100644 index 000000000..723c936e1 --- /dev/null +++ b/test/test_suite/methods/subscript_set_error.c3 @@ -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; \ No newline at end of file