What does a cast really do?

Paul Backus snarwin at gmail.com
Fri Feb 21 13:07:38 UTC 2025


On Friday, 21 February 2025 at 07:44:54 UTC, Jonathan M Davis 
wrote:
> I think that what it basically comes down to is that because -1 
> fits in int, and int implicitly converts to uint, VRP is fine 
> with converting the long with a value of -1 to uint. So, as 
> long as the value fits in 32 bits, the conversion will work 
> even if gets screwed up by the conversion between signed and 
> unsigned.

This has nothing to do with the value -1. You get the same result 
even if the value of x is completely unknown:

     void foo(uint) {}

     void example(int x)
     {
         foo(x); // compiles
         foo(long(x)); // compiles
         foo(cast(long) x); // compiles
         foo((() => cast(long) x)()); // Error: foo is not 
callable [...]
     }

My best guess is that when VRP looks at `long(x)` or `cast(long) 
x`, it can see that x is an int, so it allows the value to be 
converted *back* to int (and then from int to uint). But that 
information is lost when you hide the cast expression inside a 
function call.


More information about the Digitalmars-d-learn mailing list