Recent

Author Topic: Open Array vs Dyn Array as argument type in functions, changes in FPC 3.3  (Read 882 times)

prof7bit

  • Full Member
  • ***
  • Posts: 161
Consider this:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.   TBytes = array of Byte;
  5.  
  6. procedure Foo(A: array of Byte);
  7. begin
  8. end;
  9.  
  10. procedure Bar(A: TBytes);
  11. begin
  12. end;
  13.  
  14. var
  15.   X: array[0..15] of Byte;
  16.  
  17. begin
  18.   Foo(X);
  19.   Bar(X); // error in FPC 3.3.1, compiles fine in 3.2.1
  20. end.
  21.  

The above code has been compiling and working fine in FPC 3.2 but it fails to compile in FPC main. The error is:

Incompatible type for arg no. 1: Got "Array[0..15] Of Byte", expected "TBytes"

Is this intentional? Was the previous behavior a bug? I can vaguely remember being mildly surprised when I found out that I can use it like in the code above, but then did not spend any more thoughts on it.


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Open Array vs Dyn Array as argument type in functions, changes in FPC 3.3
« Reply #1 on: September 23, 2021, 12:20:38 pm »
I don't know, but if you look at the 3.2.x assembler, it effectively seems to create a TBytes temp, and this is indeed not very nice:

Code: [Select]
leal -4(%ebp),%eax
movl $RTTI_$P$PROJECT1_$$_TBYTES,%edx
call fpc_dynarray_clear
movl $16,-8(%ebp)
leal -8(%ebp),%eax
pushl %eax
movl $RTTI_$P$PROJECT1_$$_TBYTES,%edx
movl $1,%ecx
leal -4(%ebp),%eax
call fpc_dynarray_setlength
movl -4(%ebp),%edi
movl $U_$P$PROJECT1_$$_X,%esi
movl $4,%ecx
rep
movsl
movl -4(%ebp),%eax
call P$PROJECT1_$$_BAR$TBYTES

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Open Array vs Dyn Array as argument type in functions, changes in FPC 3.3
« Reply #2 on: September 23, 2021, 01:28:11 pm »
The above code has been compiling and working fine in FPC 3.2 but it fails to compile in FPC main. The error is:

Incompatible type for arg no. 1: Got "Array[0..15] Of Byte", expected "TBytes"

Is this intentional? Was the previous behavior a bug? I can vaguely remember being mildly surprised when I found out that I can use it like in the code above, but then did not spend any more thoughts on it.

Yes, this is intentional and documented.

 

TinyPortal © 2005-2018