[Mono-bugs] [Bug 624324] New: gmcs generates invalid programs

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Wed Jul 21 10:46:12 EDT 2010


http://bugzilla.novell.com/show_bug.cgi?id=624324

http://bugzilla.novell.com/show_bug.cgi?id=624324#c0


           Summary: gmcs generates invalid programs
    Classification: Mono
           Product: Mono: Compilers
           Version: 2.6.x
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: C#
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: achille.fouilleul at gadz.org
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---
           Blocker: ---


User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.7)
Gecko/20100713 Firefox/3.6.7 (.NET CLR 3.5.30729)

When compiled by Mono's gmcs (both 2.6.7 and current github code), the
following program fails:

------------------------------------------------------------------------------
using System;

class P
{
    public int A;
};

static class Program
{
    static int Extra() { return 36; }

    static Func<int> Get(int dummy)
    {
        var p = new P { A = 6 };
        switch (dummy)
        {
            case 0:
                int extra = Extra();
                return () => p.A + extra;

            case 1:
                return () => p.A * 7;
        }
        throw new NotSupportedException();
    }

    static void Run(int i)
    {
        var f = Get(i);
        Console.WriteLine("target: {0}", f.Target != null ? "not null" :
"null");
        Console.WriteLine(f());
    }

    static void Main()
    {
        Run(0);
        Run(1);
    }
}
------------------------------------------------------------------------------

Under Mono 2.6.7, for example:
------------------------------------------------------------------------------
target: not null
42
target: null

Unhandled Exception: System.InvalidProgramException: Invalid IL code in
(wrapper delegate-invoke) System.Func`1<int>:invoke_int__this__ (): IL_004b:
ldarg.1   


  at Program.Run (Int32 i) [0x00000] 
  at Program.Main () [0x00000] 
------------------------------------------------------------------------------

Under .NET 4.0:
------------------------------------------------------------------------------
target: not null
42

Unhandled Exception: System.ArgumentException: Delegate to an instance method
ca
nnot have null 'this'.
   at System.MulticastDelegate.ThrowNullThisInDelegateToInstance()
   at Program.Get(Int32 dummy)
   at Program.Run(Int32 i)
   at Program.Main()
------------------------------------------------------------------------------

When compiled with Microsoft's CSC (3.5 or 4.0), the program runs successfully
under both .NET and Mono.

I've had a look at the IL disassembly of the Get() method. The instance of
AnonStorey1 that contains the closure fields is constructed by the code that
corresponds to the "case 0" branch and assigned to "loc.3". Therefore, when the
"case 1" branch is taken, the delegate that is returned is instantiated from
the uninitialized "loc.3" local variable. In that second case, the lambda
doesn't even reference the "extra" local, so its code ("<>m__1") could be moved
to AnonStorey0 and the delegate created from its instance held by "loc.0".



Reproducible: Always



Expected Results:  
target: not null
42
target: not null
42

-- 
Configure bugmail: http://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