Integer promotion issue
Adam D. Ruppe
destructionator at gmail.com
Sun Jan 26 14:14:20 UTC 2020
On Sunday, 26 January 2020 at 13:01:58 UTC, Guillaume Piolat
wrote:
> It seems silly however in the wild this C int promotion rule
> make stuff work without the author knowledge.
indeed.
Though if that rule wasn't there we'd probably have more author
knowledge anyway. But it is too late now.
For D though maybe we could change our truncating rule to make it
more relaxed if all the inputs are literals or the same type as
the assignment. So it still promotes, just allows implicit cast
back to the original.
short a;
short b = a + 1; // implicitly does cast(short)(cast(int) a + 1)
int a;
short b;
short c = b + a; // error: cannot implicitly cast because the
`int a` triggers more strictness
ubyte a;
ubyte b;
ubyte c = a + b; // allowed
ushort d;
ubyte c = a + d; // prohibited
So it will follow the C promotion rules and then implicitly cast
back to the largest integral type in the original expression.
ubyte a = 255 + 5; // literals are excluded from the new implicit
rule loosening, so this does what it does now = int(int(255) +
int(5)) => VRP -> too large for ubyte, error.
Cuz I just think if we are explicitly specifying a particular
smaller type throughout it indicates a pretty clear intent
already.
More information about the Digitalmars-d
mailing list