[fpc-pascal] Property write access on records

Sven Barth pascaldragon at googlemail.com
Fri Mar 6 23:15:43 CET 2020

Am 06.03.2020 um 03:17 schrieb Ryan Joseph via fpc-pascal:
>> On Mar 5, 2020, at 2:24 PM, Ryan Joseph <genericptr at gmail.com> wrote:
>> Can we consider changing this behavior?
> from https://lists.freepascal.org/pipermail/fpc-pascal/2020-March/057434.html
> On 05/03/2020 08:24, Ryan Joseph via fpc-pascal wrote:
>   Yes, it's a temporary variable but doesn't it make sense to give an error because the property is read only? It's basically a no-op like it is now and defeats the purpose of read-only properties. It also creates a nasty bug because the programmer thinks they've written to something but they actually didn't.
> I think there is already an open bug report for this.
> Jonas
> ================
> I looked on the bug tracker for "properties" and didn't see this so I'll make one but I wanted to confirm first it's not intended behavior. Jonas seems to hint that it is but Sven didn't mention it.

I've found two bug reports related to this (I searched for "property" 
and only those reports that are neither closed nor resolved):
- https://bugs.freepascal.org/view.php?id=23620
- https://bugs.freepascal.org/view.php?id=14534

And I noticed that the following already does generate an error:

=== code begin ===

program trecprop;

{$mode objfpc}

   TTestRec = record
     x: LongInt;

   { TTestClass }

   TTestClass = class(TObject)
     fTestRec: TTestRec;
     function GetTestRec: TTestRec;
     property Rec: TTestRec read fTestRec;
     property Rec2: TTestRec read GetTestRec;

   c: TTestClass;

function TTestClass.GetTestRec: TTestRec;
   Result := fTestRec;

   c := TTestClass.Create;
     c.Rec.x := 42;
     c.Rec2.x := 42;

=== code end ===

=== shell begin ===

PS C:\fpc\git> .\compiler\ppcx64.exe -n -Furtl\units\x86_64-win64 -viwn 
-FEtestoutput .\fpctests\trecprop.pp
Target OS: Win64 for x64
Compiling .\fpctests\trecprop.pp
trecprop.pp(32,10) Error: Argument cannot be assigned to
trecprop.pp(33,11) Error: Argument cannot be assigned to
trecprop.pp(38) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted

=== shell end ===

What currently does not generate an error is if you use a with-statement 
with the property (which is referenced by the bug reports).

So would you please provide a full example that shows your error? Cause 
if there is yet another case where the compiler does not generate an 
error where it should (aside from the with-statement) then this would be 
good to know.


More information about the fpc-pascal mailing list