Operator overloading: a LEFT literal string + a RIGHT literal number fails with Error: Incompatible types. Which is not correct.

Original Reporter info from Mantis: Thaddy
  • Reporter name: Thaddy de Koning

Description:

construct.pas(26,23) Error: Incompatible types: got "LongInt" expected "ShortString"

Note all other string types as well: both Ansi and Unicode.
Strange thing is that all other combinations pass all tests.
Below is a test program that I have tested with on WIN-x86_64 and Rasbian.

I could reduce it to just the one case:Literal string left, variable integer right.

Steps to reproduce:

{MODE OBJFPC}<br/> {IFDEF MSWINDOWS}{APPTYPE CONSOLE}{ENDIF}

operator + (Left: LongInt; const Right: string): string;
begin
  writestr(Result,Left,Right);
end;
  
operator + (const Left: string; Right: LongInt): string;
begin
  writestr(Result,Left,Right);
end;
  
// V = variable L is literal
// S = string I is integer  
var
  Text: string = 'text';
  Number: LongInt = 123456;
begin
  Writeln(Text + Number); // PASS: VS+VI
  Writeln(Number + Text); // PASS: VI+VS
  Writeln(789012 + Text); // PASS: LI+VS
  Writeln(Text+789012);   // PASS: VS+LI
  //---- problem here...
  Writeln('else text'+Number);// FAIL LS+VI : fails only on LEFT string literals + non-literal number with all string types
  //---- end of problem
  Writeln(Number + 'other text'); //PASS: VI+LS
  Writeln(string('else text')+Number); //PASS: CAST(LS)+VI
  writeln(789012 + 'other text'); // PASS: LI+LS
  writeln('other text',789012); // PASS: LS+LI
  Readln;
end.

Additional information:

Based on discussion here: http://forum.lazarus.freepascal.org/index.php/topic,44713.0.html

Mantis conversion info:

  • Mantis ID: 35248
  • OS: all
  • OS Build: all
  • Build: 41736
  • Platform: all
  • Version: 3.3.1