www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 550] New: Shifting by more bits than size of quantity is allowed

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

           Summary: Shifting by more bits than size of quantity is allowed
           Product: D
           Version: 0.174
          Platform: PC
               URL: http://www.digitalmars.com/d/expression.html
        OS/Version: Windows
            Status: NEW
          Keywords: spec
          Severity: normal
          Priority: P4
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: deewiant gmail.com
OtherBugsDependingO 511
             nThis:


The compiler allows the following code, directly from the spec:

int c;
c << 33;        // error

Even though "[i]t's illegal to shift by more bits than the size of the quantity
being shifted". I'm not sure if this is a useful restriction, but either the
spec or DMD is in error.


-- 
Nov 18 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=550


smjg iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com
           Keywords|                            |accepts-invalid





If the right operand is a variable, it probably makes more sense from an
efficiency POV to allow it.  If it's a compile-time constant, the compiler
could easily catch the error.  However, I'm beginning to think such a
restriction may interfere with generic programming.


-- 
Nov 18 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=550


deewiant gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P4                          |P3





Only partially fixed in DMD 0.176. Stepping up the priority since compiler
behaviour is now inconsistent.

void main() {
        int c;
        c = c << 33; // Error: shift left by 33 exceeds 32
}

void main() {
        int c;
        c = c >> 33; // Works, shouldn't
}

void main() {
        int c;
        c <<= 33; // Works, shouldn't
}

void main() {
        int c;
        c >>= 33; // Works, shouldn't
}

void main() {
        int c;
        c = c >>> 33; // Works, shouldn't
}

void main() {
        int c;
        c >>>= 33; // Works, shouldn't
}

Also, this error message seems a bit strange:

void main() {
        int c;
        c = c << -1; // Error: shift left by -1 exceeds 32
}


-- 
Dec 03 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=550






Though the spec doesn't say it, it might as well be worth also disallowing
shifts equal to the size of the quantity, as they just zero the value.

Or, at least, they should: some appear to be no-ops currently. As pointed out
by Thomas Kühne in digitalmars.D.learn (
http://lists.puremagic.com/pipermail/digitalmars-d-learn/2007-May/005040.html
), compile-time shifts should be done by ((shift >= x.sizeof * 8) ? 0 : shift)
instead of the current (shift % (x.sizeof * 8)).


-- 
May 12 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=550


matti.niemenmaa+dbugzilla iki.fi changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic





-------
DMD 1.022 fixes the issues, but the error messages for the shift expressions
(e.g. c = c << 33) don't have file and line number info. The assignments (c <<=
33) do, though.


-- 
Oct 05 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=550


braddr puremagic.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED





Per Walter's request, moving the new problem (missing line numbers) into a new
bug.  This one has been addressed.  A little counter intuitively, the source of
the two issues is probably not actually related to the same part of the code.


-- 
Oct 20 2007