Bug 4491 – Assigning large const value to ulong type results in "signed integer overflow"

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2010-07-21T13:50:00Z
Last change time
2015-06-09T05:11:36Z
Keywords
diagnostic
Assigned to
nobody
Creator
ibuclaw

Comments

Comment #0 by ibuclaw — 2010-07-21T13:50:16Z
The following code: import std.stdio; void main() { ulong t;// = 18446744073709551615; printf("%llu\n", t.max); } Outputs: 18446744073709551615 However, I get a "signed integer overflow" when I try to assign the value directly. Code: import std.stdio; void main() { ulong t = 18446744073709551615; printf("%llu\n", t); } Outputs: bug.d(4): signed integer overflow Using 'cast(ulong)18446744073709551615' doesn't help either, so I presume this happens before D knows what datatype the large integer will be assigned to. IMO, assignments of constant values within the range of foo.min to foo.max should be allowed for all types.
Comment #1 by clugdbug — 2010-07-21T14:32:06Z
You need to add an 'L' suffix when it's larger than int.max, and a 'U' suffix when it's unsigned. import std.stdio; void main() { ulong t = 18446744073709551615UL; printf("%llu\n", t); } The error message should make this clearer. Marking as a 'diagnostic' bug.
Comment #2 by bearophile_hugs — 2010-07-21T14:51:27Z
What's bad in the compiler/language accepting a line like: ulong t = 18446744073709551615;
Comment #3 by ibuclaw — 2010-07-22T07:48:45Z
(In reply to comment #1) > You need to add an 'L' suffix when it's larger than int.max, and a 'U' suffix > when it's unsigned. > > import std.stdio; > void main() > { > ulong t = 18446744073709551615UL; > printf("%llu\n", t); > } > > The error message should make this clearer. Marking as a 'diagnostic' bug. I suppose that makes sense once you know. Though it seems that it should more like syntactical sugar to me, rather than a mandatory marking. For example, "1e6" gets translated to 1_000_000.