[Mono-bugs] [Bug 399175] New: InvalidProgramException for unreachable code in iterator with CodeDomProvider

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Wed Jun 11 03:50:53 EDT 2008


https://bugzilla.novell.com/show_bug.cgi?id=399175


           Summary: InvalidProgramException for unreachable code in iterator
                    with CodeDomProvider
           Product: Mono: Compilers
           Version: 1.9.0
          Platform: i386
        OS/Version: Windows Vista
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: C#
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: jeff at thefirst.org
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---


When normally compiled, an iterator gives a compiler warning for unreachable
code, but otherwise executes correctly. However, when the same code is compiled
in a CodeDomProvider, it throws an InvalidProgramException when executed. But I
would expect it to execute the same way.

To reproduce, compile and run the following code (also attached).
Using the iterator directly with Temporary.Temporary.func() works correctly,
but using the same iterator compiled with CodeDomProvider throws an
InvalidProgramException.
Even though the "yield break" makes the code following it unreachable, I expect
the code compiled with CodeDomProvider to behave the same as normally-compiled
code called directly with Temporary.Temporary.func().
Even though the code after "yield break" is not reached, the bug still requires
it to be there as a foreach loop with its own "yield return". The bug is not
produced if the code after "yield break" is removed, or if it is changed to
some kind of code other than a foreach loop with its own "yield return".

using System;
using System.Collections.Generic;
using System.CodeDom.Compiler;

class TestDomProvider
{
    static void Main(string[] args)
    {
        string source = @"
using System;
using System.Collections.Generic;

namespace Temporary
{
    public class Temporary
    {
        public static IEnumerable<char> func()
        {
            yield return '0';
            yield break;
            foreach (char c in ""1"")
            {
                yield return c;
            }
        }
    }
}
";
        var parameters = new CompilerParameters();
        // This gets the location of the System assembly.
       
parameters.ReferencedAssemblies.Add(typeof(System.Int32).Assembly.Location);
        parameters.GenerateInMemory = true;
        var results =
CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource(parameters,
source);
        var compiledType =
results.CompiledAssembly.GetType("Temporary.Temporary");

        // This works:
        foreach (char c in Temporary.Temporary.func()) { }
        // This should do the same thing but gives InvalidProgramException:
        foreach (char c in
(IEnumerable<char>)compiledType.GetMethod("func").Invoke(null, new object[0]))
{ }
    }
}

namespace Temporary
{
    public class Temporary
    {
        public static IEnumerable<char> func()
        {
            yield return '0';
            yield break;
            foreach (char c in "1")
            {
                yield return c;
            }
        }
    }
}


-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list