D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3468 - dmd allows negative values to be assigned to an uint variable
Summary: dmd allows negative values to be assigned to an uint variable
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-11-02 04:16 UTC by Qian Xu
Modified: 2024-12-13 17:50 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Qian Xu 2009-11-02 04:16:35 UTC
Description: "uint i = -1" is allowed by dmd 1.050. 
Expectation: A warning or an error should be shown by compiling the code.

I misspelled int as uint. A boolean expression (if i < 0) always equals false. It took me long time to debug my code. Finally I have found out, that the value i is not -1.
Comment 1 Walter Bright 2009-11-02 16:08:08 UTC
I'll put this as an enhancement request because negate is currently an operation that is defined to work on unsigned values, hence no error.
Comment 2 Mathias LANG 2015-11-22 12:23:45 UTC
This 'bug' is still around in D2.
It looks like an heritage from C / C++, one of the bad kind.

It allows you to write gems such as:
```
uint func (int i) { return i; }

void main ()
{
  uint v = func(-42);
}
```

@Walter: Would you be okay with the idea of deprecating this ?
Related: https://issues.dlang.org/show_bug.cgi?id=259
Comment 3 Dmitry Olshansky 2018-05-16 09:05:21 UTC
That would make unsigned types way more sane to work with.

And those who like -1 to be shortcut for "all 1's" could use ~0.
Comment 4 dlangBugzillaToGithub 2024-12-13 17:50:50 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18126

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB