Bug 20983 - mcs reports unassigned variable when using named arguments
Summary: mcs reports unassigned variable when using named arguments
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 3.4.0
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-06-30 17:44 UTC by Richard Cook
Modified: 2014-07-07 07:32 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

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