Comment #0 by bearophile_hugs — 2012-04-05T17:35:56Z
In DMD 2.059beta this code compiles with no warnings or errors:
void main(string[] args) {
uint x;
ubyte y = cast(ubyte)args.length;
ubyte z = x % y;
}
So I'd like code similar to this too to compile with no need of a cast:
void main() {
ulong x;
int y;
int z = x % y;
}
Currently it gives:
test.d(4): Error: cannot implicitly convert expression (x % cast(ulong)y) of type ulong to int
Comment #1 by bearophile_hugs — 2012-04-11T16:02:44Z
Now I think this is supposed to work, so it's not an enhancement request.
Comment #2 by lovelydear — 2012-04-21T10:25:02Z
(In reply to comment #1)
> Now I think this is supposed to work, so it's not an enhancement request.
I don't see where in the spec ulong can be promoted to int.
http://dlang.org/type.html
Comment #3 by bearophile_hugs — 2012-04-22T03:35:15Z
(In reply to comment #2)
> (In reply to comment #1)
> > Now I think this is supposed to work, so it's not an enhancement request.
>
> I don't see where in the spec ulong can be promoted to int.
> http://dlang.org/type.html
Thank you, then I think the spec too need to be fixed.
Comment #4 by lovelydear — 2012-04-22T10:43:27Z
That doesn't make any sense. You can't promote a 64 bit unsigned type into a 32 bit signed type.
Comment #5 by bearophile_hugs — 2012-04-22T11:57:20Z
(In reply to comment #4)
> That doesn't make any sense. You can't promote a 64 bit unsigned type into a 32
> bit signed type.
Give me some time to think some more about it, I have a backlog of bugs to take a look at (thanks to your recent efforts).
Comment #6 by clugdbug — 2012-04-23T03:23:44Z
(In reply to comment #0)
> In DMD 2.059beta this code compiles with no warnings or errors:
> void main(string[] args) {
> uint x;
> ubyte y = cast(ubyte)args.length;
> ubyte z = x % y;
> }
> So I'd like code similar to this too to compile with no need of a cast:
> void main() {
> ulong x;
> int y;
> int z = x % y;
> }
These two bits of code aren't analogous.
If y was -1, when cast to ulong it becomes 0xFFFF_FFFF_FFFF_FFFF
Therefore, x % y could be 0.. ulong.max; there's no way that can fit into an int.
The compiler is correct.
OTOH if you change int -> uint, it works.
Comment #7 by bearophile_hugs — 2012-04-24T19:39:48Z
(In reply to comment #6)
> These two bits of code aren't analogous.
> If y was -1, when cast to ulong it becomes 0xFFFF_FFFF_FFFF_FFFF
> Therefore, x % y could be 0.. ulong.max; there's no way that can fit into an
> int.
> The compiler is correct.
You are of course right, thank you Don and sorry SomeDude.
> OTOH if you change int -> uint, it works.
Right, this compiles:
void main() {
ulong x;
uint y = 1;
int z = x % y;
}