Browse Source

* propertly check equality with the for-counter when propagating constants, resolves #39915

florian 2 years ago
parent
commit
444fe092da
2 changed files with 17 additions and 1 deletions
  1. 1 1
      compiler/optconstprop.pas
  2. 16 0
      tests/webtbs/tw39915.pp

+ 1 - 1
compiler/optconstprop.pas

@@ -142,7 +142,7 @@ unit optconstprop;
                   begin
                     CalcDefSum(tfornode(n).t2);
                     { the constant can propagete if is is not the counter variable ... }
-                    if not(tassignmentnode(arg).left.isequal(tfornode(n).left)) and
+                    if not(tassignmentnode(arg).left.isequal(actualtargetnode(@tfornode(n).left)^)) and
                     { if it is a temprefn or its address is not taken in case of loadn }
                       ((tassignmentnode(arg).left.nodetype=temprefn) or not(tabstractvarsym(tloadnode(tassignmentnode(arg).left).symtableentry).addr_taken)) and
                       { and no definition in the loop? }

+ 16 - 0
tests/webtbs/tw39915.pp

@@ -0,0 +1,16 @@
+{ %OPT=-gt -O3 }
+program project1;
+
+{$mode objfpc}
+
+function CharPos(const S: AnsiString; const C: AnsiChar; const Index: SizeInt): SizeInt;
+begin
+  for Result := Index to Length(S) do
+    if S[Result] = C then
+      Exit;
+  Result := 0;
+end;
+
+begin
+  Writeln(CharPos('Hello!', '!', 1));
+end.