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: [email protected]
        ReportedBy: [EMAIL PROTECTED]
         QAContact: [email protected]
          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.
_______________________________________________
mono-bugs maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-bugs

Reply via email to