www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bug or Feature: unsigned integer overflow

reply Tobias Pankrath <tobias pankrath.net> writes:
void main()
{
     auto x = 9223372036854775808; // long.max + 1
}

 onlineapp.d(3): Error: signed integer overflow
According to spec x should be of type ulong and this should compile? It indeed compiles if I add the uL postfix. Is this a bug or indented behaviour?
Dec 13 2019
parent reply berni44 <dlang d-ecke.de> writes:
On Saturday, 14 December 2019 at 07:09:30 UTC, Tobias Pankrath 
wrote:
 void main()
 {
     auto x = 9223372036854775808; // long.max + 1
 }
You need to tell, that this is an unsigned long literal, else the compiler treats it as an int: void main() { auto x = 9223372036854775808UL; // long.max + 1 }
Dec 13 2019
parent reply Tobias Pankrath <tobias pankrath.net> writes:
On Saturday, 14 December 2019 at 07:44:37 UTC, berni44 wrote:
 On Saturday, 14 December 2019 at 07:09:30 UTC, Tobias Pankrath 
 wrote:
 void main()
 {
     auto x = 9223372036854775808; // long.max + 1
 }
You need to tell, that this is an unsigned long literal, else the compiler treats it as an int: void main() { auto x = 9223372036854775808UL; // long.max + 1 }
As far as I understand the spec, the type is inferred from the value range:
 Literal Type
 Usual decimal notation
0 .. 2_147_483_647 int
 2_147_483_648 .. 9_223_372_036_854_775_807 long
 9_223_372_036_854_775_808 .. 18_446_744_073_709_551_615 ulong
See: https://dlang.org/spec/lex.html#integerliteral What I am aiming at: Is the spec wrong or am I misunderstanding it and did this change recently?
Dec 14 2019
parent reply berni44 <dlang d-ecke.de> writes:
On Saturday, 14 December 2019 at 09:33:13 UTC, Tobias Pankrath 
wrote:
 See: https://dlang.org/spec/lex.html#integerliteral

 What I am aiming at: Is the spec wrong or am I misunderstanding 
 it and did this change recently?
You are right. The implementation does not do what the specs tell here. I filed a bug report: https://issues.dlang.org/show_bug.cgi?id=20449
Dec 14 2019
parent Tobias Pankrath <tobias pankrath.net> writes:
On Saturday, 14 December 2019 at 10:32:10 UTC, berni44 wrote:
 On Saturday, 14 December 2019 at 09:33:13 UTC, Tobias Pankrath 
 wrote:
 See: https://dlang.org/spec/lex.html#integerliteral

 What I am aiming at: Is the spec wrong or am I 
 misunderstanding it and did this change recently?
You are right. The implementation does not do what the specs tell here. I filed a bug report: https://issues.dlang.org/show_bug.cgi?id=20449
Thank you!
Dec 14 2019