www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compiler does some flow analysis with -O..?

reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
Try this.

void main()
{
    int x = void;
    int y = 3;

    if(y < 10)
        x += 5;
    else
        x = 2;
}

Notice that x is uninitialized, so "x += 5;" shouldn't work.

If you compile this, even with -w, DMD happily accepts it.  But if you
throw -O, it gives the error:

dtest.d(187): Error: variable x used before set

This seems to be a relatively recent development, and doesn't seem to
be documented.  It's also very surprising that it only happens when -O
is thrown.

I like it a lot.  Could this functionality be formalized and extended
(i.e. to include accesses to variables declared like "int x;", like it
says in the spec)?
Apr 09 2009
next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 09 Apr 2009 18:19:02 +0400, Jarrett Billingsley
<jarrett.billingsley gmail.com> wrote:

 Try this.

 void main()
 {
     int x = void;
     int y = 3;

     if(y < 10)
         x += 5;
     else
         x = 2;
 }

 Notice that x is uninitialized, so "x += 5;" shouldn't work.

 If you compile this, even with -w, DMD happily accepts it.  But if you
 throw -O, it gives the error:

 dtest.d(187): Error: variable x used before set

 This seems to be a relatively recent development, and doesn't seem to
 be documented.  It's also very surprising that it only happens when -O
 is thrown.

 I like it a lot.  Could this functionality be formalized and extended
 (i.e. to include accesses to variables declared like "int x;", like it
 says in the spec)?

I believe this is just an optimization and not a general-purpose flow analysis: Step 0: int x = void; int y = 3; if (y < 10) x += 5; else x = 2; Step 1: int x = void; int y = 3; if (true) x += 5; else x = 2; Step2: int x = void; int y = 3; x += 5; // Error: variable x used before set
Apr 09 2009
parent reply "Nick Sabalausky" <a a.a> writes:
"Denis Koroskin" <2korden gmail.com> wrote in message 
news:op.ur4kb8ffo7cclz soldat.creatstudio.intranet...
 Now I am convinced:

 extern(C) int rand();

 void main()
 {
    int x = void;
    if (rand() == 0) {
        x += 5; // line 7
    }
 }

 test.d(7): Error: variable x used before set

 It only happens with DMD1.042+

 Yay!

I guess I'm going to have to start using -O in my debug builds...unless this behavior becomes independant of the -O switch...wink wink nudge nudge...
Apr 09 2009
parent Robert Fraser <fraserofthenight gmail.com> writes:
Denis Koroskin wrote:
 BTW, that's a breaking change! XD

Ssh!
Apr 10 2009
prev sibling next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Thu, Apr 9, 2009 at 10:28 AM, Denis Koroskin <2korden gmail.com> wrote:

 I believe this is just an optimization and not a general-purpose flow
 analysis:

It happens even with a non-trivial condition.
Apr 09 2009
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 09 Apr 2009 18:53:07 +0400, Jarrett Billingsley
<jarrett.billingsley gmail.com> wrote:

 On Thu, Apr 9, 2009 at 10:28 AM, Denis Koroskin <2korden gmail.com>  
 wrote:

 I believe this is just an optimization and not a general-purpose flow
 analysis:

It happens even with a non-trivial condition.

Now I am convinced: extern(C) int rand(); void main() { int x = void; if (rand() == 0) { x += 5; // line 7 } } test.d(7): Error: variable x used before set It only happens with DMD1.042+ Yay!
Apr 09 2009
prev sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 09 Apr 2009 20:36:43 +0400, Nick Sabalausky <a a.a> wrote:

 "Denis Koroskin" <2korden gmail.com> wrote in message
 news:op.ur4kb8ffo7cclz soldat.creatstudio.intranet...
 Now I am convinced:

 extern(C) int rand();

 void main()
 {
    int x = void;
    if (rand() == 0) {
        x += 5; // line 7
    }
 }

 test.d(7): Error: variable x used before set

 It only happens with DMD1.042+

 Yay!

I guess I'm going to have to start using -O in my debug builds...unless this behavior becomes independant of the -O switch...wink wink nudge nudge...

Yeah, me too! BTW, that's a breaking change! XD
Apr 09 2009