www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7834] New: Assign x%int to int without cast?

reply d-bugmail puremagic.com writes:
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



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: -------
Apr 05 2012
next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Apr 11 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7834


SomeDude <lovelydear mailmetrash.com> changed:

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



PDT ---

 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: -------
Apr 21 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7834






 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: -------
Apr 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7834




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: -------
Apr 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7834





 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: -------
Apr 22 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
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




 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: -------
Apr 23 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7834






 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: -------
Apr 24 2012