Bug 23475 - "Invalid IL code ... method body is empty" when using fallthrough case in switch statement
Summary: "Invalid IL code ... method body is empty" when using fallthrough case in swi...
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-09-29 22:34 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2014-09-30 05:18 UTC (History)
1 user (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 Brendan Zagaeski (Xamarin Team, assistant) 2014-09-29 22:34:27 UTC
"Invalid IL code ... method body is empty" when using fallthrough case in switch statement.

This is a sibling of the earlier bug 21805. I'm not sure if this situation is specifically mentioned in the C# specification, so the current behavior might not necessarily be "wrong."


## Steps to reproduce

1. Compile the following program using `mcs`:

using System;

public enum Foo { One, Two };

class MainClass
{
    public static void Main(string[] args)
    {
        const Foo foo = Foo.Two;
        object obj;

        switch (foo)
        {
            case Foo.One:
            case Foo.Two:
                obj = new object();
                break;
        }
        Console.WriteLine(obj.ToString());
    }
}


2. Run the program with `mono`.


## Result when compiled with `mcs`

### Command-line output from step 2

> $ mono Program.exe
> Unhandled Exception:
> System.InvalidProgramException: Invalid IL code in MainClass:Main (string[]): method body is empty.
> 
> [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidProgramException: Invalid IL code in MainClass:Main (string[]): method body is empty.


## Result when compiled with `csc`

> $ mono Program.exe
> System.Object



## Additional result (same on both `mcs` and `csc`)

If you remove `const` from the `foo` local variable, both compilers reject the code due to "Use of unassigned local variable 'obj'". Since `csc` has the same behavior as `mcs` in this "additional" case, the only issue with `mcs` is when `foo` is *not* marked `const`.



## Version Information

Tested with Mono 3.10.0 (detached/ac51002) and 3.10.1 (master/4e3ca74).
Comment 1 Marek Safar 2014-09-30 05:18:08 UTC
Fixed in master