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.
Comment #1 by dlang-bot — 2023-02-02T11:42:11Z
@matthriscu created dlang/dlang.org pull request #3517 "Fix issue 11493 - Correctly explain code snippet behaviour" fixing this issue: - Fix 11493 - correctly explain code snippet behaviour Signed-off-by: Matei Hriscu <[email protected]> https://github.com/dlang/dlang.org/pull/3517
Comment #2 by dlang-bot — 2023-02-02T12:01:02Z
dlang/dlang.org pull request #3517 "Fix issue 11493 - Correctly explain code snippet behaviour" was merged into master: - deb0d0a8328f79223906d13aed8964f43663281b by Matei Hriscu: Fix 11493 - correctly explain code snippet behaviour Signed-off-by: Matei Hriscu <[email protected]> https://github.com/dlang/dlang.org/pull/3517