Recent

Author Topic: Unexpected Enumeration Type low and high values  (Read 394 times)

440bx

  • Hero Member
  • *****
  • Posts: 6065
Unexpected Enumeration Type low and high values
« on: October 19, 2025, 12:36:59 am »
Hello,

Consider the following code:
Code: Pascal  [Select][+][-]
  1. program _EnumRange;
  2.  
  3. type
  4.   WideEnum =
  5.   (
  6.    LoWideEnum = low(int64),
  7.    HiWideEnum = high(int64)
  8.   );
  9.  
  10. begin
  11.   writeln(' low(int64): ',  low(int64));
  12.   writeln('high(int64): ', high(int64));
  13.  
  14.   writeln;
  15.  
  16.   writeln(' low(WideEnum): ',  ord(low(WideEnum)));
  17.   writeln('high(WideEnum): ', ord(high(WideEnum)));
  18.  
  19.   readln;
  20. end.                              
  21.  
the low and high of int64 are:
Quote
low(int64): -9223372036854775808
high(int64): 9223372036854775807


while the low and high of the enumeration type are:
Quote
low(WideEnum): -1
high(WideEnum): 0

Considering that enumerations use a maximum of 4 bytes, it is not a surprise that the range is not what has been declared in the enumeration itself.

What is a bit of a surprise is that the compiler emits no warning, no hint, no nothing about the loss of significant digits in the type.

Also, the resulting range due to bit truncation, while correct, is not exactly intuitive, reinforcing the fact that some sort of warning from the compiler would be very useful in that situation.

Personally, I think the compiler should emit some message informing the programmer that it cannot honor such a type definition.

Comments welcome.


FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12080
  • Debugger - SynEdit - and more
    • wiki
Re: Unexpected Enumeration Type low and high values
« Reply #1 on: October 19, 2025, 12:51:26 am »
Seems that this is fixed in 3.3.1.

I get
Code: Text  [Select][+][-]
  1. project1.lpr(7,27) Error: Enumeration symbols can only have values in the range of -2^31 to 2^31-1
  2. project1.lpr(9,3) Error: Enumeration symbols can only have values in the range of -2^31 to 2^31-1

440bx

  • Hero Member
  • *****
  • Posts: 6065
Re: Unexpected Enumeration Type low and high values
« Reply #2 on: October 19, 2025, 12:53:20 am »
Seems that this is fixed in 3.3.1.
That's great.

By any chance, do you happen to know if that fix is also included in the upcoming v3.2.4 ?
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12080
  • Debugger - SynEdit - and more
    • wiki
Re: Unexpected Enumeration Type low and high values
« Reply #3 on: October 19, 2025, 01:29:10 am »
By any chance, do you happen to know if that fix is also included in the upcoming v3.2.4 ?

I do not know. But my recent 3.2.3 does not seem to have it. So likely the answer is no. (not sure if or what may still be merged after the RC)

440bx

  • Hero Member
  • *****
  • Posts: 6065
Re: Unexpected Enumeration Type low and high values
« Reply #4 on: October 19, 2025, 01:32:49 am »
Thank you Martin.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018