Recent

Author Topic: Bug with constref?  (Read 213 times)

LemonParty

  • Hero Member
  • *****
  • Posts: 527
Bug with constref?
« on: May 17, 2026, 05:11:29 pm »
I am trying to compile the next code:
Code: Pascal  [Select][+][-]
  1. {$mode ObjFPC}{$H+}
  2.  
  3. function F(constref A): UInt32;
  4. var
  5.   D: UInt32 absolute A;
  6. begin
  7.   Result:= D;
  8. end;
  9.  
  10. function F2(constref Arr): Integer;
  11. var
  12.   aArr: array [Byte] of Byte absolute Arr;
  13. begin
  14.   Result:= F(aArr[0]);
  15. end;
  16.  
  17. begin
  18.        
  19. end.
And getting
Quote
(14,17) Error: Can't assign values to const variable
When use const instead of constref code compiles.
Is it bug?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

ASerge

  • Hero Member
  • *****
  • Posts: 2496
Re: Bug with constref?
« Reply #1 on: May 17, 2026, 05:28:10 pm »
Simplified version:
Code: Pascal  [Select][+][-]
  1. {$mode ObjFPC}{$H+}
  2.  
  3. procedure F(constref A); // or var A
  4. begin
  5. end;
  6.  
  7. procedure F2(const Arr: TIntegerArray); // var solve problem
  8. begin
  9.   F(Arr[0]);
  10. end;
  11.  
  12. begin
  13.  
  14. end.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12894
  • FPC developer.
Re: Bug with constref?
« Reply #2 on: May 17, 2026, 05:46:33 pm »
And getting
Quote
(14,17) Error: Can't assign values to const variable
When use const instead of constref code compiles.
Is it bug?

Passing to a constref means taking the address. That might not be allowed for CONST parameters. My gut feeling that if it is a bug, it is the quality of the error message.

LemonParty

  • Hero Member
  • *****
  • Posts: 527
Re: Bug with constref?
« Reply #3 on: May 17, 2026, 07:15:45 pm »
Reported on bugtracker.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018