Diagnostic and cause
LinkIndex out of bounds
- Family
- topaz
- Phase
- run
- Code
TPZ4001
Cause and conditions
Reading an Array at a computed int index less than zero or at least the array length faults during execution.
Related rule: Trigger and repairResponse
Use an index from zero through length minus one, or test the index before the read. The repaired example reads index zero of a one-element Array.
Source evidence
- compiler/crates/topaz_diag/src/explain.rs at
151141d7bb44· 508-516 - compiler/crates/topaz_product_runtime/src/diagnostic.rs at
151141d7bb44· 54-54 - compiler/crates/topaz_value/src/lib.rs at
151141d7bb44· 161-161 - compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 687-724 - compiler/crates/topaz_product_runtime/src/runtime/evaluate.rs at
151141d7bb44· 434-442 - compiler/crates/topaz_value/src/lib.rs at
151141d7bb44· 157-164 - compiler/crates/topaz_product_runtime/src/diagnostic.rs at
151141d7bb44· 38-57 - compiler/crates/topaz_cli/src/compile/direct_run.rs at
151141d7bb44· 37-77 - TPZ4001 at
f42fbeb4f27e - TPZ4001 repair at
151141d7bb44
Compiler phase: runtime.
Compiler explanation: A runtime index operation addressed a position outside the collection/string bounds.
Message templates are Pending. The explanation above is not an observed CLI message.
Observed for the candidate in Topaz 5.21.3. Execution
- error[TPZ4001]: index 1 is out of bounds for an array of length 1
The evaluated int index is not a valid position in the Array. Array indices start at zero and must be less than the current length; invalid reads fault instead of clamping or wrapping.
Trigger and repair
Reading an Array at a computed int index less than zero or at least the array length faults during execution.
Source evidence
- compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 687-724 - compiler/crates/topaz_product_runtime/src/runtime/evaluate.rs at
151141d7bb44· 434-442 - compiler/crates/topaz_value/src/lib.rs at
151141d7bb44· 157-164 - compiler/crates/topaz_product_runtime/src/diagnostic.rs at
151141d7bb44· 38-57 - compiler/crates/topaz_cli/src/compile/direct_run.rs at
151141d7bb44· 37-77 - TPZ4001 at
f42fbeb4f27e - TPZ4001 repair at
151141d7bb44
Response
LinkUse an index from zero through length minus one, or test the index before the read. The repaired example reads index zero of a one-element Array.
Reproduction and verification
LinkProduct diagnostic test
Linkfunction readAt(index: int) -> int {let values = [10]values[index]}readAt(1)
Expected result
Expected result
The separate diagnostic product test validates this command, runtime diagnostic, or filesystem setup. The shared example runner does not execute that assertion plan.
Run topaz run example..
Expect exit status 1, empty standard output, and TPZ4001.
The evaluated int index is not a valid position in the Array. Array indices start at zero and must be less than the current length; invalid reads fault instead of clamping or wrapping.
The primary location is example..
Expect exactly one diagnostic.
The diagnostic product test checks these expectations with the same files and command.
Source evidence
- compiler/crates/topaz_diag/src/explain.rs at
151141d7bb44· 508-516 - compiler/crates/topaz_product_runtime/src/diagnostic.rs at
151141d7bb44· 54-54 - compiler/crates/topaz_value/src/lib.rs at
151141d7bb44· 161-161 - compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 687-724 - compiler/crates/topaz_product_runtime/src/runtime/evaluate.rs at
151141d7bb44· 434-442 - compiler/crates/topaz_value/src/lib.rs at
151141d7bb44· 157-164 - compiler/crates/topaz_product_runtime/src/diagnostic.rs at
151141d7bb44· 38-57 - compiler/crates/topaz_cli/src/compile/direct_run.rs at
151141d7bb44· 37-77 - TPZ4001 at
f42fbeb4f27e - TPZ4001 repair at
151141d7bb44
Corrected program
Linkfunction readAt(index: int) -> int {let values = [10]values[index]}readAt(0)
No stdout.
Expected result
Expected result
Execution completes. Exit code 0.
This program applies the repair described above. The verification status records whether it has been run.
Source evidence
- compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 687-724 - compiler/crates/topaz_product_runtime/src/runtime/evaluate.rs at
151141d7bb44· 434-442 - compiler/crates/topaz_value/src/lib.rs at
151141d7bb44· 157-164 - compiler/crates/topaz_product_runtime/src/diagnostic.rs at
151141d7bb44· 38-57 - compiler/crates/topaz_cli/src/compile/direct_run.rs at
151141d7bb44· 37-77 - TPZ4001 at
f42fbeb4f27e - TPZ4001 repair at
151141d7bb44