Recent

Author Topic: Bug In FreePascal Record Assignment???  (Read 1864 times)

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Bug In FreePascal Record Assignment???
« on: January 09, 2018, 11:11:49 pm »
I noticed something weird while writing some code this afternoon.

Seems FreePascal discards reference values (that is strings, arrays, classes etc) assigned to a record in a static class constructor.

Take a look at the sample below.

Code: Pascal  [Select][+][-]
  1. program BugFPC;
  2.  
  3. {$IFDEF FPC}
  4. {$MODE DELPHI}
  5. {$ENDIF FPC}
  6.  
  7. uses
  8.   SysUtils;
  9.  
  10. type
  11.   TBug = record
  12.  
  13.   strict private
  14.  
  15.     class constructor Bugger();
  16.  
  17.   public
  18.   var
  19.     a: Int32;
  20.     st: string;
  21.     arr: TBytes;
  22.  
  23.     class var
  24.  
  25.       skim: TBug;
  26.  
  27.     constructor Create(b: Int32);
  28.  
  29.   end;
  30.  
  31.   { TBug }
  32.  
  33. class constructor TBug.Bugger;
  34. begin
  35.   skim := Default (TBug);
  36.   skim.a := 5;
  37.   skim.st := 'fish';
  38.   skim.arr := TBytes.Create(1, 2, 3);
  39.  
  40.   Writeln(skim.a); // should be 5
  41.   Writeln(skim.st);
  42.   Writeln(Length(skim.arr)); // should be 3
  43. end;
  44.  
  45. constructor TBug.Create(b: Int32);
  46. var
  47.   temp: TBug;
  48. begin
  49.   temp := skim;
  50.   Writeln(temp.a); // should be 5
  51.   Writeln(temp.st);
  52.   Writeln(Length(skim.arr)); // should be 3 but got 0
  53. end;
  54.  
  55. begin
  56.   try
  57.     TBug.Create(9);
  58.     ReadLn;
  59.   except
  60.     on E: Exception do
  61.       Writeln(E.ClassName, ': ', E.Message);
  62.   end;
  63.  
  64. end.

if I run this code with Delphi, it prints out

5
fish
3
5
fish
3

which is the correct answer but FPC prints out

5
fish
3
5

0

please, is there any way to circumvent this issue and get the standard Delphi Behaviour which is obviously correct?

can anyone with the latest stable and trunk versions please test.

I did test with Lazarus 1.9.0 r55891 FPC 3.1.1 i386-win32-win32/win64 Trunk since this is the only version I have at the moment.

Regards.
« Last Edit: January 09, 2018, 11:17:51 pm by Xor-el »

Nitorami

  • Sr. Member
  • ****
  • Posts: 491
Re: Bug In FreePascal Record Assignment???
« Reply #1 on: January 09, 2018, 11:41:12 pm »
Tried your exact code with the textmode IDE FPC 3.0.2, win32, and it prints the correct text.

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Bug In FreePascal Record Assignment???
« Reply #2 on: January 09, 2018, 11:47:18 pm »
Tried your exact code with the textmode IDE FPC 3.0.2, win32, and it prints the correct text.
Ok, thanks for the testing.
It seems the bug is from trunk version..

 

TinyPortal © 2005-2018