From 2508a293769ba7208e36bf598187ee4846d39a21 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 23 Aug 2026 11:48:24 +0100 Subject: [PATCH] Correctly check constant value indexing on constants --- src/check_builtin.cpp | 4 ++-- src/check_expr.cpp | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index bd3528a82..34cf19f43 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1302,7 +1302,7 @@ gb_internal bool check_builtin_simd_operation(CheckerContext *c, Operand *operan Type *elem = base_array_type(x.type); i64 max_count = x.type->SimdVector.count; i64 value = -1; - if (!check_index_value(c, x.type, false, ce->args[1], max_count, &value)) { + if (!check_index_value(c, &x, x.type, false, ce->args[1], max_count, &value)) { return false; } @@ -1323,7 +1323,7 @@ gb_internal bool check_builtin_simd_operation(CheckerContext *c, Operand *operan Type *elem = base_array_type(x.type); i64 max_count = x.type->SimdVector.count; i64 value = -1; - if (!check_index_value(c, x.type, false, ce->args[1], max_count, &value)) { + if (!check_index_value(c, &x, x.type, false, ce->args[1], max_count, &value)) { return false; } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index acb34d5a0..3562a850c 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -5429,7 +5429,7 @@ gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *tar operand->type = target_type; } -gb_internal bool check_index_value(CheckerContext *c, Type *main_type, bool open_range, Ast *index_value, i64 max_count, i64 *value, Type *type_hint=nullptr) { +gb_internal bool check_index_value(CheckerContext *c, Operand *indexable_operand, Type *main_type, bool open_range, Ast *index_value, i64 max_count, i64 *value, Type *type_hint=nullptr) { Operand operand = {Addressing_Invalid}; check_expr_with_type_hint(c, &operand, index_value, type_hint); if (operand.mode == Addressing_Invalid) { @@ -5469,7 +5469,9 @@ gb_internal bool check_index_value(CheckerContext *c, Type *main_type, bool open } if (operand.mode == Addressing_Constant && - (c->state_flags & StateFlag_no_bounds_check) == 0) { + ((c->state_flags & StateFlag_no_bounds_check) == 0 || + indexable_operand->mode == Addressing_Constant) + ) { BigInt i = exact_value_to_integer(operand.value).value_integer; if (i.sign && !is_type_enum(index_type) && !is_type_multi_pointer(main_type)) { TEMPORARY_ALLOCATOR_GUARD(); @@ -9692,8 +9694,8 @@ gb_internal void check_matrix_index_expr(CheckerContext *c, Operand *o, Ast *nod i64 row_index = 0; i64 column_index = 0; - bool row_ok = check_index_value(c, t, false, ie->row_index, row_count, &row_index, nullptr); - bool column_ok = check_index_value(c, t, false, ie->column_index, column_count, &column_index, nullptr); + bool row_ok = check_index_value(c, o, t, false, ie->row_index, row_count, &row_index, nullptr); + bool column_ok = check_index_value(c, o, t, false, ie->column_index, column_count, &column_index, nullptr); if (is_const && (ie->row_index->tav.mode != Addressing_Constant || ie->column_index->tav.mode != Addressing_Constant)) { error(o->expr, "Cannot index constant matrix with non-constant indices '%s'", expr_to_string(node)); } @@ -12164,7 +12166,7 @@ gb_internal ExprKind check_index_expr(CheckerContext *c, Operand *o, Ast *node, } i64 index = 0; - bool ok = check_index_value(c, t, false, ie->index, max_count, &index, index_type_hint); + bool ok = check_index_value(c, o, t, false, ie->index, max_count, &index, index_type_hint); if (is_const) { if (index < 0) { ERROR_BLOCK(); @@ -12354,7 +12356,7 @@ gb_internal ExprKind check_slice_expr(CheckerContext *c, Operand *o, Ast *node, capacity = max_count; } i64 j = 0; - if (check_index_value(c, t, true, nodes[i], capacity, &j)) { + if (check_index_value(c, o, t, true, nodes[i], capacity, &j)) { index = j; }