From 815734c47a4afe7d07083757f9ed6df233762abb Mon Sep 17 00:00:00 2001 From: Sven/Sarah Barth Date: Fri, 8 Apr 2022 17:40:36 +0200 Subject: [PATCH] * 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 --- compiler/nflw.pas | 2 ++ tests/webtbs/tw32034.pp | 14 ++++++++++++++ tests/webtbs/tw39656.pp | 24 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 tests/webtbs/tw32034.pp create mode 100644 tests/webtbs/tw39656.pp diff --git a/compiler/nflw.pas b/compiler/nflw.pas index 1b654943ed..4a481696af 100644 --- a/compiler/nflw.pas +++ b/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 diff --git a/tests/webtbs/tw32034.pp b/tests/webtbs/tw32034.pp new file mode 100644 index 0000000000..8285461014 --- /dev/null +++ b/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. + diff --git a/tests/webtbs/tw39656.pp b/tests/webtbs/tw39656.pp new file mode 100644 index 0000000000..5bb54aa20a --- /dev/null +++ b/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. +