Recent

Author Topic: Dynamic array as a typed constant  (Read 1931 times)

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Dynamic array as a typed constant
« Reply #15 on: November 06, 2022, 05:04:01 pm »
FPC decided years ago already to break compatiblity with Delphi here to improve type safety. When you have a non-writable constant then it makes sense for it to really be not writable.

I filed an issue on this https://gitlab.com/freepascal.org/fpc/source/-/issues/39986

ASerge

  • Hero Member
  • *****
  • Posts: 2246
Re: Dynamic array as a typed constant
« Reply #16 on: November 06, 2022, 05:12:12 pm »
When you have a non-writable constant then it makes sense for it to really be not writable.
Then the compiler behaves inconsistently:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$WRITEABLECONST OFF}
  3. const
  4.   A: array[0..0] of SizeInt = (1);
  5.   B: array of SizeInt = (2);
  6.   C: SizeInt = 3;
  7. begin
  8.   //A[0] := 5; // Error: Can't assign values to const variable
  9.   B[0] := 5;  // No error
  10.   //C := 5; // Error: Can't assign values to const variable
  11. end.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Dynamic array as a typed constant
« Reply #17 on: November 06, 2022, 05:59:56 pm »
Well, I did file the issue on this https://gitlab.com/freepascal.org/fpc/source/-/issues/39986, but it was immediately closed (not by @PascalDragon).

Quote from: Michael Van Canneyt
This is normal. You cannot change the array pointer i_arr, but you can change what it points to.

But you could report a bug for the compiler not rejecting the Inc at compile time. This is likely a remnant of the fact that typed dynamic array constants were only introduced in FPC 3.2.0.

Which is contrary to what you just said...


avk

  • Hero Member
  • *****
  • Posts: 752
Re: Dynamic array as a typed constant
« Reply #18 on: November 06, 2022, 06:05:59 pm »
Hehe, looks like MVC is sometimes waist-deep wooden?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Dynamic array as a typed constant
« Reply #19 on: November 07, 2022, 07:23:50 am »
FPC decided years ago already to break compatiblity with Delphi here to improve type safety. When you have a non-writable constant then it makes sense for it to really be not writable.

I filed an issue on this https://gitlab.com/freepascal.org/fpc/source/-/issues/39986

Thank you.

When you have a non-writable constant then it makes sense for it to really be not writable.
Then the compiler behaves inconsistently:

As I said: typed dynamic array constants are a fairly recent addition and the compiler has specific checks for various types so it can happen that it behaves inconsistently for them. Which is after all why I had asked Bogen85 to report this.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Dynamic array as a typed constant
« Reply #20 on: November 07, 2022, 08:34:46 am »
Thank you.

You are welcome.  :)

So, since constant statically sized array modifications are caught by the compiler:

Code: Pascal  [Select][+][-]
  1. program staticarr;
  2. {$mode objfpc}
  3. {$WriteableConst off}
  4.  
  5. type
  6.   ArrInt3 = array[0..2] of integer;
  7.  
  8. procedure procarr3(const arr: ArrInt3);
  9.   begin
  10.         arr[0] := 4;
  11.   end;
  12.  
  13. var
  14.   arr3: ArrInt3 = (1,2,3);
  15.  
  16. begin
  17.   procarr3(arr3);
  18. end.
  19.  

Code: Text  [Select][+][-]
  1. Free Pascal Compiler version 3.2.2 [2022/08/31] for x86_64
  2. Copyright (c) 1993-2021 by Florian Klaempfl and others
  3. Target OS: Linux for x86-64
  4. Compiling staticarr.pas
  5. staticarr.pas(10,4) Error: Can't assign values to const variable
  6. staticarr.pas(19) Fatal: There were 1 errors compiling module, stopping
  7. Fatal: Compilation aborted
  8. Error: /usr/bin/ppcx64 returned an error exitcode
  9.  

Code: Pascal  [Select][+][-]
  1. program staticarr2;
  2. {$mode objfpc}
  3. {$WriteableConst off}
  4.  
  5. type
  6.   ArrInt3 = array[0..2] of integer;
  7.  
  8. procedure procarr3(var arr: ArrInt3);
  9.   begin
  10.         arr[0] := 4;
  11.   end;
  12.  
  13. const
  14.   arr3: ArrInt3 = (1,2,3);
  15.  
  16. begin
  17.   procarr3(arr3);
  18. end.
  19.  

Code: Text  [Select][+][-]
  1. $ fpc staticarr2.pas
  2. Free Pascal Compiler version 3.2.2 [2022/08/31] for x86_64
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling staticarr2.pas
  6. staticarr2.pas(17,16) Error: Can't assign values to const variable
  7. staticarr2.pas(19) Fatal: There were 1 errors compiling module, stopping
  8. Fatal: Compilation aborted
  9. Error: /usr/bin/ppcx64 returned an error exitcode
  10.  


And, as I demonstrated earlier:

This does compile and run:
Code: Pascal  [Select][+][-]
  1. program dynarr2;
  2. {$mode objfpc}
  3. {$WriteableConst off}
  4.  
  5. type
  6.   ArrInt = array of LongInt;
  7.  
  8. procedure procarr(const arr: ArrInt);
  9.   begin
  10.     arr[0] := 4;
  11.   end;
  12.  
  13. begin
  14.   procarr([1,2,3])  
  15. end.
  16.  

But this fails:
Code: Pascal  [Select][+][-]
  1. program dynarr2;
  2. {$mode objfpc}
  3. {$WriteableConst off}
  4.  
  5. type
  6.   ArrInt = array of LongInt;
  7.  
  8. procedure procarr(const arr: ArrInt);
  9.   begin
  10.     arr[0] := 4;
  11.   end;
  12.  
  13. begin
  14.   procarr([1,2,3])  
  15. end.
  16.  

Code: Text  [Select][+][-]
  1. Free Pascal Compiler version 3.2.2 [2022/08/31] for x86_64
  2. Copyright (c) 1993-2021 by Florian Klaempfl and others
  3. Target OS: Linux for x86-64
  4. Compiling dynarr2.pas
  5. Linking dynarr2
  6. 15 lines compiled, 0.1 sec
  7.  

Should the above case (that does compile and run) be reported as well? I would indicate it is related to the FPC issue 39986

(as it is a const paramater which might be handled differently?)



 

TinyPortal © 2005-2018