From 292bf1cbbc75e32f61954c5725c5f36c1187ac63 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 3 Jan 2026 02:32:40 +0100 Subject: [PATCH] Designated initialization with ranges would not error on overflow by 1. --- releasenotes.md | 1 + src/compiler/sema_initializers.c | 4 ++-- .../bitstruct/designated_init_range_overflow.c3 | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/test_suite/bitstruct/designated_init_range_overflow.c3 diff --git a/releasenotes.md b/releasenotes.md index 5236e213c..e4d0d8f8a 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -48,6 +48,7 @@ - `String.tokenize_all` would yield one too many empty tokens at the end. - `String.replace` no longer depends on `String.split`. - Fix the case where `\u` could crash the compiler on some platforms. +- Designated initialization with ranges would not error on overflow by 1. ### Stdlib changes - Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads. diff --git a/src/compiler/sema_initializers.c b/src/compiler/sema_initializers.c index 3736bc42b..b8804f6f8 100644 --- a/src/compiler/sema_initializers.c +++ b/src/compiler/sema_initializers.c @@ -1377,10 +1377,10 @@ static Type *sema_find_type_of_element(SemaContext *context, Type *type, Designa *did_report_error = true; return NULL; } - if (end_index > (ArrayIndex)len) + if (end_index >= (ArrayIndex)len) { *did_report_error = true; - SEMA_ERROR(element->index_expr, "The index may must be less than the array length (which was %llu).", (unsigned long long)len); + SEMA_ERROR(element->index_end_expr, "The index must be less than the array length (which was %llu).", (unsigned long long)len); return NULL; } element->index_end = end_index; diff --git a/test/test_suite/bitstruct/designated_init_range_overflow.c3 b/test/test_suite/bitstruct/designated_init_range_overflow.c3 new file mode 100644 index 000000000..f056bbef8 --- /dev/null +++ b/test/test_suite/bitstruct/designated_init_range_overflow.c3 @@ -0,0 +1,16 @@ +enum Row : inline int { A, B, C, D, E, F } + +int[3][6] m = { + [Row.E] = { + [0..3] = 1, // #error: The index must be less than the array length (which was 3) + }, + [Row.F] = { + [1] = 2, + }, +}; + +fn int main() +{ + (void)m; + return 0; +} \ No newline at end of file