No bounds checking for dynamic arrays at compile time?

bearophile bearophileHUGS at lycos.com
Thu Dec 13 13:52:38 PST 2012


Walter Bright:

> For errors, what I try to do is look at the kinds of patterns 
> of error that are commonplace, and try to devise ways to head 
> them off.

This was a bug commonly found, I think you accepted it, but it's 
not fixed yet. I hope it's not forgotten, it's a little breaking 
change:

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

- - - - - - - - - - - - - - - -

Some other common bug patterns:

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

class Foo {
     int x, y;
     this(int x_, int y_) {
         this.x = x;
         y = y;

     }
}
void main() {}

- - - - - - - - - - - - - - - -

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

Arguments and members with the same name:

class Foo {
     int x;
     this(int x) { x = x; }
     void inc(int x) { this.x += x; }
}
class Bar {
     int x;
     this() { x = 5; }
}
struct Spam {
     static int x;
     void inc(int x) { Spam.x += x; }
}
void main() {}

- - - - - - - - - - - - - - - -

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

C# refuses code similar to this:


public class Foo {
     public int x = 10;
}
public class Test : Foo {
     public int x = 20;
}
void main() {}

- - - - - - - - - - - - - - - -

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


class Foo {
     int[] args;
     this(int[] args_...) {
         args = args_;
     }
}
Foo foo() {
     return new Foo(1, 2, 3); // passes stack data to Foo
}
void main() {
     assert(foo().args == [1, 2, 3]);
}

- - - - - - - - - - - - - - - -

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


auto x1 = y1 ? z1 : w1; // OK
auto x2 = x0 + (y1 ? z1 : w1); // OK
auto x3 = (x0 + y1) ? z1 : w1; // OK
auto x4 = x0 + y1 ? z1 : w1; // Not good
auto x5 = y1 ? z1 : (y2 ? z2 : w2); // OK
auto x6 = y1 ? z1 : y2 ? z2 : w2; // Not good

- - - - - - - - - - - - - - - -

> Expending effort on better detection of errors that people 
> don't make is a waste of time.

I agree. Bugs 5409 and 8757 are demonstrably common in already 
debugged C/C++ code. Bug 5212 is a trap.


Now this issue is fixed:
http://d.puremagic.com/issues/show_bug.cgi?id=6883

So this code:

// program#1
void main() {
     int[5] x;
     x[x.length] = 1;
     x[$] = 1;
     enum size_t n = 2;
     x[x.length + n] = 2;
     x[$ + n] = 2;
}


Generates the errors:
test.d(3): Error: array index 5 is out of bounds x[0 .. 5]
test.d(4): Error: array index 5 is out of bounds x[0 .. 5]
test.d(6): Error: array index 7 is out of bounds x[0 .. 5]
test.d(7): Error: array index 7 is out of bounds x[0 .. 5]


If I keep the same code but I replace x with a dynamic array no 
compile-time errors are generated:

// program#2
void main() {
     auto x = new int[5];
     x[x.length] = 1;
     x[$] = 1;
     enum size_t n = 2;
     x[x.length + n] = 2;
     x[$ + n] = 2;
}


program#1 code that uses fixed-sized arrays is flagged as wrong 
at compile time. program#2 is equally wrong, why isn't it good to 
give the same compilation errors for all or part of those four 
cases in program#2? Do they need lot of special casing?

Bye,
bearophile


More information about the Digitalmars-d mailing list