Recent

Author Topic: Open array parameter accepts non-array var  (Read 8823 times)

riwu

  • New Member
  • *
  • Posts: 15
Re: Open array parameter accepts non-array var
« Reply #15 on: January 27, 2017, 01:43:33 am »
Interesting issue. I have tried this and found that FPC 3.0.0 generates a compile error "Assignments to formal parameters and arrays are not possible", but the error does not appear if the call to f is made from within a while loop. Odd.

{$mode objfpc}
procedure f(arr: array of ShortInt);
begin
  while true do //uncommenting the while makes the difference
    f(arr[0]);
end;
This is the error i'm getting.
Like what others mentioned, it only happens at O2 and above.

Here's my version: http://i.imgur.com/N79ZWM8.png

Lpi config:
Code: [Select]
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
  <ProjectOptions>
    <Version Value="9"/>
    <PathDelim Value="\"/>
    <General>
      <Flags>
        <MainUnitHasCreateFormStatements Value="False"/>
        <MainUnitHasTitleStatement Value="False"/>
      </Flags>
      <SessionStorage Value="InProjectDir"/>
      <MainUnit Value="0"/>
      <Title Value="Project1"/>
      <UseAppBundle Value="False"/>
      <ResourceType Value="res"/>
    </General>
    <i18n>
      <EnableI18N LFM="False"/>
    </i18n>
    <VersionInfo>
      <StringTable ProductVersion=""/>
    </VersionInfo>
    <BuildModes Count="1">
      <Item1 Name="Default" Default="True"/>
    </BuildModes>
    <PublishOptions>
      <Version Value="2"/>
    </PublishOptions>
    <RunParams>
      <local>
        <FormatVersion Value="1"/>
      </local>
    </RunParams>
    <Units Count="1">
      <Unit0>
        <Filename Value="Project1.pas"/>
        <IsPartOfProject Value="True"/>
      </Unit0>
    </Units>
  </ProjectOptions>
  <CompilerOptions>
    <Version Value="11"/>
    <PathDelim Value="\"/>
    <Target>
      <Filename Value="Project1"/>
    </Target>
    <SearchPaths>
      <IncludeFiles Value="$(ProjOutDir)"/>
      <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
    </SearchPaths>
    <CodeGeneration>
      <Optimizations>
        <OptimizationLevel Value="2"/>
      </Optimizations>
    </CodeGeneration>
    <Linking>
      <Debugging>
        <GenerateDebugInfo Value="False"/>
      </Debugging>
    </Linking>
  </CompilerOptions>
  <Debugging>
    <Exceptions Count="3">
      <Item1>
        <Name Value="EAbort"/>
      </Item1>
      <Item2>
        <Name Value="ECodetoolError"/>
      </Item2>
      <Item3>
        <Name Value="EFOpenError"/>
      </Item3>
    </Exceptions>
  </Debugging>
</CONFIG>

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2285
    • havefunsoft.com
Re: Open array parameter accepts non-array var
« Reply #16 on: January 27, 2017, 02:54:04 am »
let's do something stupid then:
Code: Pascal  [Select]
  1. procedure f(arr: array of Integer); overload;
  2. begin
  3.   {$OPTIMIZATION OFF}
  4.   //while true do  
  5.     f(arr[0]);
  6.   {$OPTIMIZATION DEFAULT}
  7. end;
  8.  
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Handoko

  • Hero Member
  • *****
  • Posts: 3200
  • My goal: build my own game engine using Lazarus
Re: Open array parameter accepts non-array var
« Reply #17 on: January 27, 2017, 04:58:55 am »
I performed the test again on Win7, and these are what I found:

- Compile won't fail on optimization level 0 and level 1.
- Compile fails on optimization level 2, 3, 4.
- {$OPTIMIZATION OFF} makes no difference.

Thaddy

  • Hero Member
  • *****
  • Posts: 9194
Re: Open array parameter accepts non-array var
« Reply #18 on: January 27, 2017, 06:24:18 am »
After some research this is also the case on arm. It seems a bug in the high level optimizer at level 2 or higher.
I had to bring the conditionals to surround the begin end; block.
Code: Pascal  [Select]
  1. procedure f(arr: array of Integer); overload;
  2. {$PUSH}{...$OPTIMIZATION OFF}
  3. begin
  4.  
  5.   //while true do  
  6.     f(arr[0]);
  7. end;
  8. {$POP}
  9.  
Renders:
Code: [Select]
fpc -CX -XXs -O4 tests.pas
Free Pascal Compiler version 3.1.1-r35333 [2017/01/26] for arm
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for ARMHF
Compiling tests.pas
tests.pas(5,1) Error: Assignments to formal parameters and open arrays are not possible
tests.pas(16) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/local/bin/ppcarm returned an error exitcode

And this code:
Code: Pascal  [Select]
  1. program tests;
  2. {$mode delphi}
  3. procedure f(arr: array of Integer); overload;
  4. {$PUSH}{$OPTIMIZATION OFF}
  5. begin
  6.   //while true do  
  7.     f(arr[0]);
  8. end;
  9. {$POP}
  10.  
  11. var
  12.   a : integer;
  13. begin
  14.   f(a);
  15. end.
Renders:
Code: [Select]
fpc -CX -XXs -O4 tests.pas
Free Pascal Compiler version 3.1.1-r35333 [2017/01/26] for arm
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for ARMHF
Compiling tests.pas
tests.pas(14,6) Warning: Variable "a" does not seem to be initialized
Linking tests
Finally this code:
Code: Pascal  [Select]
  1. program tests;
  2. {$mode delphi}
  3. procedure f(arr: array of Integer); overload;
  4. {$PUSH}{.....$OPTIMIZATION OFF}
  5. begin
  6.   while true do  
  7.     f(arr[0]);
  8. end;
  9. {$POP}
  10.  
  11. var
  12.   a : integer;
  13. begin
  14.   f(a);
  15. end.
Renders:
Code: [Select]
fpc -CX -XXs -O4 tests.pas
Free Pascal Compiler version 3.1.1-r35333 [2017/01/26] for arm
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for ARMHF
Compiling tests.pas
tests.pas(14,6) Warning: Variable "a" does not seem to be initialized
Linking tests



« Last Edit: January 27, 2017, 06:52:56 am by Thaddy »
also related to equus asinus.