www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4907] New: Catching more simple out-of-bounds errors at compile-time

http://d.puremagic.com/issues/show_bug.cgi?id=4907

           Summary: Catching more simple out-of-bounds errors at
                    compile-time
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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
builds.

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



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: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 21 2010