Package: fp-compiler
Version: 2.4.0-1
Tags: patch

Hi,

the succ() and pred() calls are buggy in fpc 2.4.0.
It was discussed at the fpc-devel mailing list [1] and was fixed
in fpc svn trunk revision 14537 [2].

Attached a patch pred_succ.diff and a small testprogram pred_succ.pas.

The testprogram should print '256' but prints '0' without patch.

thx
Stefan

[1] http://www.mail-archive.com/fpc-de...@lists.freepascal.org/msg16041.html
[2] http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=rev&revision=14537

-- 
Stefan Kisdaroczi
Bergauer AG
Studbachstrasse 12
CH-8340 Hinwil
mail: stefan.kisdaro...@bergauer.ch
skype: stefan.kisdaroczi
tel:  +41 44 938 39 74
fax:  +41 44 938 39 75
diff -uNrp fpc-2.4.0.orig/fpcsrc/compiler/ninl.pas 
fpc-2.4.0/fpcsrc/compiler/ninl.pas
--- fpc-2.4.0.orig/fpcsrc/compiler/ninl.pas     2009-09-28 17:33:41.000000000 
+0200
+++ fpc-2.4.0/fpcsrc/compiler/ninl.pas  2010-02-09 11:06:17.000000000 +0100
@@ -1410,7 +1410,6 @@ implementation
         hp        : tnode;
         vl,vl2    : TConstExprInt;
         vr        : bestreal;
-        checkrange: boolean;
 
       begin { simplify }
          result:=nil;
@@ -1632,16 +1631,20 @@ implementation
               in_pred_x,
               in_succ_x:
                 begin
-                  { only perform range checking if the result is an enum }
-                  checkrange:=(resultdef.typ=enumdef);
-
                   if (left.nodetype=ordconstn) then
-                   begin
-                     if (inlinenumber=in_succ_x) then
-                       
result:=cordconstnode.create(tordconstnode(left).value+1,left.resultdef,checkrange)
-                     else
-                       
result:=cordconstnode.create(tordconstnode(left).value-1,left.resultdef,checkrange);
-                   end;
+                    begin
+                      if (inlinenumber=in_succ_x) then
+                        vl:=tordconstnode(left).value+1
+                      else
+                        vl:=tordconstnode(left).value-1;
+                      if is_integer(left.resultdef) then
+                      { the type of the original integer constant is 
irrelevant,
+                        it should be automatically adapted to the new value }
+                        result:=genintconstnode(vl)
+                      else
+                        { check the range for enums, chars, booleans }
+                        result:=cordconstnode.create(vl,left.resultdef,true)
+                    end
                 end;
               in_low_x,
               in_high_x:
program pred_succ;
begin
writeln(succ(255));
end.

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to