D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4491 - Assigning large const value to ulong type results in "signed integer overflow"
Summary: Assigning large const value to ulong type results in "signed integer overflow"
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2010-07-21 13:50 UTC by Iain Buclaw
Modified: 2015-06-09 05:11 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 Iain Buclaw 2010-07-21 13:50:16 UTC
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 Don 2010-07-21 14:32:06 UTC
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 bearophile_hugs 2010-07-21 14:51:27 UTC
What's bad in the compiler/language accepting a line like:

ulong t = 18446744073709551615;
Comment 3 Iain Buclaw 2010-07-22 07:48:45 UTC
(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.