Bug 17417 – Deprecate signed integer promotions in bitwise operations
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-05-22T12:15:00Z
Last change time
2017-05-24T14:22:38Z
Keywords
spec
Assigned to
nobody
Creator
dfj1esp02
Comments
Comment #0 by dfj1esp02 — 2017-05-22T12:15:29Z
Signed integers - byte and short - are promoted to int in bitwise operations which doesn't make much sense and is bug prone. Also true for promotion from int to long in binary bitwise operations.
Comment #1 by uplink.coder — 2017-05-22T13:35:03Z
There is a good reason to do the promotion.
The generated machine-code is more efficient in many cases.
Types smaller the uint should only be used for storage, not for operations :)
Comment #2 by dfj1esp02 — 2017-05-23T14:15:36Z
Promotion in bitwise operators makes better sense for unsigned integers, but doesn't make much sense for signed integers, bitwise operators are supposed to act on bit arrays, which signed integers are not. After deprecation the compiler should give an error for signed integer promotions in bitwise operators.
Comment #3 by uplink.coder — 2017-05-23T14:35:27Z
(In reply to anonymous4 from comment #2)
> Promotion in bitwise operators makes better sense for unsigned integers, but
> doesn't make much sense for signed integers, bitwise operators are supposed
> to act on bit arrays, which signed integers are not. After deprecation the
> compiler should give an error for signed integer promotions in bitwise
> operators.
There is precedent for for bitwise operations on singed integers as well, such as right/left shifts.
To save division cost
Comment #4 by dfj1esp02 — 2017-05-23T15:36:18Z
I think it's usually used with int, in which case there will be no promotion. Another thing I can think of is cast(ubyte)(short>>8) which would be a bit more difficult to check that sign extension doesn't affect the result if we would want to allow it.
Comment #5 by andrei — 2017-05-23T16:29:44Z
This is core behavior that we don't plan to change. Value Range Propagation is the way to avoid casts back and forth. Feel free to submit issues related to it, thanks.
Comment #6 by dfj1esp02 — 2017-05-24T11:42:24Z
The issue is not about VRP. Integer promotions don't require casts and happen without casts (basically inadvertently).
Example: if we have two 16-bit values and want to combine them into a 32-bit value:
uint v=(s1<<16)|s2;
VRP has nothing to do here.
Comment #7 by dfj1esp02 — 2017-05-24T11:53:30Z
VRP can't do anything here because promotion works according to spec.
Comment #8 by andrei — 2017-05-24T14:22:38Z
Whether or not VRP may help the situation, we won't change the operators. Please do not reopen, thanks.