[fpc-devel] pure function feedback

J. Gareth Moreton gareth at moreton-family.com
Sun Jun 30 04:17:17 CEST 2024


Fixed 2 and 3.

- 2 was due to not handling block nodes properly when they didn't have 
any statements (data-flow analysis stripped the procedure completely 
clean since no result was set), hence an access violation occurred while 
accessing a node that was nil.

- 3 was a two-fold problem:

 1. The functiuon being ineligible was because a typeconv node couldn't
    be stripped out and so the final assignment tree wasn't in an
    expected format.  The analysis code now specifically permits this
    form of type conversion (convtype = tc_equal and identical
    resultdefs between the typeconv and the load nodes).
 2. The second problem was due to a small hack where implicit Result
    parameters were converted from var to out so data-flow analysis
    worked properly, but they weren't converted back to var parameters
    afterwards, causing an access violation elsewhere in the code.

Merge request has been updated - I have also added two new tests based 
on your examples.  Thanks again Marģers.

Kit

On 28/06/2024 13:24, J. Gareth Moreton via fpc-devel wrote:
> Hi Marģers,
>
> Thanks for the feedback!
>
> Assigning the results of pure functions to constants has not yet been 
> developed.  I had planned to add it once the bulk of pure functions 
> (i.e. the current merge request) had been approved since it requires 
> some additional work (specifically what happens if the function is not 
> actually pure or otherwise can't return a straightforward result).
>
> 2 and 3 are definite bugs though.  The second case should at the very 
> least make the function impure, while the third case should indeed not 
> disqualify the function.  I'll see what I can do. Thanks again.
>
> Kit
>
> On 28/06/2024 09:35, Marģers . via fpc-devel wrote:
>> 1. pure function value to constants
>>
>> function foo(i:longword):longword; pure;
>> begin
>>   foo:=9;
>> end;
>>
>> const bar = foo( 8 );  //-- not allowed.. but i expect this to work!
>>          jar : longword = foo( 7 ); //-- and this as well
>>
>>
>> 2. this does not end grasefully
>>
>> function foo (i:longword):shortstring; pure;
>> var s : shortstring;
>>       n : longword;
>> begin
>>   s:=''';
>>   for n:=1 to i do s:=s +'a';
>>   //-- not assing return value
>> end;
>> begin writeln(foo(9)); end.
>>
>> 3.
>> function foo (i:longword):ansistring; pure;
>> var s : shortstring;
>>       n : longword;
>> begin
>>   s:=''';
>>   for n:=1 to i do s:=s +'a';
>>   foo:=s; //-- this makes function not pure, but why whould it!
>> end;
>> begin writeln(foo(9)); end.
>>
>>
>> _______________________________________________
>> fpc-devel maillist  - fpc-devel at lists.freepascal.org
>> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>

-- 
This email has been checked for viruses by Avast antivirus software.
www.avast.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20240630/e3d71db1/attachment.htm>


More information about the fpc-devel mailing list