D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11493 - dlang.org/type.html incorrectly says that you can't cast from -1 to unsigned types
Summary: dlang.org/type.html incorrectly says that you can't cast from -1 to unsigned ...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, pull, spec
Depends on:
Blocks:
 
Reported: 2013-11-10 13:22 UTC by Jonathan M Davis
Modified: 2023-02-02 12:01 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jonathan M Davis 2013-11-10 13:22:54 UTC
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 Dlang Bot 2023-02-02 11:42:11 UTC
@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 <matei.hriscu@proton.me>

https://github.com/dlang/dlang.org/pull/3517
Comment 2 Dlang Bot 2023-02-02 12:01:02 UTC
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 <matei.hriscu@proton.me>

https://github.com/dlang/dlang.org/pull/3517