[fpc-devel] Implicit Type Conversion when running with range and overflow checking

Louis Salkind lou at salkind.net
Wed Feb 25 00:47:26 CET 2015


Pardon the stupid question(s)...  I was surprised by the behavior of the 
following code when range checking and overflow checking was enabled on 
win32:

procedure test_rangeoverflow;
var
   a, b: BYTE;
   i: Integer;
begin
   a := 128;
   b := 129;
   i := a + b;   // no errors, i is 257; a and b have been promoted to 
integers
                 // but shouldn't there be a range check, as a+b has 
overflowed a byte and should not be promoted?
   i := BYTE(a + b);    // no errors, and i is 1.  Even if a+b is 
promoted to integer,
                        // shouldn't there be range check when coercing 
and integer of 257 to byte?
   a := a + b;    // gives range check when storing result, but not an 
overflow error first
end;

Comments?  I'm guessing this is either specified in the Pascal standard, 
or fpc is emulating Delphi behavior.  Nevertheless, the result seemed 
non-intuitive enough to me that I wanted to confirm this was the 
intended behavior.



More information about the fpc-devel mailing list