Bug 11493 – dlang.org/type.html incorrectly says that you can't cast from -1 to unsigned types
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-10T13:22:54Z
Last change time
2023-02-02T12:01:02Z
Keywords
accepts-invalid, pull, spec
Assigned to
No Owner
Creator
Jonathan M Davis
Comments
Comment #0 by issues.dlang — 2013-11-10T13:22:54Z
dlang.org/type.html gives this example
ubyte u1 = cast(byte)-1; // error, -1 cannot be represented in a ubyte
ushort u2 = cast(short)-1; // error, -1 cannot be represented in a ushort
uint u3 = cast(int)-1; // ok, -1 can be represented in a uint
ulong u4 = cast(long)-1; // ok, -1 can be represented in a ulong
However, all 4 compile, which makes sense, because casts are blunt and don't care about what does and doesn't fit. This won't compile
ubyte u1 = -1;
ushort u2 = -1;
which is a bit interesting given that this compiles just fine
byte i1 = -1;
short i2 = -1;
ubyte u1 = i1;
ushort u2 = i2;
But regardless of whether
ubyte u1 = -1;
ushort u2 = -1;
should compile, the example in documentation definitely should, and the documentation claims that it shouldn't, so the documentation is wrong and needs to be updated.
And honestly, I don't understand the logic of how -1 can be represented in a uint but not a ushort like the docs claim, since it can't be represented in either, since they're unsigned, so I don't understand how the example in the docs ever would have made sense.