www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3147] New: Incorrect value range propagation for addition

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147

           Summary: Incorrect value range propagation for addition
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrei metalanguage.com


This code doesn't compile and it should:

void main()
{
    byte x, y, z;
    short a = x+y;
}

Although the manifest type of x+y is int, the actual range of x+y, regardless
of the values of x and y, is -byte.min-byte.min to byte.max+byte.max. That
range fits properly in a short so the compiler should let the code go through.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 06 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147


Rob Jacques <sandford jhu.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sandford jhu.edu




--- Comment #1 from Rob Jacques <sandford jhu.edu>  2009-07-07 08:07:25 PDT ---
Additionally, propagation doesn't work for
byte, ubyte, bool and char.
for operations + * - / % >>

Also
    byte x,y;
    short z;
    z = x<<1; // Error: cannot implicitly convert expression (cast(int)x << 1)
of type int to byte
    z = x << y; // compiles


    // Repeat for >>>

Also
    ubyte x,y=10;
    ubyte z;
    z = -y;  // compiles

Inconsistancies:
    byte x,y;
    short z;
    z += x; //compiles, z = z + x doesn't
    x += y; //compiles, x = x + y doesn't
    x++;    //compiles, x = x+1, doesn't
    ++x;    //compiles, x = x+1, doesn't
    x = x + 1; // Error: cannot implicitly convert expression (cast(int)x + 1)
of type int to byte

    // Again repeat for the other operations/type combos

also
   byte[] x, y, z;
   z[] = x[] * z[]; //compiles
   // repeat for other array operation and type combos

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 07 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147



--- Comment #2 from Sobirari Muhomori <dfj1esp02 sneakemail.com> 2010-11-16
13:40:52 PST ---
Some asserts for modulus and shifts
---
byte b;
short s;
int i;
long l;
static assert(is(typeof(s%b)==byte));
static assert(is(typeof(i%s)==short));
static assert(is(typeof(b<<b)==int));
static assert(is(typeof(b>>i)==byte));
static assert(is(typeof(b>>>b)==byte));
static assert(is(typeof(b>>l)==byte));
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147



--- Comment #3 from Andrei Alexandrescu <andrei metalanguage.com> 2010-11-16
13:54:16 PST ---
The point is not to ascribe the smallest static type to the result. That would
break compatibility with C in many ways. All that happens is the compiler
tracks the range of the expression statically while using the same typing rules
as C.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147



--- Comment #4 from Sobirari Muhomori <dfj1esp02 sneakemail.com> 2010-11-16
14:01:34 PST ---
---
l = cast(int)(s%b);
l = cast(short)(s%b);
---
What's the difference?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147



--- Comment #5 from Sobirari Muhomori <dfj1esp02 sneakemail.com> 2010-11-16
14:22:01 PST ---
In any case
---
static if(__traits(compiles, s=b+b))
{
    static assert(__traits(compiles, b=s%b));
    static assert(__traits(compiles, s=s>>b));
    static assert(__traits(compiles, s=s>>>b));
}
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147



--- Comment #6 from Sobirari Muhomori <dfj1esp02 sneakemail.com> 2010-11-16
22:03:08 PST ---
There should be ints in shift asserts
---
static if(__traits(compiles, s=b+b))
{
    static assert(__traits(compiles, b=i%b));
    static assert(__traits(compiles, b=b>>i));
    static assert(__traits(compiles, b=b>>>i));
}
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147


kennytm gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |kennytm gmail.com


--- Comment #7 from kennytm gmail.com 2011-06-12 11:05:38 PDT ---
DMD pull #116.

https://github.com/D-Programming-Language/dmd/pull/116

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 12 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147



--- Comment #8 from kennytm gmail.com 2011-06-12 11:06:25 PDT ---
*** Issue 6000 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 12 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147


kennytm gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dfj1esp02 sneakemail.com


--- Comment #9 from kennytm gmail.com 2011-06-12 11:07:25 PDT ---
*** Issue 5225 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 12 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #10 from yebblies <yebblies gmail.com> 2011-06-16 01:01:05 PDT ---
*** Issue 5528 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147



--- Comment #11 from bearophile_hugs eml.cc 2011-06-16 05:30:16 PDT ---
This example was in bug 5528:

void main() {
    uint i = 10;
    ubyte x1 = i % ubyte.max;
    ulong l = 10;
    uint x2 = l % uint.max;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 16 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3147


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


--- Comment #12 from Walter Bright <bugzilla digitalmars.com> 2011-06-27
14:45:08 PDT ---
https://github.com/D-Programming-Language/dmd/commit/dce514326cbe086f35d23c4c2b24cb2dc1da659d

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