[Mono-bugs] [Bug 46222][Nor] New - Iterator (yield statement) implementation wrong

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Sun, 13 Jul 2003 15:40:32 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by sestoft@dina.kvl.dk.

http://bugzilla.ximian.com/show_bug.cgi?id=46222

--- shadow/46222	Sun Jul 13 15:40:32 2003
+++ shadow/46222.tmp.19879	Sun Jul 13 15:40:32 2003
@@ -0,0 +1,91 @@
+Bug#: 46222
+Product: Mono/MCS
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: Misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: sestoft@dina.kvl.dk               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Iterator (yield statement) implementation wrong
+
+Description of Problem:
+
+The implementation of the yield statement in C# 2 iterators refers to wrong
+field of proxy class or enclosing class and therefore produces wrong
+results.  Apparently references to the b field from the enclosing class Seq
+are somehow confused with the proxy-local field i, although I cannot see
+from the disassembled bytecode exactly what goes wrong.  If the iterator
+uses only three (not four) fields from the enclosing class, everything is ok. 
+
+Steps to reproduce the problem:
+1. Compile and run this program with mcs -v2:
+
+using System;
+using System.Collections;
+
+class Seq : IEnumerable {
+  private readonly int m, n, k, b; // Represents the sequence b + k * [m..n]
+
+  public Seq(int m, int n, int k, int b) {
+    this.m = m; this.n = n; this.k = k; this.b = b;
+  }
+
+  public IEnumerator GetEnumerator() {
+    for (int i=m; i<=n; i++) {
+      Console.WriteLine("m={0} n={1} k={2} b={3} i={4}", m, n, k, b, i);
+      yield b + k * i;
+    }
+  }
+}
+
+class MyTest {
+  public static void Main(String[] args) {
+    Seq seq = new Seq(0, 4, 1, 0);
+    foreach (int i in seq)
+      Console.WriteLine(i);
+  }
+}
+
+
+Actual Results:
+
+m=0 n=4 k=1 b=0 i=0
+0
+m=0 n=4 k=1 b=1 i=1
+2
+m=0 n=4 k=1 b=2 i=2
+4
+m=0 n=4 k=1 b=3 i=3
+6
+m=0 n=4 k=1 b=4 i=4
+8
+
+
+Expected Results:
+
+m=0 n=4 k=1 b=1 i=0
+0
+m=0 n=4 k=1 b=1 i=1
+1
+m=0 n=4 k=1 b=1 i=2
+2
+m=0 n=4 k=1 b=1 i=3
+3
+m=0 n=4 k=1 b=1 i=4
+4
+
+
+How often does this happen? 
+
+Always on mono 0.25.
+
+
+Additional Information: