digitalmars.D.bugs - [Issue 16997] New: Real-life evidence for integer promotion of unary
- via Digitalmars-d-bugs (47/47) Dec 20 2016 https://issues.dlang.org/show_bug.cgi?id=16997
https://issues.dlang.org/show_bug.cgi?id=16997 Issue ID: 16997 Summary: Real-life evidence for integer promotion of unary minus creating bugs when porting C code Product: D Version: D2 Hardware: All OS: All Status: NEW Severity: major Priority: P1 Component: dmd Assignee: nobody puremagic.com Reporter: aliloko gmail.com Consider the following C++ program: ----- #include <cstdio> int main() { signed char c = -128; printf("%d\n", (int)(-c)); return 0; } ----- This prints "128". Consider the supposedly identical D code: ----- import core.stdc.stdio; void main() { byte c = -128; printf("%d\n", cast(int)(-c)); // -128 } ----- Since the D unary minus doesn't promote byte to int, it yields a different result. This is a real case that costed ketmar 40 min, while porting working C code. Porting to D silently broke the code due to subtly different integer promotion rules. I'm pretty sure I would have given up becasue code that is translated is often enough not understood yet. After investigation: * Taking the unary minus of byte and short and then asting to int yield different results for the lowest values: -128 and -32768 * Taking the unary minus of ubyte and ushort and then casting to int yield different results for any value except 0 Why are the promotion rules for unary + and - different from C? --
Dec 20 2016