LLVM talks 1: Clang for Chromium

Walter Bright newshound2 at digitalmars.com
Fri Dec 16 09:50:28 PST 2011


On 12/16/2011 4:23 AM, bearophile wrote:
> This code doesn't compile with DMD:
> Error: assignment cannot be used as a condition, perhaps == was meant?
>
> void main() {
>      int x, y;
>      if (x = y) {}
> }
>
>
> But this gives no errors:
>
> void main() {
>      int x, y;
>      if (x |= y) {}
>      if (x += y) {}
> }
>
>
> Do you know why DMD forbids assignments as conditions, but it accepts compound assignments there? It looks like a incongruence that's better to remove.

Because using = where == was meant is commonplace, but I've never seen the 
others in the wild.



> a.cc:2:16: warning: operator '?:' has lower precedence than '+'; '+' will be evaluated first
>    return x + b ? y : 0;
>           ~~~~~ ^
> a.cc:2:16: note: place parentheses around the '?:' expression to evaluate it first
>    return x + b ? y : 0;
>                 ^
>              (         )
>
> 1 warning generated.
>
> They say:
>
>> It's a bug every time!
>
>
> Given the frequence of bugs caused by the ?: operator, I think something like this will be good to have in D too.

I haven't seen the "bug every time" with this.



> a.cc:8:23: warning: argument to ’sizeof’ in ’memset’ call is the same expression as the destination;
>    did you mean to dereference it?
>    memset(s, 0, sizeof(s));
>           ~            ^
>
> 1 warning generated.

Fixing bugs in C code is not in D's charter, which is why it also does not scan 
printf strings, etc.


> At 14.45-16.39 there is an interesting part, about slide 22 of the PDF. It's about crashes/bugs caused by undefined order of evaluation of function arguments. This is a class of bugs that don't have to happen in D2 code.

D2 intends to define the order of evaluation of function arguments as strictly 
left-to-right. There are some problems implementing this, but that's where we 
want to go with it.


More information about the Digitalmars-d mailing list