Recent

Author Topic: Delphi mode generic function in expression - compile errors  (Read 864 times)

alpine

  • Hero Member
  • *****
  • Posts: 1064
Delphi mode generic function in expression - compile errors
« on: August 27, 2022, 10:48:45 am »
I wrote a generic compare function for sorting and when I've decided to invert the sign the compiler gave an error.

Here is a code illustrating the problem:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$MODE Delphi}
  3.  
  4. function Fun<T>(X: T): Integer;
  5. begin
  6. end;
  7.  
  8. var
  9.   I: Integer;
  10. begin
  11.   I := Fun<Integer>(1);
  12.   I := Fun<Integer>(1) * -1; // Fatal: Syntax error, ";" expected but "*" found
  13.   I := -1 * Fun<Integer>(1); // Error: Internal error 2015071704
  14. end.

Why is this happening?

Lazarus 2.2.2 (rev lazarus_2_2_2) FPC 3.2.2 i386-win32-win32/win64
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Delphi mode generic function in expression - compile errors
« Reply #1 on: August 27, 2022, 11:10:19 am »
I can make both disappear, but note that an internal error should always be reported on the bug tracker.
I suppose a fix for the internal error will also fix the first one. They are somewhat related as below shows.

Work Around code :
Code: Pascal  [Select][+][-]
  1. {$MODE Delphi}
  2. function Fun<T>(X: T): Integer;
  3. begin
  4.   Result :=1; // just in case the compiler can not work without a result value... Both errors still appear, though
  5. end;
  6.  
  7. var
  8.   I: Integer;
  9. begin
  10.   I := Fun<Integer>(1);
  11.   I := (Fun<Integer>(1)) * -1; // Makes disappear Fatal: Syntax error, ";" expected but "*" found
  12.   I := -1 * (Fun<Integer>(1)); // Makes disappear Error: Internal error 2015071704
  13. end.


« Last Edit: August 27, 2022, 11:18:30 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Delphi mode generic function in expression - compile errors
« Reply #2 on: August 27, 2022, 11:22:43 am »
Yes, that is the first thing to try that comes into mind. Also I found this: https://forum.lazarus.freepascal.org/index.php/topic,55039.msg409402.html#msg409402 with a similar example. But this one is a pretty simpler for an internal error to happen.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Delphi mode generic function in expression - compile errors
« Reply #3 on: August 27, 2022, 11:52:53 am »
It seems that multiplication by a const is evaluated by the compiler before the specialization is complete.
If that is the case, the compiler cannot know if a multiplication is even possible with generics.
IOW the specialization comes too late in mode delphi.
But indeed, although the subject you link to isn't quite the same, using mode objfpc works:
Code: Pascal  [Select][+][-]
  1. {$MODE objfpc}
  2. generic function Fun<T>(X: T): Integer;
  3. begin
  4.   Result :=1;
  5. end;
  6.  
  7. var
  8.   I: Integer;
  9. begin
  10.   I := specialize Fun<Integer>(1);
  11.   I := specialize Fun<Integer>(1) * -1;
  12.   I := -1 * specialize Fun<Integer>(1);
  13. end.
I know you knew that, but just to clarify for other readers.
« Last Edit: August 27, 2022, 12:00:31 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Delphi mode generic function in expression - compile errors
« Reply #4 on: August 27, 2022, 12:12:57 pm »
Btw, you can factor out the objfpc part and the specialization into a separate unit, so the rest of the program can still use the delphi way.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Delphi mode generic function in expression - compile errors
« Reply #5 on: August 28, 2022, 06:14:08 pm »
Issue created: https://gitlab.com/freepascal.org/fpc/source/-/issues/39881 thanks to Serge Anvarov who managed to do it over the weekend for me
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Delphi mode generic function in expression - compile errors
« Reply #6 on: August 28, 2022, 06:17:52 pm »
I wrote a generic compare function for sorting and when I've decided to invert the sign the compiler gave an error.

Here is a code illustrating the problem:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$MODE Delphi}
  3.  
  4. function Fun<T>(X: T): Integer;
  5. begin
  6. end;
  7.  
  8. var
  9.   I: Integer;
  10. begin
  11.   I := Fun<Integer>(1);
  12.   I := Fun<Integer>(1) * -1; // Fatal: Syntax error, ";" expected but "*" found
  13.   I := -1 * Fun<Integer>(1); // Error: Internal error 2015071704
  14. end.

Why is this happening?

The compiler simply does not support more complex expressions involving inline specializations in mode Delphi and it will be a long time till all those issues will be fixed. If you want to use such then use mode ObjFPC.

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Delphi mode generic function in expression - compile errors
« Reply #7 on: August 28, 2022, 06:29:12 pm »
*snip*
The compiler simply does not support more complex expressions involving inline specializations in mode Delphi and it will be a long time till all those issues will be fixed. If you want to use such then use mode ObjFPC.
I will. To be fair, I'm fine with the parenthesis as well. It was an improvement over old Delphi converted unit and I felt it will be good to share it.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018