Recent

Author Topic: constant declarations not working as they do in Delphi  (Read 836 times)

440bx

  • Hero Member
  • *****
  • Posts: 948
constant declarations not working as they do in Delphi
« on: February 10, 2019, 02:20:18 am »
Hello,

FPC will not compile the following program:
Code: Pascal  [Select]
  1. {$APPTYPE CONSOLE}
  2.  
  3. program ArrayConstError;
  4.  
  5. procedure DoIt;
  6. const
  7.   LINE_CNT   = 2;
  8.  
  9.   DataBuf    : packed array[1..LINE_CNT] of packed array[0..1023] of char
  10.     = (#0, #0);
  11.  
  12.   // FPC does NOT accept this construction.  Delphi 2.0 does.
  13.  
  14.   DataArray  : packed array[1..LINE_CNT] of pchar
  15.     = (DataBuf[1], DataBuf[2]);
  16.  
  17. begin
  18.   writeln(DataBuf[1]);
  19.   writeln(DataBuf[2]);
  20.  
  21.   writeln(DataArray[1]);
  22.   writeln(DataArray[2]);
  23. end;
  24.  
  25. begin
  26.   DoIt;
  27. end.
  28.  

When attempting to compile the program above, FPC returns the following errors:
Code: Pascal  [Select]
  1. Compile Project, Mode: Default, Target: lib\i386-win32\ArrayConstError.exe: Exit code 1, Errors: 2
  2. ArrayConstError.lpr(13,18) Error: Incompatible types: got "Array[0..1023] Of Char" expected "PChar"
  3. ArrayConstError.lpr(13,30) Error: Incompatible types: got "Array[0..1023] Of Char" expected "PChar"
  4.  

Delphi version 2.0 compiles it without complaint.    I do not know if it compiles with other versions of Delphi.  Assistance to determine if it does or not is appreciated.

Should this be considered a "Delphi compatibility mode" bug ?

using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: constant declarations not working as they do in Delphi
« Reply #1 on: February 10, 2019, 02:25:57 am »
I don't see mode Delphi around the top, isn't it needed?

440bx

  • Hero Member
  • *****
  • Posts: 948
Re: constant declarations not working as they do in Delphi
« Reply #2 on: February 10, 2019, 02:40:22 am »
I don't see mode Delphi around the top, isn't it needed?
I tried it.  It didn't make any difference.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Cyrax

  • Hero Member
  • *****
  • Posts: 737
Re: constant declarations not working as they do in Delphi
« Reply #3 on: February 10, 2019, 03:06:09 am »
Changing your code to this makes it to compile successfully. FPC 3.3.1 trunk r41244 (i386-linux, i386-win32).

Code: Pascal  [Select]
  1. ...
  2. DataArray  : packed array[1..LINE_CNT] of pchar
  3.     = (@DataBuf[1], @DataBuf[2]);
  4. ...
  5.  

440bx

  • Hero Member
  • *****
  • Posts: 948
Re: constant declarations not working as they do in Delphi
« Reply #4 on: February 10, 2019, 03:11:23 am »
Changing your code to this makes it to compile successfully. FPC 3.3.1 trunk r41244 (i386-linux, i386-win32).

Code: Pascal  [Select]
  1. ...
  2. DataArray  : packed array[1..LINE_CNT] of pchar
  3.     = (@DataBuf[1], @DataBuf[2]);
  4. ...
  5.  
You're right, it does but, I don't think it should be necessary.  Even when specifying {$MODE DELPHI} it doesn't compile.  I believe it should.

Strictly from a compiler's definition viewpoint, FPC - as Delphi does - considers zero based arrays of char to be assignment compatible with pchar, because of that, the use of the address of operator should not be necessary.

ETA: I am using FPC v3.0.4 under Windows 7 64bit.

@Cyrax: thank you, I do appreciate your offering a solution. :)
« Last Edit: February 10, 2019, 03:15:02 am by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 1300
Re: constant declarations not working as they do in Delphi
« Reply #5 on: February 10, 2019, 05:34:52 am »
Delphi version 2.0 compiles it without complaint.    I do not know if it compiles with other versions of Delphi.
Delphi 10.3 also comple and code work right.

Strictly from a compiler's definition viewpoint, FPC - as Delphi does - considers zero based arrays of char to be assignment compatible with pchar, because of that, the use of the address of operator should not be
I agree with you, it is somewhat wrong that need to specify the @. This will show garbage:
Code: Pascal  [Select]
  1. {$APPTYPE CONSOLE}
  2. program ArrayConstError;
  3.  
  4. procedure DoIt;
  5. const
  6.   LINE_CNT = 2;
  7.   DataBuf: packed array[1..LINE_CNT] {of packed array[0..1023] of Char} of PChar
  8.     = ('Some'#0, #0);
  9.   DataArray: packed array[1..LINE_CNT] of PChar = (@DataBuf[1], @DataBuf[2]);
  10. begin
  11.   Writeln(DataBuf[1]);
  12.   Writeln(DataArray[1]);
  13. end;
  14.  
  15. begin
  16.   DoIt;
  17.   Readln;
  18. end.

440bx

  • Hero Member
  • *****
  • Posts: 948
Re: constant declarations not working as they do in Delphi
« Reply #6 on: February 10, 2019, 11:30:10 am »
Delphi 10.3 also comple and code work right.
Thank you for checking the behavior in other versions of Delphi.   Given that the discrepancy is consistent, I will report it as a bug.

This will show garbage:
Code: Pascal  [Select]
  1. {$APPTYPE CONSOLE}
  2. program ArrayConstError;
  3.  
  4. procedure DoIt;
  5. const
  6.   LINE_CNT = 2;
  7.   DataBuf: packed array[1..LINE_CNT] {of packed array[0..1023] of Char} of PChar
  8.     = ('Some'#0, #0);
  9.   DataArray: packed array[1..LINE_CNT] of PChar = (@DataBuf[1], @DataBuf[2]);
  10. begin
  11.   Writeln(DataBuf[1]);
  12.   Writeln(DataArray[1]);
  13. end;
  14.  
  15. begin
  16.   DoIt;
  17.   Readln;
  18. end.
Unfortunately, in that case, FPC and Delphi behave the same way.  The use of @ is incorrect but, I reported that problem/bug a while back and, it doesn't look like that problem/bug will be corrected because it is consistent with Delphi.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.