Is it true scope declarations are being removed?

bearophile bearophileHUGS at lycos.com
Mon Jan 24 13:59:40 PST 2011


Jonathan M Davis:

> Much of anything that requires code flow analysis doesn't tend to happen.

Right. You need (lot of) work to implement it, in a system language it can't be fully accurate, and it may slow down compilation a little (Scala compiler is powerful, but I don't think it's very fast).

But from using C lints I've seen that catching simple cases is better than catching none of them, and in SafeD the code is less wild so probably a higher percentage of cases can be caught.

Currently DMD catches only very simple errors:

class Foo {}
Foo foo() {
    scope Foo f = new Foo;
    return f; // test.d(4): Error: escaping reference to scope local f
}
Foo bar() {
    scope Foo f = new Foo;
    Foo f2 = f;
    return f2; // no error here
}
void main() {}


Something similar happens with this little C program compiled with -Wall with GCC:

int *foo() {
    int a[5];
    return a;
}
int *bar() {
    int a[5];
    int *p = a;
    return p;
}
int main() {
    return 0;
}

GCC warns only about the first function:
test.c: In function 'foo':
test.c:3:5: warning: function returns address of local variable

But a good C lint catches them both, statically (and the checking is very fast):
test.c  3  Warning 604:  Returning address of auto variable 'a'
test.c  8  Warning 674:  Returning address of auto through variable 'p'

Bye,
bearophile


More information about the Digitalmars-d mailing list