Recent

Author Topic: Why with allows assignment  (Read 41235 times)

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Why with allows assignment
« Reply #30 on: January 03, 2025, 09:43:19 pm »
What do you think should be result of this:
Code: Pascal  [Select][+][-]
  1. function Test: TTest;
  2. const
  3.   ctr: Integer = 0;
  4. begin
  5.   Result.I := ctr;
  6.   Inc(ctr);
  7. end;
  8.  
  9. with Test do
  10. begin
  11.   WriteLn(I);
  12.   WriteLn(I);
  13. end;
The current semantics of with is that you access the result of a single Test call, so both WriteLns are giving the same result.
I mean if you personally think, with should be nothing more than a stupid macro, replacing I with Test.I, that's a opinion you can have, but it's specifically not how with is intended to be used for decades

Also it's literally the only part of with which isn't awful and makes it actually a useful feature:
Code: Pascal  [Select][+][-]
  1. with TstringList.Create do
  2. try
  3.   ...
  4. finally
  5.   Free;
  6. end;
« Last Edit: January 03, 2025, 09:48:22 pm by Warfley »

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #31 on: January 03, 2025, 10:00:37 pm »
that's a opinion you can have, but it's specifically not how with is intended to be used for decades
Nice try.  That's _not_ an opinion. 

Find a formal Pascal standard that supports what you're saying.  Good luck!!  Just in case, FPC is _not_ a Pascal standard.

BTW, do you think assigning a value to a function should be allowed outside of the function ?  Are function results lvalues ?  (looks like in some cases, to FPC, they are.)

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Why with allows assignment
« Reply #32 on: January 03, 2025, 10:31:25 pm »
Find a formal Pascal standard that supports what you're saying.  Good luck!!  Just in case, FPC is _not_ a Pascal standard.

BTW, do you think assigning a value to a function should be allowed outside of the function ?  Are function results lvalues ?  (looks like in some cases, to FPC, they are.)

I don't care what the Pascal standards state, most people never write standard Pascal. ISO Pascal does not even support units. Pascal, at least the dialects people use in the real world, is not standardized, it's a quasi standard like java, where the major distributions define the functionality through their implementations and documentation.

This is intended behavior for FPC, like it or not, but it's how the language works

And what cases do you mean? I only know of cases where the result of a function will be written into a temporary variable, like with does. Functions don't Return lvalues, but their result can be written to an object which can be used as an lvalue

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: Why with allows assignment
« Reply #33 on: January 03, 2025, 10:43:33 pm »
What a load of..
back to the question:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.   TTest = record
  5.     V: Integer;
  6.   end;
  7.  
  8. var
  9.   X: TTest;
  10.  
  11.   function Test: TTest;
  12.   begin
  13.     Result := X;
  14.   end;
  15.  
  16. var
  17.   T: TTest;
  18. begin
  19.   with Test do
  20.     V := 1; //No Error
  21.   // did everybody overlook this due to the festive days.... >:( >:D
  22.   //Test.V := 1; // of course that's an error, you can't do that with a *function* test
  23.     T.V := 1; // is as it should be
  24. end.
               
Simply stupid error and nothing to do with "with".
 6 days from home and the kids can't even read the question properly.
« Last Edit: January 03, 2025, 10:45:05 pm by Thaddy »
But I am sure they don't want the Trumps back...

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #34 on: January 03, 2025, 11:06:12 pm »
I don't care what the Pascal standards state,
and that's one of the many reasons Pascal has been going nowhere for years now.

most people never write standard Pascal.
Most people do write standard Pascal, admittedly with a lot of extensions but, a lot of FPC is standard Pascal, e.g, compound statements, if statements, while statements, etc, etc, etc.   Wow!! unknowingly, you've been writing standard Pascal.

ISO Pascal does not even support units. Pascal, at least the dialects people use in the real world, is not standardized, it's a quasi standard like java, where the major distributions define the functionality through their implementations and documentation.
A lot of that is true but, if you knew how to write a compiler you'd know that "with" does _not_ create any temporaries and you'd also know that, outside of a function, function results cannot be treated as lvalues and, not only that, you'd also know why.

This is intended behavior for FPC, like it or not, but it's how the language works
I don't know if it is intended or not but, one thing is absolutely certain, it is wrong. 

And what cases do you mean? I only know of cases where the result of a function will be written into a temporary variable, like with does.
It is NOT the "with" that causes the function result to be held into a temporary variable.  The result will be held in a temporary variable if it does not fit into a register (or sometimes, two) whether or  not there is a "with".  You should know that, it is _obvious_ and the reason why is obvious too.

Functions don't Return lvalues,
Of course they don't and for that very reason a value cannot be assigned to them outside of the function.   You cannot have "FunctionName.FunctionRecordFieldname := somevalue" whether there is a "with" or not because as you stated, function results cannot be treated as lvalues outside of the function.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Why with allows assignment
« Reply #35 on: January 03, 2025, 11:18:08 pm »
It is NOT the "with" that causes the function result to be held into a temporary variable.  The result will be held in a temporary variable if it does not fit into a register (or sometimes, two) whether or  not there is a "with".  You should know that, it is _obvious_ and the reason why is obvious too.

I already linked the respective position in the FPC source but here it is:
Code: Pascal  [Select][+][-]
  1.                 if not valid_for_addr(p,false) then
  2.                   begin
  3.                     calltempnode:=ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent,true);
  4.                     addstatement(newstatement,calltempnode);
  5.                     addstatement(newstatement,cassignmentnode.create(
  6.                         ctemprefnode.create(calltempnode),
  7.                         p));
  8.                     p:=ctemprefnode.create(calltempnode);
  9.                     typecheckpass(p);
  10.                   end;
  11.  
Do you see the ctempcreatenode.create? It creates a new temporary variable, then assigns the return from the function to it (cassignmentnode.create), before creating references to this node to be used instead of the call. All of that in the function _with_statement in pstatmnt.pas

I'm sorry but what you wrote is just plainly wrong. With creates a new temporary object for the result of the function, to be referenced within the with scope.

LV

  • Full Member
  • ***
  • Posts: 219
Re: Why with allows assignment
« Reply #36 on: January 03, 2025, 11:55:12 pm »
I was passing by

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: Why with allows assignment
« Reply #37 on: January 04, 2025, 12:11:09 am »
same thing, you try to access the function test, which is utter nonsense.
You need to do it through the result of the function.
But I am sure they don't want the Trumps back...

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #38 on: January 04, 2025, 12:12:02 am »
With creates a new temporary object for the result of the function, to be referenced within the with scope.
A temporary will be created whether or not "with" is used simply because the function result must be usable whether or not a "with" is present.

The presence of the "with" has nothing to do with the creation of a temporary.  The fact that FPC seems to be creating it while processing the "with" is irrelevant, it would create it anyway had there been no "with".

If you need to see this, simply assign a variable a field of a function that returns a record.  you'll see a temporary whether or not a "with" is present, i.e, somevar := somefunction.recordfield;



(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #39 on: January 04, 2025, 12:24:45 am »
@LV,

How did you get it to emit an error ?   I am using v3.2.2 and it compiles.

Thanks.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Why with allows assignment
« Reply #40 on: January 04, 2025, 12:40:24 am »
With creates a new temporary object for the result of the function, to be referenced within the with scope.
A temporary will be created whether or not "with" is used simply because the function result must be usable whether or not a "with" is present.
No that's simply wrong, you do not need to create a temp node. The FPC node tree is implemented in a way you cann use the call node directly. Infact if you go to line 640 and skip the if (just replace the long condition with a simple true) no temp object will be created in the node tree, and with behaves exactly as you intend it to.

The whole block with the temp node is specifically created to handle with statements to not be simple macros. If with was implemented like you want it to be, the whole _with_statement function would be 100 lines shorter

PS: under certain circumstances will the call node create it's own temporary, e.g. when the result is a managed type (e.g. if the record contains an ansistring). In this case the with statement will create an additional temporary and copy the data from one temporary to the other, which is actually quite inefficient.
So yes sometimes a new temporary is always created for the function call, but if that is the case, with will create another one on top
« Last Edit: January 04, 2025, 12:43:35 am by Warfley »

LV

  • Full Member
  • ***
  • Posts: 219
Re: Why with allows assignment
« Reply #41 on: January 04, 2025, 12:42:10 am »
@440bx. I set a breakpoint, ran the program, then pressed F8, F8, and hovered over V.
Lazarus 3.4 (rev lazarus_3_4) FPC 3.2.2 x86_64-win64-win32/win64.
best regards.

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #42 on: January 04, 2025, 01:21:18 am »
No that's simply wrong,
Really ??? where do you think the result of a function that returns a type that does not fit in a register goes ???  in a sardine can ? a ziploc bag ? a mailing envelop ???

or might it just be a metaphysical "ethereal' entity ?   of course, it cannot possibly be go into a temporary area... (unless there is a "with")...  unbelievable!!!!



@LV,

I mistakenly thought the yellow popup indicated a compile error.  I realize now, it is a debugger message.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Why with allows assignment
« Reply #43 on: January 04, 2025, 01:27:19 am »
Really ??? where do you think the result of a function that returns a type that does not fit in a register goes ???  in a sardine can ? a ziploc bag ? a mailing envelop ???

or might it just be a metaphysical "ethereal' entity ?   of course, it cannot possibly be go into a temporary area... (unless there is a "with")...  unbelievable!!!!

I've been talking about the node tree, the intermediate representation used by the FPC, not the assembly to which it is converted. And the FPC already introduces a new temporary object for the with statement on the node tree, which then can either be a memory location or a register depending on the backend, data type and optimization. But also are temp nodes not the only way the Compiler can allocate memory. They are just a tool to manage internal temporary objects that are needed for some structures such as with statements, type conversions or others. If you don't know about the FPC internals don't lecture me on what the FPC does for which purpose
« Last Edit: January 04, 2025, 01:33:25 am by Warfley »

440bx

  • Hero Member
  • *****
  • Posts: 5010
Re: Why with allows assignment
« Reply #44 on: January 04, 2025, 01:47:42 am »
I know very little about FPC internals but, that is neither here nor there.  The subject is what the "with" statement is supposed to do and one of the thing it neither does nor is supposed to do is to create temporary variables _even_ if that's what an _incorrectly_ implemented compiler does.

The presence or absence of a "with" statement should NOT affect the result. 

if it does then it's a bug in the compiler.  It's that simple.    Showing me (or anyone else) buggy code does NOT change that.

BTW, you haven't explained yet how it can be correct to assign a value to a function result outside of the function. 

and just for fun, consider this code:
Code: Pascal  [Select][+][-]
  1.   with Test, Test, Test, Test, Test, Test, Test do
  2.     V := 1; //No Error
  3.  
Is the compiler supposed to create 7 temporary variables ? (remember, according to you, the temporary is created because of the "with" not the fact that the function returns a structured type.)  If that were the case, which one of the temporaries gets assigned the value ??? 

BTW, you could create Test, Test1, Test2, Test3 and so forth and have a "with" statement "with" statement with several _dozens_ of these functions randomly mixed.   How many temporaries would the "with" create ?  which one(s) would get assigned a value ? and why ?

BOTTOM LINE: in the example given by the OP, FPC is not processing the "with" statement correctly.  It is a BUG because among other reasons it is allowing a value to be assigned to a function outside of the function definition.  That alone proves it is a bug, not to mention that (yet again!) the presence or absence of a "with" should make no difference in the result.

LASTLY: I am done explaining this.  Enough is enough!
« Last Edit: January 04, 2025, 01:49:21 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018