Bug 18495 – Integral promotion for a ~ operator

Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2018-02-22T15:06:23Z
Last change time
2018-02-22T15:46:24Z
Assigned to
No Owner
Creator
Kirill

Comments

Comment #0 by mrkirushko — 2018-02-22T15:06:23Z
As far as I understand, according to the manual (https://dlang.org/spec/expression.html#complement_expressions) the bitwise invertion operator should give a result of the same type as of its operand. But the code " ubyte d1 = 10; ubyte d2; d2 = ~d1; " gives a strange " Deprecation: integral promotion not done for '~d1', use '-transition=intpromote' switch or '~cast(int)d1' " message when compiled. None of the C compilers I ever used had any issues with that as well. They just compile with no warnings or errors. The code compiles and works as expected so it is not a major issue.
Comment #1 by mrkirushko — 2018-02-22T15:13:06Z
Of course if I do add "-transition=intpromote" as advised by the compiler, it just gives an error: "cannot implicitly convert expression `~cast(int)d1` of type `int` to `ubyte`. Fixing this requires to add some ugly looking code like " d2 = 0xFF & ~d1; " ore something even worse like " d2 = cast(ubyte) ~d1; ". This just can not be right.
Comment #2 by schveiguy — 2018-02-22T15:26:28Z
This is recent, and as designed. Please see: https://dlang.org/changelog/2.078.0.html#fix16997 See the original bug report here: https://issues.dlang.org/show_bug.cgi?id=16997 Note that in C: unsigned char x = 0x80; int y = ~x; // 0xffffff7f In D, without any switches: ubyte x = 0x80; int y = ~x; // 0x0000007f With the intpromote switch, it's the same as C. This is the point behind the change -- it was a bug that the integer promotion didn't happen before the complement. Yes, in C, you don't have to cast an int back down to unsigned char explicitly, it just truncates without complaint. D does not. This is the difference you are seeing that requires the cast.
Comment #3 by schveiguy — 2018-02-22T15:46:24Z
Although this bug is invalid, there legitimately is a problem with the spec: https://issues.dlang.org/show_bug.cgi?id=18496