declaring operator inc crashed the compiler lazarus / 64-FPC development ---------------------------------------------------------------------- engkin · 2019-04-09 05:47 FPC 3.0.4 and FPC 3.3.1-r41846 Windows 32. The following code is crashing the compiler: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} Type  TAZ=String; operator inc(az: TAZ; i: integer=1) raz:TAZ; inline;beginend; beginend. Specifically, adding a default value to the second parameter caused the crash. fpc.exe -MObjFPC -Scghi -O1 -l -vewnhibq -Filib\i386-win32 -Fu. -FUlib\i386-win32 -FE. -oProject1.exe QuoteFree Pascal Compiler version 3.0.4 [2017/12/03] for i386 Copyright (c) 1993-2017 by Florian Klaempfl and others (1002) Target OS: Win32 for i386 (3104) Compiling Project1.pas Fatal: (1018) Compilation aborted An unhandled exception occurred at $004770C0: EAccessViolation: Access violation   $004770C0   $004D5CA5   $004F9170   $004F98A1   $0054024B   $004330D7   $00413A79 Error: C:\laz4android2.0.0\fpc\3.0.4\bin\i386-win32\ppc386.exe returned an error exitcode Is it known? Can you test and confirm? Edit: Thaddy opened two bug reports: 35347 and 35348. ---------------------------------------------------------------------- WooBean · 2019-04-09 06:23 Hi, just a clue: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} Type  TAZ=String; //operator inc(az: TAZ; i: integer=1) raz:TAZ; inline; // COMPILER (FPC 3.0.4/win32) ERROR//WHERE SOMEBODY ALLOWED THAT SYNTAX FOR DECLARING operator?//operator inc(az: TAZ; i: integer) raz:TAZ; inline;//begin//end; function inc(az: TAZ; i: integer=1):TAZ; inline; //very BAD naming idea, but it compiles anywaybegin  //if called as inc(az) it uses default parameter i=1end;  beginend. ---------------------------------------------------------------------- Nitorami · 2019-04-09 06:36 Tested with fpc3.0.4, win32 and confirm this crashes the compiler. It seems like the compiler cannot handle default parameters within operator declarations. Not sure whether your construct makes a lot of sense in the first place, but the compiler should not crash on it. Issue a bug report ? ---------------------------------------------------------------------- Thaddy · 2019-04-09 06:42 Yes, confirmed (trunk41386) with regard to the default value being the cause. Same goes - as expected - for dec. The TAZ type doesn't matter, same with other types like double. Note that inc/dec look like a special case, since the same signature looks to cover two call types. Side note: I knew this was possible (w/o default), but is it documented somewhere? it is still not in the official docs, not even trunk. ---------------------------------------------------------------------- munair · 2019-04-09 07:16 Also confirmed with FPC 3.0.4 on Linux x64. ---------------------------------------------------------------------- Thaddy · 2019-04-09 07:29 Ah, doc is under function overloading. (wrong place? or is procedure missing?) https://freepascal.org/docs-html/ref/refse93.html It seems there is a conflict with the procedure signatures for  inc(var Taz); overload when using a default. Still the compiler should not crash, but at least it explains it. inc/dec are procedures and not operators. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+} Type  TAZ=String;procedure inc(var az:TAZ);inline;beginend; procedure inc(var az: TAZ; i: integer); inline;beginend; beginend. But is accepted as operator, which is strange and afaik undocumented. Not even in the trunk docs. This http://wiki.freepascal.org/Operator_overloading does not count, since no official doc. ---------------------------------------------------------------------- howardpc · 2019-04-09 07:51 Adding an ampersand to inc (&inc) produces this error message, and the compiler does not attempt anything: project1.lpr(8,14) Error: It is not possible to overload this operator. Related overloadable operators (if any) are: ---------------------------------------------------------------------- Thaddy · 2019-04-09 07:59 Howard, it is a bit more complex and there are two issue that I am reporting. [edit] mantis 31347 and 31348 ---------------------------------------------------------------------- Akira1364 · 2019-04-09 14:58 Inc and Dec are definitely operators, and always have been AFAIK. They're equivalent to ++ and -- in other languages the same way that shl and shr are equivalent to << and >>. The default implementations are constructed by the compiler itself in one of the built-in "inline nodes", as direct addition and subtraction respectively. Build any FPC program you want with the -a flag to output the assembly listing to file, and you'll never ever see a call to procedures called Inc or Dec, because they don't exist. Look here if you don't believe me: https://github.com/graemeg/freepascal/blob/master/compiler/symtable.pa s#L459 Anyways, here's an example that shows how engkin can solve their problem (they need to overload the addition operator instead to handle two different types at the same time): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Example; {$mode ObjFPC}{$H+}{$coperators On} uses SysUtils; type TAZ = String; // Overloads for Inc can only be declared with both the parameter type // and the return type being all the the same.// Note that yes, this "looks" like a function, but it still only works like a procedure.operator Inc(const AZ: TAZ): TAZ; inline;begin  Result := AZ + AZ;end; // This is also valid.// Again, while it looks like a function, it works like a procedure.operator Inc(const A, B: TAZ): TAZ; inline;begin  Result := A + B;end; // You can implement what you want with the + operator, though.operator +(const AZ: TAZ; const I: Integer): TAZ; inline;begin  Result := AZ + IntToStr(I);end; var S: TAZ = 'Hello!'; begin  Inc(S);   // prints 'Hello!Hello!'  WriteLn(S);    Inc(S, S);    // prints 'Hello!Hello!Hello!Hello!'  WriteLn(S);   // += is not a different operator in FPC, just a combination of + and :=, so the next line works.  S += 55;   // prints 'Hello!Hello!Hello!Hello!55'  WriteLn(S);end. ---------------------------------------------------------------------- Thaddy · 2019-04-09 15:55 Indeed Not too fine a point on it but did you read the reaction on the bug report__ ---------------------------------------------------------------------- Akira1364 · 2019-04-09 16:36 Took a look at the bug report just now... it's true that you can't use them in expressions in FPC, but that's just an implementation detail. For example, if FPC added actual ++ and -- syntax (the same way it does in fact have << and >> syntax) it would be directly equivalent to Inc and Dec from the perspective of the compiler, just as << and >> are directly equivalent to shl and shr. Either way, the definition of Inc and Dec in the docs as being "procedures" is not actually extracted from real source code that exists anywhere in the FPC codebase. It is definitely a bug that engkin's original code actually crashes the compiler, though. It should just fail to compile it with a normal error message (as again it's not a valid way to declare an Inc overload.) ---------------------------------------------------------------------- engkin · 2019-04-10 04:25 @WooBean, thank you, yes Inc is considered operator. I noticed it from the error message I received when tried the following without declaring Inc: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   inc(sAZ1);QuoteProject1.pas(65,3) Error: Operator is not overloaded: inc "AnsiString" so I decided to try this "operator". @Nitorami, thank you for testing. The compiler should not crash. The code, stripped down to show the problem, originally related to this post. @Thaddy, thank you for testing. I really appreciate the extra effort you put in considering Dec, checking the docs, and reporting the problem. You did not just report the crash!!  I'll update the first post with links to your bug reports. @Munair, initially I was wondering about Linux and 64bit. Thank you for checking. @howardpc, this seems to be the second error message where the compiler calls Inc or &Inc an "operator". @Akira1364, professional help worth gold. I have confidence if I ever get stuck, members of this forum will help me out. Thank you. ---------------------------------------------------------------------- munair · 2019-04-10 06:02 Quote from: Akira1364 on April 09, 2019, 04:58:07 pmThe default implementations are constructed by the compiler itself in one of the built-in "inline nodes", as direct addition and subtraction respectively. Build any FPC program you want with the -a flag to output the assembly listing to file, and you'll never ever see a call to procedures called Inc or Dec, because they don't exist. Look here if you don't believe me: I believe you. I thought it was common knowledge that inc and dec are replaced by the compiler by their equivalent operators (for obvious reasons). ---------------------------------------------------------------------- Thaddy · 2019-04-10 06:42 Michael adapted the documentation. The crash is still open. ---------------------------------------------------------------------- ASerge · 2019-04-10 07:14 By the way, in Delphi Round and Trunc also operators. ---------------------------------------------------------------------- PascalDragon · 2019-04-10 07:18 Quote from: ASerge on April 10, 2019, 09:14:25 amBy the way, in Delphi Round and Trunc also operators. ---------------------------------------------------------------------- engkin · 2019-04-10 12:54 Quote from: PascalDragon on April 10, 2019, 09:18:56 amQuote from: ASerge on April 10, 2019, 09:14:25 amBy the way, in Delphi Round and Trunc also operators. Shell I open a bug report? ---------------------------------------------------------------------- munair · 2019-04-10 16:18 Quote from: engkin on April 10, 2019, 02:54:55 pmQuote from: PascalDragon on April 10, 2019, 09:18:56 amQuote from: ASerge on April 10, 2019, 09:14:25 amBy the way, in Delphi Round and Trunc also operators. Shell I open a bug report?  Please do. ---------------------------------------------------------------------- Thaddy · 2019-04-18 05:44 Sven has fixed the crash. Note that inc/dec are unary operators and take just one! parameter. You can overload the procedures, though, if needed. To sum up operator inc(var value:integer; increment:integer =1); is now forbidden.(and doesn't crash, but throws an error) procedure inc/dec overloads are legal. Michael has also  updated the documentation, as mentioned previously.