Thread overview
[Issue 7834] New: Assign x%int to int without cast?
[Issue 7834] Assign x%int to int without cast
Apr 21, 2012
SomeDude
Apr 22, 2012
SomeDude
Apr 23, 2012
Don
April 06, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7834

           Summary: Assign x%int to int without cast?
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2012-04-05 17:35:56 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 11, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7834


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Assign x%int to int without |Assign x%int to int without
                   |cast?                       |cast
           Severity|enhancement                 |normal


--- Comment #1 from bearophile_hugs@eml.cc 2012-04-11 16:02:44 PDT ---
Now I think this is supposed to work, so it's not an enhancement request.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7834


SomeDude <lovelydear@mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear@mailmetrash.com


--- Comment #2 from SomeDude <lovelydear@mailmetrash.com> 2012-04-21 10:25:02 PDT ---
(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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7834



--- Comment #3 from bearophile_hugs@eml.cc 2012-04-22 03:35:15 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7834



--- Comment #4 from SomeDude <lovelydear@mailmetrash.com> 2012-04-22 10:43:27 PDT ---
That doesn't make any sense. You can't promote a 64 bit unsigned type into a 32 bit signed type.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7834



--- Comment #5 from bearophile_hugs@eml.cc 2012-04-22 11:57:20 PDT ---
(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).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7834


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |clugdbug@yahoo.com.au
         Resolution|                            |INVALID


--- Comment #6 from Don <clugdbug@yahoo.com.au> 2012-04-23 03:23:44 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7834



--- Comment #7 from bearophile_hugs@eml.cc 2012-04-24 19:39:48 PDT ---
(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;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------