Bug 17633 – Unary negation has the wrong type

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-07-10T21:27:42Z
Last change time
2023-10-16T18:54:58Z
Assigned to
No Owner
Creator
Eyal
See also
https://issues.dlang.org/show_bug.cgi?id=17637

Comments

Comment #0 by eyal — 2017-07-10T21:27:42Z
Unary negating a ubyte has type ubyte. This is a bug because it contradicts the behavior of the same unary operator syntax in C: enum ubyte x = 1; void assert_minus_1(int y) { assert(y == -1); } static assert(is(typeof(-x) == int), "Violating C's type rules"); unittest { assert_minus_1(-x); // -x is 255, it's zero-extended instead of sign-extended! }
Comment #1 by eyal — 2017-07-10T21:39:05Z
If C's rules are not desirable here. That's fine. assert_minus_1 should STILL not fail. instead of -ubyte becoming int as in C, -ubyte can at least become byte.