This project is read-only.


Invalid IL generated for a certain combination of switch/if/return/break when optimizations are enabled


The following code throws "System.InvalidProgramException: JIT Compiler encountered an internal limitation." at runtime when compiled with rcsc /o
class Program {
    static string boo(int i) {
        switch (i) {
            case 42:
                var x = "foo";

                if (x != "bar")

                return x;
        return null;

    static void Main() {
This probably happens due to bad stack handling:
IL_0000:  ldarg.0
IL_0001:  ldc.i4.s   42
IL_0003:  bne.un.s   IL_0018
IL_0005:  ldstr      "foo"
IL_000a:  dup
IL_000b:  ldstr      "bar"
IL_0010:  call       bool [mscorlib]System.String::op_Inequality(string, string)
// when this branch is taken there's a "foo" on the stack 
// but the code at IL_0018 expects the stack to be empty
IL_0015:  brtrue.s   IL_0018 
IL_0017:  ret
IL_0018:  ldnull
IL_0019:  ret


angocke wrote Jun 23, 2014 at 12:45 AM

Fixed in changeset 28aab68c13248e1b2d65109495a7b1f2fc71815d