www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - TDPL error or compiler bug?

reply Timon Gehr <timon.gehr gmx.ch> writes:
static assert(0xF234_5678_9ABC_5A5AUL == 17452669531959614042UL);

auto a = 0xF234_5678_9ABC_5A5AL; // ok, type is ulong
auto b = 17452669531959614042L; // error, signed integer overflow

But p33. in TDPL states:
"To write a hexadecimal integral literal, use the prefix 0x or 0X 
followed by a sequence of the letters 0-9, a-f, A-F, or _. [...] All of 
these literals can be suffixed similarly to the decimal constants, and 
the rules governing their types are identical to those for decimal 
literals."

Is this a compiler bug or an error in TDPL?
Sep 04 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, September 04, 2011 22:59:21 Timon Gehr wrote:
 static assert(0xF234_5678_9ABC_5A5AUL == 17452669531959614042UL);
 
 auto a = 0xF234_5678_9ABC_5A5AL; // ok, type is ulong
 auto b = 17452669531959614042L; // error, signed integer overflow
 
 But p33. in TDPL states:
 "To write a hexadecimal integral literal, use the prefix 0x or 0X
 followed by a sequence of the letters 0-9, a-f, A-F, or _. [...] All of
 these literals can be suffixed similarly to the decimal constants, and
 the rules governing their types are identical to those for decimal
 literals."
 
 Is this a compiler bug or an error in TDPL?

It sounds like the compiler assumes that a hexadecimal literal is unsigned and that a decimal literal is signed - which is what I would have expected it to do. TDPL should probably be changed. But maybe Walter or Andrei has a different opinion on that. - Jonathan M Davis
Sep 04 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/04/2011 11:05 PM, Jonathan M Davis wrote:
 On Sunday, September 04, 2011 22:59:21 Timon Gehr wrote:
 static assert(0xF234_5678_9ABC_5A5AUL == 17452669531959614042UL);

 auto a = 0xF234_5678_9ABC_5A5AL; // ok, type is ulong
 auto b = 17452669531959614042L; // error, signed integer overflow

 But p33. in TDPL states:
 "To write a hexadecimal integral literal, use the prefix 0x or 0X
 followed by a sequence of the letters 0-9, a-f, A-F, or _. [...] All of
 these literals can be suffixed similarly to the decimal constants, and
 the rules governing their types are identical to those for decimal
 literals."

 Is this a compiler bug or an error in TDPL?

It sounds like the compiler assumes that a hexadecimal literal is unsigned and that a decimal literal is signed - which is what I would have expected it to do. TDPL should probably be changed. But maybe Walter or Andrei has a different opinion on that. - Jonathan M Davis

Note that static assert(is(typeof(0x1234_5678_9ABC_5A5AL)==long)); The signedness of hex literals depends on whether or not the value fits in the signed type.
Sep 04 2011