unnecessary casts

monarch_dodra monarchdodra at gmail.com
Thu Jan 31 03:39:03 PST 2013


On Thursday, 31 January 2013 at 00:03:43 UTC, Timon Gehr wrote:
> On 01/30/2013 11:49 PM, Namespace wrote:
>> Is the compiler (dmd) fit enough to detect and avoid 
>> unnecessary casts?
>> ...
>
> Well, 'unnecessary casts' are a no-op anyway. (Yes, afaik DMD 
> will even eliminate them from the AST.)

There is a bug though that the value returned by an opCast should 
be an r-value. However, because the cast is transformed into a 
no-op, then it becomes erroneously legal to take the address of 
the cast value, or to pass it by ref:

//----
void foo()(auto ref int i)
{++i;}

void main()
{
   double d = 0;
   int    i = 0;
   foo(cast(int)d); //OK:   Calls foo(int);
   foo(cast(int)i); //Oops! Calls foo(ref int);
   assert(d == 0); //OK
   assert(i == 0); //Darn...
}
//----

I *think* this is a known bug. I couldn't find the entry 
though... Can anybody confirm?


More information about the Digitalmars-d-learn mailing list