* * *

Author Topic: Difference between += and := -- Is this a bug in FPC?  (Read 3563 times)

EganSolo

  • Full Member
  • ***
  • Posts: 135
Difference between += and := -- Is this a bug in FPC?
« on: October 18, 2017, 02:33:05 am »
Before I get ahead of myself and post this as a bug, I thought I would ask here first.
Consider line 8 in the code fragment below: The code uses +=, whereas the commented assignment on line nine  uses :=
When I run the code as is (with +=) and the boolean variable, EmptyStr on line seven is true, the debugger exits the method as if the code on lines eight and ten does not exist. But when I comment that line and uncomment line nine, then the code functions properly.

Am I simply misusing +=? I mean CurrentString is a variable passed to method where this code is implemented and MIF_EmtyCharStyleMarker is a constant declared in another unit.
 
Code: Pascal  [Select]
  1.           Case WordHTMLTag of
  2.             wht_Italics : CurrentString += MIF_ItalicMarker;
  3.             wht_Bold    : CurrentString += MIF_BoldMarker  ;
  4.             wht_span    : If IsOpeningTag
  5.                           then begin
  6.                             EmptyStr := HtmlClass = '';
  7.                             If EmptyStr then
  8.                                CurrentString += MIF_EmptyCharStyleMarker
  9.                            // CurrentString := CurrentString + MIF_EmptyCharStyleMarker
  10.                             else MIF_AddStartCharaTag(HTMLClass)
  11.                           end
  12.                           else If MIF_HasIllegalMarker(CurrentString)
  13.                                then CurrentString += MIF_EmptyCharStyleMarker
  14.                                else MIF_AddEndCharaTag;
  15.             else Assert(False, 'we should not bere here!');
  16.            end;
  17.  

Blaazen

  • Hero Member
  • *****
  • Posts: 2510
  • POKE 54296,15
    • Eye-Candy Controls
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #1 on: October 18, 2017, 03:22:33 am »
Code: Pascal  [Select]
  1.     CurrentString += MIF_EmptyCharStyleMarker
  2.     CurrentString := CurrentString + MIF_EmptyCharStyleMarker
These two lines are identical. There must be something wrong elsewhere out of pasted code.
Lazarus 1.9.0 r57500 FPC 3.0.4 x86_64-linux-qt Chakra, Qt 4.8.7, Plasma 5.11.4
Lazarus 1.8.0 r56594 FPC 3.0.4 i386-win32-win32/win64 Wine 3.2

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Munair

  • Sr. Member
  • ****
  • Posts: 316
  • Keep it simple.
    • Ditrianum
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #2 on: October 18, 2017, 04:19:14 am »
You are not misusing += but it is C-style. I never use that style, but from the manual I find that you must use the compiler directive {$COPERATORS ON} to support it.

With a similar example, without the directive, I get a compile error "Variable identifier expected". Interestingly, WITH the directive I also get the same compile error. See the screenshot.

If I replace += with normal fp syntax, everything compiles just fine. My suggestion: stick to native fp syntax. Even though it may look more verbose, the resulting code is the same.

Example:
Code: Pascal  [Select]
  1. unit upcstyle;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$COPERATORS ON}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Label1: TLabel;
  17.     procedure FormCreate(Sender: TObject);
  18.   private
  19.     { private declarations }
  20.   public
  21.     { public declarations }
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. var
  35.   EmptyStr: boolean;
  36.   HtmlClass: string;
  37. begin
  38.   EmptyStr := (HtmlClass = '');
  39.   if EmptyStr then
  40.     Label1.Caption += 'empty' //doesn't work
  41.     //Label1.Caption := Label1.Caption + 'empty' //works
  42.   else
  43.     Label1.Caption := 'not empty';
  44. end;
  45.  
  46. end.
  47.  

Steps to reproduce: Start a new project. Add a label to the form. Replace the code in unit1 with the example code.

EDIT: I just learned that {$mode objfpc} already gives support to C style operators. It explains why the compiler keeps complaining. In the example above {$COPERATORS ON} doesn't make any difference.
« Last Edit: October 18, 2017, 09:54:03 am by Munair »
Lazarus 1.6.2, testing 1.8, FPC 3.0.0; Debian 9 KDE 5.8.6 x64; Windows 7 x64, PC-DOS2000

engkin

  • Hero Member
  • *****
  • Posts: 1952
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #3 on: October 18, 2017, 05:15:17 am »
Before I get ahead of myself and post this as a bug, I thought I would ask here first.
Consider line 8 in the code fragment below: The code uses +=, whereas the commented assignment on line nine  uses :=
When I run the code as is (with +=) and the boolean variable, EmptyStr on line seven is true, the debugger exits the method as if the code on lines eight and ten does not exist. But when I comment that line and uncomment line nine, then the code functions properly.

Am I simply misusing +=? I mean CurrentString is a variable passed to method where this code is implemented and MIF_EmtyCharStyleMarker is a constant declared in another unit.
 
Code: Pascal  [Select]
  1.           Case WordHTMLTag of
  2.             wht_Italics : CurrentString += MIF_ItalicMarker;
  3.             wht_Bold    : CurrentString += MIF_BoldMarker  ;
  4.             wht_span    : If IsOpeningTag
  5.                           then begin
  6.                             EmptyStr := HtmlClass = '';
  7.                             If EmptyStr then
  8.                                CurrentString += MIF_EmptyCharStyleMarker
  9.                            // CurrentString := CurrentString + MIF_EmptyCharStyleMarker
  10.                             else MIF_AddStartCharaTag(HTMLClass)
  11.                           end
  12.                           else If MIF_HasIllegalMarker(CurrentString)
  13.                                then CurrentString += MIF_EmptyCharStyleMarker
  14.                                else MIF_AddEndCharaTag;
  15.             else Assert(False, 'we should not bere here!');
  16.            end;
  17.  

Your usage is correct.
The generated code is also correct.
There is a bug in the debug information, confirmed using -al in the compiler options

When += is used here is the generated code:
Quote
.Ll28:
# [46] If EmptyStr then
   cmpb   $0,-20(%ebp)
   je   .Lj58
.Ll29:
# [49] else
   pushl   $0
   movl   8(%ebp),%eax
   movl   (%eax),%edx
   movl   8(%ebp),%eax
   movl   TC_$P$PROJECT1_$$_MIF_EMPTYCHARSTYLEMARKER,%ecx
   call   fpc_ansistr_concat
   jmp   .Lj32

While with CurrentString := CurrentString + MIF_EmptyCharStyleMarker
Quote
.Ll28:
# [46] If EmptyStr then
   cmpb   $0,-20(%ebp)
   je   .Lj58
.Ll29:
# [48] CurrentString := CurrentString + MIF_EmptyCharStyleMarker
   pushl   $0
   movl   8(%ebp),%eax
   movl   (%eax),%edx
   movl   8(%ebp),%eax
   movl   TC_$P$PROJECT1_$$_MIF_EMPTYCHARSTYLEMARKER,%ecx
   call   fpc_ansistr_concat
   jmp   .Lj32

Notice that the assembly is identical. Just the debug information is wrong.

Munair

  • Sr. Member
  • ****
  • Posts: 316
  • Keep it simple.
    • Ditrianum
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #4 on: October 18, 2017, 05:23:33 am »
Does that mean that the directive {$COPERATORS ON} no longer makes a difference? My example suggests it is not just the debugger.
« Last Edit: October 18, 2017, 05:28:16 am by Munair »
Lazarus 1.6.2, testing 1.8, FPC 3.0.0; Debian 9 KDE 5.8.6 x64; Windows 7 x64, PC-DOS2000

EganSolo

  • Full Member
  • ***
  • Posts: 135
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #5 on: October 18, 2017, 06:53:41 am »
Thank you, everyone, for your thoughtful responses. This was extremely helpful and enlightening. I did not think to use the -al option, now I know better.

So, should I report this as a bug?
 

Thaddy

  • Hero Member
  • *****
  • Posts: 5796
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #6 on: October 18, 2017, 09:22:07 am »
Does that mean that the directive {$COPERATORS ON} no longer makes a difference? My example suggests it is not just the debugger.
Depends on mode: in objfpc mode it is on by default. In mode delphi it is off by default. So {$COPERATORS ON} is basically just required if you want to use it in other modes than {$mode objfpc}
« Last Edit: October 18, 2017, 09:24:00 am by Thaddy »
recommends {$macro on}{$define Silly:=ObjFpc}

Munair

  • Sr. Member
  • ****
  • Posts: 316
  • Keep it simple.
    • Ditrianum
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #7 on: October 18, 2017, 09:45:28 am »
Does that mean that the directive {$COPERATORS ON} no longer makes a difference? My example suggests it is not just the debugger.
Depends on mode: in objfpc mode it is on by default. In mode delphi it is off by default. So {$COPERATORS ON} is basically just required if you want to use it in other modes than {$mode objfpc}
Then my example above should be considered a bug. I never use c-style operators. This is the first issue I run into taking the TS problem.
Lazarus 1.6.2, testing 1.8, FPC 3.0.0; Debian 9 KDE 5.8.6 x64; Windows 7 x64, PC-DOS2000

Thaddy

  • Hero Member
  • *****
  • Posts: 5796
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #8 on: October 18, 2017, 09:47:56 am »
I don't see the bug?
recommends {$macro on}{$define Silly:=ObjFpc}

Munair

  • Sr. Member
  • ****
  • Posts: 316
  • Keep it simple.
    • Ditrianum
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #9 on: October 18, 2017, 09:49:43 am »
I don't see the bug?
See my first comment with screenshot. It doesn't compile here.

Clearly the compiler expects something else from line 40 to 41. It doesn't seem to support the += construction followed by the ELSE statement.
« Last Edit: October 18, 2017, 10:03:13 am by Munair »
Lazarus 1.6.2, testing 1.8, FPC 3.0.0; Debian 9 KDE 5.8.6 x64; Windows 7 x64, PC-DOS2000

Thaddy

  • Hero Member
  • *****
  • Posts: 5796
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #10 on: October 18, 2017, 10:23:52 am »
The C style operators need to be fully evaluated first. It is C style, and certainly not complete (e.g. use =+ etc are not implemented)
recommends {$macro on}{$define Silly:=ObjFpc}

Munair

  • Sr. Member
  • ****
  • Posts: 316
  • Keep it simple.
    • Ditrianum
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #11 on: October 18, 2017, 10:25:49 am »
The C style operators need to be fully evaluated first. It is C style, and certainly not complete (e.g. use =+ etc are not implemented)
Which is why I suggested to stick to FP syntax.  ;)
« Last Edit: October 18, 2017, 10:31:04 am by Munair »
Lazarus 1.6.2, testing 1.8, FPC 3.0.0; Debian 9 KDE 5.8.6 x64; Windows 7 x64, PC-DOS2000

Leledumbo

  • Hero Member
  • *****
  • Posts: 7848
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #12 on: October 18, 2017, 11:02:57 am »
In this case, the two should be identical. Can you make a super simple example that still reproduce this issue so that we can confirm?

Munair

  • Sr. Member
  • ****
  • Posts: 316
  • Keep it simple.
    • Ditrianum
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #13 on: October 18, 2017, 11:28:46 am »
In this case, the two should be identical. Can you make a super simple example that still reproduce this issue so that we can confirm?

Even if I take out the ELSE condition, the compiler still complains:
Code: Pascal  [Select]
  1. if EmptyStr then
  2.   Label1.Caption += 'empty'; //complaint

The compiler also complains without IF condition:
Code: Pascal  [Select]
  1. Label1.Caption += ''; //complaint

However, the compiler does not complain when using simple variables:
Code: Pascal  [Select]
  1. var a: string;
  2. a += ''; //no complaint
  3. Label1.Caption += ''; //complaint

It looks like it has to do with the combination of object referencing.
« Last Edit: October 18, 2017, 11:44:34 am by Munair »
Lazarus 1.6.2, testing 1.8, FPC 3.0.0; Debian 9 KDE 5.8.6 x64; Windows 7 x64, PC-DOS2000

Blaazen

  • Hero Member
  • *****
  • Posts: 2510
  • POKE 54296,15
    • Eye-Candy Controls
Re: Difference between += and := -- Is this a bug in FPC?
« Reply #14 on: October 18, 2017, 02:16:56 pm »
@ It looks like it has to do with the combination of object referencing.

No. It has to do with properties. Operator += needs variable. Setters and getters have no address.
It seems that CurrentString from the original post is a property.
Lazarus 1.9.0 r57500 FPC 3.0.4 x86_64-linux-qt Chakra, Qt 4.8.7, Plasma 5.11.4
Lazarus 1.8.0 r56594 FPC 3.0.4 i386-win32-win32/win64 Wine 3.2

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus