Recent

Author Topic: declaring operator inc crashed the compiler  (Read 6116 times)

engkin

  • Hero Member
  • *****
  • Posts: 3112
declaring operator inc crashed the compiler
« on: April 09, 2019, 07:47:31 am »
FPC 3.0.4 and FPC 3.3.1-r41846
Windows 32.

The following code is crashing the compiler:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. Type
  6.   TAZ=String;
  7.  
  8. operator inc(az: TAZ; i: integer=1) raz:TAZ; inline;
  9. begin
  10. end;
  11.  
  12. begin
  13. end.

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

Quote
Free 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.
« Last Edit: April 10, 2019, 06:28:41 am by engkin »

WooBean

  • Full Member
  • ***
  • Posts: 229
Re: declaring operator inc crashed the compiler
« Reply #1 on: April 09, 2019, 08:23:00 am »
Hi,
just a clue:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. Type
  6.   TAZ=String;
  7.  
  8. //operator inc(az: TAZ; i: integer=1) raz:TAZ; inline; // COMPILER (FPC 3.0.4/win32) ERROR
  9. //WHERE SOMEBODY ALLOWED THAT SYNTAX FOR DECLARING operator?
  10. //operator inc(az: TAZ; i: integer) raz:TAZ; inline;
  11. //begin
  12. //end;
  13.  
  14. function inc(az: TAZ; i: integer=1):TAZ; inline; //very BAD naming idea, but it compiles anyway
  15. begin
  16.   //if called as inc(az) it uses default parameter i=1
  17. end;
  18.  
  19.  
  20. begin
  21. end.
  22.      
  23.  
Platforms: Win7/64, Linux Mint Ulyssa/64

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: declaring operator inc crashed the compiler
« Reply #2 on: April 09, 2019, 08:36:51 am »
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

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: declaring operator inc crashed the compiler
« Reply #3 on: April 09, 2019, 08:42:40 am »
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.
« Last Edit: April 09, 2019, 09:23:55 am by Thaddy »
Specialize a type, not a var.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: declaring operator inc crashed the compiler
« Reply #4 on: April 09, 2019, 09:16:04 am »
Also confirmed with FPC 3.0.4 on Linux x64.
keep it simple

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: declaring operator inc crashed the compiler
« Reply #5 on: April 09, 2019, 09:29:24 am »
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][+][-]
  1. {$mode objfpc}{$H+}
  2. Type
  3.   TAZ=String;
  4. procedure inc(var az:TAZ);inline;
  5. begin
  6. end;
  7.  
  8. procedure inc(var az: TAZ; i: integer); inline;
  9. begin
  10. end;
  11.  
  12. begin
  13. end.

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.
« Last Edit: April 09, 2019, 09:43:56 am by Thaddy »
Specialize a type, not a var.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: declaring operator inc crashed the compiler
« Reply #6 on: April 09, 2019, 09:51:14 am »
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

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: declaring operator inc crashed the compiler
« Reply #7 on: April 09, 2019, 09:59:35 am »
Howard, it is a bit more complex and there are two issue that I am reporting.
[edit]
mantis 31347 and 31348
« Last Edit: April 09, 2019, 01:11:54 pm by Thaddy »
Specialize a type, not a var.

Akira1364

  • Hero Member
  • *****
  • Posts: 561
Re: declaring operator inc crashed the compiler
« Reply #8 on: April 09, 2019, 04:58:07 pm »
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.pas#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][+][-]
  1. program Example;
  2.  
  3. {$mode ObjFPC}{$H+}
  4. {$coperators On}
  5.  
  6. uses SysUtils;
  7.  
  8. type TAZ = String;
  9.  
  10. // Overloads for Inc can only be declared with both the parameter type
  11. // and the return type being all the the same.
  12. // Note that yes, this "looks" like a function, but it still only works like a procedure.
  13. operator Inc(const AZ: TAZ): TAZ; inline;
  14. begin
  15.   Result := AZ + AZ;
  16. end;
  17.  
  18. // This is also valid.
  19. // Again, while it looks like a function, it works like a procedure.
  20. operator Inc(const A, B: TAZ): TAZ; inline;
  21. begin
  22.   Result := A + B;
  23. end;
  24.  
  25. // You can implement what you want with the + operator, though.
  26. operator +(const AZ: TAZ; const I: Integer): TAZ; inline;
  27. begin
  28.   Result := AZ + IntToStr(I);
  29. end;
  30.  
  31. var S: TAZ = 'Hello!';
  32.  
  33. begin
  34.   Inc(S);
  35.  
  36.   // prints 'Hello!Hello!'
  37.   WriteLn(S);
  38.  
  39.   Inc(S, S);
  40.  
  41.   // prints 'Hello!Hello!Hello!Hello!'
  42.   WriteLn(S);
  43.  
  44.   // += is not a different operator in FPC, just a combination of + and :=, so the next line works.
  45.   S += 55;
  46.  
  47.   // prints 'Hello!Hello!Hello!Hello!55'
  48.   WriteLn(S);
  49. end.
« Last Edit: April 09, 2019, 10:22:45 pm by Akira1364 »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: declaring operator inc crashed the compiler
« Reply #9 on: April 09, 2019, 05:55:05 pm »
Indeed
Not too fine a point on it but did you read the reaction on the bug report__
« Last Edit: April 09, 2019, 05:58:02 pm by Thaddy »
Specialize a type, not a var.

Akira1364

  • Hero Member
  • *****
  • Posts: 561
Re: declaring operator inc crashed the compiler
« Reply #10 on: April 09, 2019, 06:36:17 pm »
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.)
« Last Edit: April 09, 2019, 06:54:14 pm by Akira1364 »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: declaring operator inc crashed the compiler
« Reply #11 on: April 10, 2019, 06:25:36 am »
@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][+][-]
  1.   inc(sAZ1);
Quote
Project1.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

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: declaring operator inc crashed the compiler
« Reply #12 on: April 10, 2019, 08:02:15 am »
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:

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).
keep it simple

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: declaring operator inc crashed the compiler
« Reply #13 on: April 10, 2019, 08:42:37 am »
Michael adapted the documentation. The crash is still open.
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: declaring operator inc crashed the compiler
« Reply #14 on: April 10, 2019, 09:14:25 am »
By the way, in Delphi Round and Trunc also operators.

 

TinyPortal © 2005-2018