Wave icon indicating copy to clipboard operation
Wave copied to clipboard

Diagnose constant out-of-range fixed-array indices

Open LunaStev opened this issue 1 month ago • 0 comments

Motivation

The frontend knows the declared array length but currently discards it during index validation. Obvious fixed-array mistakes reach code generation without a diagnostic.

At 7a8361d5c10ec3e66fc9fe4a5801622afb6bf631, the public v0.2.1-pre-beta compiler accepts both:

fun main() -> i32 {
    var values: array<i32, 1> = [7];
    return values[2];
}

and var values: array<i32, 0>; return values[0]; with wavec check (exit 0). This audit did not execute the out-of-range memory accesses.

Starting point: validate_expr_index_access.

Scope

Propose a source diagnostic when a fixed-size array and a compile-time index prove the access out of range. Reuse the existing constant evaluator, including integer signedness and conversions. Do not infer a bound for raw pointers or introduce runtime bounds checks as part of this issue. Difficulty: medium.

Agree on error versus warning, unreachable-code handling and pointer-to-array treatment first. Please mention @LunaStev to discuss the design before starting implementation.

Completion criteria

  • [ ] The agreed diagnostic covers negative indices, index equal to length, zero-length arrays and constant references/casts.
  • [ ] Valid boundary indices and ordinary pointer arithmetic retain their behavior.
  • [ ] Check/build diagnostics use the index's source span and work in JSON mode.

LunaStev avatar Oct 05 '26 12:10 LunaStev