From 0a7062e2d711b5ac7fb71e873f293a8f0d0e8bc6 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 1 Sep 2025 12:43:08 -0400 Subject: Improved error messages --- src/stdlib/lists.h | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/stdlib/lists.h') diff --git a/src/stdlib/lists.h b/src/stdlib/lists.h index 74314aa6..1386b2fa 100644 --- a/src/stdlib/lists.h +++ b/src/stdlib/lists.h @@ -10,6 +10,16 @@ #include "util.h" // Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1 +#define List_get_checked(list_expr, index_expr, item_type, start, end) \ + ({ \ + const List_t list = list_expr; \ + int64_t index = index_expr; \ + int64_t off = index + (index < 0) * (list.length + 1) - 1; \ + if (unlikely(off < 0 || off >= list.length)) \ + fail_source(__SOURCE_FILE__, start, end, "Invalid list index: ", index, " (list has length ", \ + (int64_t)list.length, ")\n"); \ + *(item_type *)(list.data + list.stride * off); \ + }) #define List_get(list_expr, index_expr, item_type, var, optional_expr, none_expr) \ ({ \ const List_t list = list_expr; \ -- cgit v1.2.3