[Mono-bugs] [Bug 82034][Nor] New - goto does not call the finally block to close the iterator when leaving a foreach loop

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Sat Jul 7 13:21:37 EDT 2007


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 jeff at thefirst.org.

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

--- shadow/82034	2007-07-07 13:21:37.000000000 -0400
+++ shadow/82034.tmp.4264	2007-07-07 13:21:37.000000000 -0400
@@ -0,0 +1,86 @@
+Bug#: 82034
+Product: Mono: Compilers
+Version: 1.2
+OS: 
+OS Details: XP
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: C#
+AssignedTo: rharinath at novell.com                            
+ReportedBy: jeff at thefirst.org               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: goto does not call the finally block to close the iterator when leaving a foreach loop
+
+Please fill in this template when reporting a bug, unless you know what you
+are doing.
+Description of Problem:
+Usually, in an iterator, we can put yield inside a try/finally block and
+when control leaves the foreach loop for the iterator (because of "break"
+or an exception) it will call the iterator's finally block. But if we leave
+the foreach loop using goto, it doesn't do this. But we use "goto" to
+transfer control out of one or more nested loops, and it should behave the
+same as "break".
+
+Steps to reproduce the problem:
+1. Compile and run this code:
+using System.Collections.Generic;
+
+class TestGoto
+{
+    static int x = 0;
+
+    static void Main(string[] args)
+    {
+        foreach (bool b in test())
+        {
+        }
+    }
+
+    static IEnumerable<bool> setX()
+    {
+        x = 1;
+        try
+        {
+            yield return true;
+        }
+        finally
+        {
+            x = 0;
+        }
+    }
+
+    static IEnumerable<bool> test()
+    {
+        foreach (bool b in setX())
+        {
+            yield return true;
+            // Change "goto label" to "break" to show the correct result.
+            goto label;
+        }
+    label:
+        if (x == 0)
+            System.Console.WriteLine("Success: finally was called");
+        else
+            System.Console.WriteLine("Error: finally was not called");
+    }
+}
+
+
+Actual Results:
+prints "Error: finally was not called" because the setX() iterator is not
+closed when leaving the foreach loop.
+
+Expected Results:
+If you change "goto label" to "break", then the iterator is closed properly
+and it prints "Success: finally was called". But a "goto" should act like
+"break" and also close the iterator.
+
+How often does this happen? 
+Every time
+
+Additional Information:


More information about the mono-bugs mailing list