Skip to content

Commit

Permalink
fix #807
Browse files Browse the repository at this point in the history
  • Loading branch information
ibond84 committed Aug 8, 2018
1 parent 2208af5 commit d3a461f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion NETGenerator/NETGenerator.cs
Expand Up @@ -10300,7 +10300,7 @@ public override void visit(IForeachNode value)
Type return_type = null;
bool is_generic = false;
MethodInfo enumer_mi = null; //typeof(System.Collections.IEnumerable).GetMethod("GetEnumerator", Type.EmptyTypes);
if (var_tp.IsValueType && !(in_what_type.IsArray && in_what_type.GetArrayRank() > 1))
if (/*var_tp.IsValueType &&*/ !var_tp.IsGenericParameter && !(in_what_type.IsArray && in_what_type.GetArrayRank() > 1))
{
enumer_mi = helper.GetEnumeratorMethod(in_what_type);
if (enumer_mi == null)
Expand Down
6 changes: 3 additions & 3 deletions TestSuite/foreach1.pas
@@ -1,4 +1,4 @@
type TClass = class(System.Collections.IEnumerable)
type TClass = class(System.Collections.IEnumerable)
public function GetEnumerator : System.Collections.IEnumerator;
begin
Result := nil;
Expand All @@ -24,15 +24,15 @@ type TClass = class(System.Collections.IEnumerable)
end;
i := 0;
arr[1] := 1; arr[2] := 2; arr[3] := 3;
{foreach v : integer in arr do
foreach v : integer in arr do
begin
case i of
0 : assert(v=1);
1 : assert(v=2);
2 : assert(v=3);
end;
Inc(i);
end;}
end;
i := 0;
SetLength(arr2,2);
arr2[0] := 3.14; arr2[1] := 2.71;
Expand Down
7 changes: 6 additions & 1 deletion TestSuite/foreach3.pas
@@ -1,4 +1,4 @@
procedure WriteArray<T>(a: array of T);
procedure WriteArray<T>(a: array of T);
begin
foreach x: T in a do
writeln(x);
Expand All @@ -8,8 +8,13 @@ procedure WriteArray<T>(a: array of T);
var a := Arr(2,3,5);
WriteArray(a);
var a2 := new integer[2,2]((3,3),(3,3));
var i := 0;
foreach x: integer in a2 do
begin
assert(x = 3);
Inc(i);
end;
assert(i = 4);
var a3 := new integer[2,2]((3,3),(3,3));
foreach var x in a3 do
assert(x=3);
Expand Down
40 changes: 40 additions & 0 deletions TestSuite/foreach5.pas
@@ -0,0 +1,40 @@
var i: integer;

type
e1 = class(System.Collections.IEnumerator)
public function MoveNext := false;
public function get_current:object := nil;
public procedure Reset := exit;
end;
e2<T> = class(System.Collections.Generic.IEnumerator<T>)
public function MoveNext := false;
public function System.Collections.IEnumerator.get_current:object := nil;
public function get_current:T := default(T);
public procedure Reset := exit;
public procedure Dispose := exit;
end;

t2=class
b:byte;
end;
t1<T> = class(System.Collections.Generic.IEnumerable<T>)

public function System.Collections.IEnumerable.GetEnumerator: System.Collections.IEnumerator;
begin
i := 1;
Result := new e1;
end;

public function GetEnumerator: System.Collections.Generic.IEnumerator<T>;
begin
i := 2;
Result := new e2<T>;
end;

end;

begin
foreach var o in new t1<t2> do
o.b := 2;
assert(i = 2);
end.
12 changes: 12 additions & 0 deletions TestSuite/foreach6.pas
@@ -0,0 +1,12 @@
function test<T>(a: T): sequence of T;
begin
var lst: List<List<T>> := new List<List<T>>;
lst.Add(new List<T>(Arr(a, a)));
foreach var x in lst do
begin
yield x[0];
end;
end;
begin
assert(test(2).ToArray[0] = 2);
end.

0 comments on commit d3a461f

Please sign in to comment.