Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make max array count I64_MAX #4674

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ jobs:
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat
odin test tests/internal -all-packages -vet -strict-style -disallow-do -define:ODIN_TEST_FANCY=false -define:ODIN_TEST_FAIL_ON_BAD_MEMORY=true
- name: Check issues
shell: cmd
run: |
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat
cd tests/issues
call run.bat
- name: Check benchmarks
shell: cmd
run: |
Expand Down
14 changes: 12 additions & 2 deletions src/check_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4841,9 +4841,19 @@ gb_internal bool check_index_value(CheckerContext *c, Type *main_type, bool open

} else { // NOTE(bill): Do array bound checking
i64 v = -1;
if (i.used <= 1) {
v = big_int_to_i64(&i);

switch (i.used) {
case 0: v = 0; break;
case 1: v = big_int_to_i64(&i); break;
default:
static BigInt max = big_int_make_i64(I64_MAX);
if (big_int_cmp(&max, &i) > 0) {
v = big_int_to_i64(&i);
GB_ASSERT(v >= 0);
}
break;
}

if (value) *value = v;
bool out_of_bounds = false;
if (v < 0) {
Expand Down
23 changes: 18 additions & 5 deletions src/check_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2626,13 +2626,26 @@ gb_internal i64 check_array_count(CheckerContext *ctx, Operand *o, Ast *e) {
gb_free(a, str.text);
return 0;
}

switch (count.used) {
case 0: return 0;
case 1: return big_int_to_u64(&count);
}
gbAllocator a = heap_allocator();
String str = big_int_to_string(a, &count);
error(e, "Array count too large, %.*s", LIT(str));
case 1: return big_int_to_i64(&count);
default:
static BigInt max = big_int_make_i64(I64_MAX);
if (big_int_cmp(&max, &count) >= 0) {
i64 res = big_int_to_i64(&count);
GB_ASSERT(res >= 0);
return res;
}
break;
}

ERROR_BLOCK();

gbAllocator a = heap_allocator();
String str = big_int_to_string(a, &count);
error(e, "Array count too large, %.*s", LIT(str));
error_line("\tNote: The maximum array count is %td\n", I64_MAX);
gb_free(a, str.text);
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions tests/issues/run.bat
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set COMMON=-define:ODIN_TEST_FANCY=false -file -vet -strict-style
..\..\..\odin test ..\test_issue_2666.odin %COMMON% || exit /b
..\..\..\odin test ..\test_issue_4210.odin %COMMON% || exit /b
..\..\..\odin test ..\test_issue_4584.odin %COMMON% || exit /b
..\..\..\odin test ..\test_issue_4658.odin %COMMON% || exit /b

@echo off

Expand Down
1 change: 1 addition & 0 deletions tests/issues/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ $ODIN test ../test_issue_2637.odin $COMMON
$ODIN test ../test_issue_2666.odin $COMMON
$ODIN test ../test_issue_4210.odin $COMMON
$ODIN test ../test_issue_4584.odin $COMMON
$ODIN test ../test_issue_4658.odin $COMMON
if [[ $($ODIN build ../test_issue_2395.odin $COMMON 2>&1 >/dev/null | grep -c "Error:") -eq 2 ]] ; then
echo "SUCCESSFUL 1/1"
else
Expand Down
12 changes: 12 additions & 0 deletions tests/issues/test_issue_4658.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package issues

import "core:testing"

arr: [max(i64)]byte

@(test)
test_max_i64_array :: proc(t: ^testing.T) {
testing.expect_value(t, arr[max(i64)-1], 0)
arr[max(i64)-1] = 255
testing.expect_value(t, arr[max(i64)-1], 255)
}
Loading