Преглед на файлове

* fix #32034 and fix #39656: in a for-in-loop with an array constructor enforce the type of the loop variable for the elements
+ added tests

Sven/Sarah Barth преди 3 години
родител
ревизия
815734c47a
променени са 3 файла, в които са добавени 40 реда и са изтрити 0 реда
  1. 2 0
      compiler/nflw.pas
  2. 14 0
      tests/webtbs/tw32034.pp
  3. 24 0
      tests/webtbs/tw39656.pp

+ 2 - 0
compiler/nflw.pas

@@ -937,6 +937,8 @@ implementation
               end
             else
               begin
+                if is_array_constructor(expr.resultdef) then
+                  tarrayconstructornode(expr).force_type(hloopvar.resultdef);
                 // search for operator first
                 pd:=search_enumerator_operator(expr.resultdef, hloopvar.resultdef);
                 // if there is no operator then search for class/object enumerator method

+ 14 - 0
tests/webtbs/tw32034.pp

@@ -0,0 +1,14 @@
+program tw32034;
+
+{$H-}
+
+var
+  s: String;
+begin
+  for s in (['1char','2chars']) do begin
+    WriteLn(s);
+    if (s <> '1char') and (s <> '2chars') then
+      Halt(1);
+  end;
+end.
+

+ 24 - 0
tests/webtbs/tw39656.pp

@@ -0,0 +1,24 @@
+program tw39656;
+
+uses
+  sysutils,math;
+var
+  r: double;
+  ar: array of double = (0.001, 0.5, 0.7, 0.999);
+  idx: LongInt;
+begin
+  {Write('good:');
+  for r in ar do Write(FloatToStr(r), ' ');
+  Writeln;}
+
+  //Write('bad:');
+  idx:=0;
+  for r in [0.001, 0.5, 0.7, 0.999] do begin
+    //Write(FloatToStr(r), ' ');
+    if not SameValue(r,ar[idx]) then
+      Halt(idx+1);
+    Inc(idx);
+  end;
+  //Writeln;
+end.
+