[Mono-dev] Compiler bug in foreach

Mehrdad Reshadi reshadi at gmail.com
Fri May 28 19:44:34 EDT 2010


I found the following bug, wondering if others have seen this, or anything
is being done to solve it. "foreach" is too common to have such a bug!

Description of Problem:
the foreach code not working correclty.
I tried this code with VisualStudio 2010, Monodevelope, mcs & gmcs (2.4.4
and 2.6.1). In call cases, the code generated by VS2010 runs correctly on
both .NET and mono runtime, and the code generatd by mono C# compiler is
wrong on both .NET and mono runtime. So, definitly a compiler problem.

Steps to reproduce the problem:
1. Runn the folloing code.

Actual Results:
-In A for Y
-In A for Y
-In A for X
In B for X
In C for Y
 -In A for Y

Expected Results:
-In A for Y
-In A for Y
-In A for Y
In C for Y
-In A for Y

How often does this happen?
Always

Additional Information:

Code Sample:
using System;

namespace MonoVirtuals
{
    class X { }
    class Y : X { }

    class A
    {
        public virtual void f(X o) { System.Console.WriteLine("-In A for
X"); }
        public virtual void f(Y o) { System.Console.WriteLine("-In A for
Y"); }
    }
    class B : A
    {
        public override void f(X o) { base.f(o);
System.Console.WriteLine("In B for X"); }
    }
    class C : B
    {
        public override void f(X o) { base.f(o);
System.Console.WriteLine("In C for X"); }
        public override void f(Y o) { base.f(o);
System.Console.WriteLine("In C for Y"); }
    }
    class MainClass
    {
        public static void Main(string[] args)
        {
            var o = new Y();
            foreach (var c in new A[3] { new A(), new B(), new C() })
                c.f(o);
            new B().f(o);
        }
    }
}

thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20100528/7c3c0672/attachment-0001.html 


More information about the Mono-devel-list mailing list