View Issue Details

IDProjectCategoryView StatusLast Update
0023620FPCCompilerpublic2020-03-07 16:25
ReporterAndreas Schneider Assigned To 
PrioritynormalSeverityfeatureReproducibilityalways
Status newResolutionopen 
Summary0023620: Handle with-statement to modify records in properties with setters
DescriptionThe attached example demonstrates the current behavior:
Using the with-statement on a property that directly reads/writes a record member, it successfully compiles and runs; i.e. at runtime, the values of the record fields are actually set.

When using the with-statement on a property that uses a setter, the compiler doesn't complain, but on runtime, the actual record fields stay unchanged.

At worst, this should throw a compiler error (or warning). At best, this should be handled cleanly, by calling the setter with the modified record. (Although it's probably up for discussion, whether it is clean to implicitly call methods in that case ...)
Additional InformationI don't have Delphi at hand to test how it behaves in this particular case.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Relationships

related to 0014534 new readonly properties are writeable by "with" 
has duplicate 0036029 resolvedJonas Maebe Compiler throws no error on writing to individual record fields via a setter 
related to 0036768 new Record helper property doesn't respect read-only state 

Activities

2013-01-09 19:41

 

fpcwith.lpr (1,351 bytes)

Serge Anvarov

2019-08-31 06:31

reporter   ~0117888

In recent versions of Delphi, this causes a compilation error "E2064 Left side cannot be assigned to". For compatibility it is better to give an error. This is better than generating code that produces unexpected results.

Thaddy de Koning

2019-08-31 09:08

reporter   ~0117890

Last edited: 2019-08-31 09:11

View 3 revisions

Yes the with statement should throw an error here, but it does not.
A record property only takes a full record and you can not set individual members directly.
You need to fill an intermediate variable of TTestRec and Assign that to the Test2 property. e.g:
var r:TTestRec;
.....
    r.a:=false;r.b:='some other text';
    test2 := r;

The "Left side cannot be assigned to" error is correct (as is the behavior without with, that errors with " Argument cannot be assigned to":
e.g.:
tc.test2.a :=false;
tc.test2.b:='some other text';

And that is correct behavior. Both in Delphi and in Freepascal

jamie philbrook

2020-03-07 16:25

reporter   ~0121436

I made a good suggestion for this for a nice compiler enhancement but I guess no one likes it...

SomeRecordProperty := With SomeRecordPorperty do Begin......End;

This would simply assign the background Record Property that WITH gets back to the Setter of the class/Record/Object etc.

 All the compiler needs to do is use the already existing background Record and send it back to the setter, if it has one of course.
 
 Just a modification of the "WITH" being able to return a value for setters or variables locally etc..

Issue History

Date Modified Username Field Change
2013-01-09 19:41 Andreas Schneider New Issue
2013-01-09 19:41 Andreas Schneider File Added: fpcwith.lpr
2013-01-09 19:57 Jonas Maebe Relationship added related to 0014534
2019-08-30 22:56 Jonas Maebe Relationship added has duplicate 0036029
2019-08-31 06:31 Serge Anvarov Note Added: 0117888
2019-08-31 09:08 Thaddy de Koning Note Added: 0117890
2019-08-31 09:08 Thaddy de Koning Note Edited: 0117890 View Revisions
2019-08-31 09:11 Thaddy de Koning Note Edited: 0117890 View Revisions
2020-03-07 15:54 Michael Van Canneyt Relationship added related to 0036768
2020-03-07 16:25 jamie philbrook Note Added: 0121436