[Mono-bugs] [Bug 389653] New: Lambda method "disappears" when put into a collection by the indexer setter

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue May 13 02:40:49 EDT 2008


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


           Summary: Lambda method "disappears" when put into a collection by
                    the indexer setter
           Product: Mono: Compilers
           Version: 1.9.0
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: C#
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: merlin9x9 at gmail.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: Customer


Compile the following code with gmcs 1.9.0 or later:

        using System;
        using System.Collections.Generic;

        static class Test
        {
                static void Main()
                {
                        var funcs = new Dictionary<int, Func<int>>();
                        funcs[0] = () => A();
                        funcs[1] = () => B();
                        funcs[2] = () => C();

                        foreach(var pair in funcs)
                        {
                                int result = pair.Value();
                                Console.WriteLine(" returned {0}.", result);
                        }
                }

                static int A()
                {
                        Console.Write("A");
                        return 1;
                }

                static int B()
                {
                        Console.Write("B");
                        return 2;
                }

                static int C()
                {
                        Console.Write("C");
                        return 3;
                }
        } // Test

This code causes gmcs erroneously to generate warnings about unreachable code
on the three lines where the lambda methods are being assigned to keys in the
dictionary. Worse, the output of this program is the following:

         returned 0.
         returned 0.
         returned 0.

The output _should_ be:

        A returned 1.
        B returned 2.
        C returned 3.

Indeed, if those offending three lines are replaced with the following, the
expected output is observed and the warnings are not generated:

        funcs.Add(0, () => A());
        funcs.Add(1, () => B());
        funcs.Add(2, () => C());

The sample as originally written and with the modification both work
identically when built with Microsoft's C# compiler, neither are any warnings
emitted.

It appears as though the lambda methods are somehow not being generated
properly, and instead get replaced with the default value of the return type
for the Func<T> objects. Interestingly, if I assign those lambda methods to
temporary variables and use those variables in the dictionary assignments,
everything works as it should.

I'm certain that the problem is more general than what I described in the
summary, but this happens to be the case in which I found it. Even if it is
limited to collections, I see the same behavior using, say, List<T> and [...]=
versus Add(...).


-- 
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