[Issue 18380] New: Integral Promotion Fix 16997 should ignore if ultimate result is cast to original or smaller type
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Feb 5 22:41:15 UTC 2018
https://issues.dlang.org/show_bug.cgi?id=18380
Issue ID: 18380
Summary: Integral Promotion Fix 16997 should ignore if ultimate
result is cast to original or smaller type
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: schveiguy at yahoo.com
Consider the following code:
byte b = byte.min;
int x = -b;
writeln(x);
Currently (without the -transition=intpromote switch), this prints -128.
With the switch, it prints 128.
This is the fix implemented by the new promotion rules, and it makes sense that
the negation of -128 should be an int of 128.
Thus, the compiler complains:
Deprecation: integral promotion not done for -b, use '-transition=intpromote'
switch or -cast(int)(b)
But, what about this code?
b = cast(byte)-b;
writeln(b);
This prints -128 with or without the intpromote switch. That is, there is no
difference depending on whether you are int-promoting first or not.
However, the compiler still emits the warning without the switch. If you do:
b = cast(byte)-cast(int)b;
It works with and without the switch, but this is busywork the user shouldn't
have to do. Obviously casting to int isn't going to make a difference.
This is really going to suck when this code becomes an error and not a
deprecation warning.
The compiler should delay printing the deprecation until it knows the
expression is not cast to the original type that was promoted or smaller.
This fix should go along with a change to the message that says:
"... or -cast(int)b or cast the result to the original type or smaller"
That's a mouthful, but maybe someone can come up with something better.
--
More information about the Digitalmars-d-bugs
mailing list