Bug 20983

Summary: mcs reports unassigned variable when using named arguments
Product: [Mono] Compilers Reporter: Richard Cook <rcook>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono, sig-rnd-sat-mono-bugs
Priority: ---    
Version: 3.4.0   
Target Milestone: ---   
Hardware: All   
OS: All   
Tags: Is this bug a regression?: ---
Last known good build:

Description Richard Cook 2014-06-30 17:44:50 UTC
===== Test.cs =====
     1  class Test
     2  {
     3      public static void Main()
     4      {
     5          string p;
     6          M(
     7            y: p = F(),
     8            x: p + G());
     9      }
    10
    11      public static void M(string x, string y) { }
    12      public static string F() { return null; }
    13      public static string G() { return null; }
    14  }
===== End of Test.cs =====

Compiling with mcs (version 3.4.0.0) emits the following output:

===== Output =====
Test.cs(8,14): error CS0165: Use of unassigned local variable `p'
Compilation failed: 1 error(s), 0 warnings
===== End of output =====

If I change line 5 to "string p = null;", then this error goes away. Decompiling the generated code suggests that mcs is generating calls to F and G in the right order:

===== Decompilation of IL =====
    // method line 2
    .method public static hidebysig
           default void Main ()  cil managed
    {
        // Method begins at RVA 0x2058
        .entrypoint
        // Code size 28 (0x1c)
        .maxstack 2
        .locals init (
                string  V_0,
                string  V_1)
        IL_0000:  ldnull
        IL_0001:  stloc.0
        IL_0002:  call string class Test::F()
        IL_0007:  dup
        IL_0008:  stloc.0
        IL_0009:  stloc.1
        IL_000a:  ldloc.0
        IL_000b:  call string class Test::G()
        IL_0010:  call string string::Concat(string, string)
        IL_0015:  ldloc.1
        IL_0016:  call void class Test::M(string, string)
        IL_001b:  ret
    } // end of method Test::Main
===== End of decompilation of IL =====

So, it looks like the correct lowered form is emitted but the initial data flow analysis does not correctly determine that "p" is initialized.
Comment 1 Marek Safar 2014-07-07 07:32:08 UTC
Fixed in master