Summary: Catching more simple out-of-bounds errors at
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from 2010-09-21 05:07:24 PDT ---
One of the advantages of static typing is that it catches some classes of bugs
early, instead of later at runtime. Similarly, catching array out-of-bounds
errors early at compile-time is better than catching them at run-time in debug

Catching all cases of out-of-bounds errors at compile time is not possible and
it's hard to do, but there are simple cases that are common coding mistakes and
probably easy to catch at compile-time:

void main() {
    int[10] arr;
    for (int i = 0; i <= arr.length; i++)
        arr[i] = i;

In idiomatic D that kind of bugs is less common because explicitly bounded
loops are less common:

void main() {
    int[10] arr;
    foreach (i, ref x; arr)
        x = i;

But probably there are enough D programmers that don't use idiomatic D or
translate code from Java/C/C++/C# code that contains explicit loops.

Currently DMD is able to spot such out-of-bounds errors at compile-time only if
the index is a compile-time constant:

const int i = 6 / 2;
void main() {
    int[3] arr;
    arr[i] = 3; // Error: array index 3 is out of bounds arr[0 .. 3]

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to