C# 'is' type check on struct - odd .NET 4.0 x86 optimization behavior

前端 未结 6 939
孤城傲影
孤城傲影 2021-02-02 07:20

Update: I have filed a bug report with Microsoft Connect, please vote for it!

Update 2: Microsoft have marked the bug report as fixed

6条回答
  •  独厮守ぢ
    2021-02-02 07:49

    I worked up a similar example that fails the same way:

    using System;
    using System.Runtime.CompilerServices;
    
    public class Program {
      static void Main() {
        Console.Write(Verify(Test.Create()));
        Console.ReadLine();
      }
      //[MethodImpl(MethodImplOptions.NoInlining)]
      static bool Verify(IDisposable item) {
        return item is Test;
      }
      struct Test : IDisposable {
        public void Dispose() { }
        public static Test Create() { return new Test(); }
      }
    }
    

    It is a JIT optimizer bug. Can't quite put the finger on it, it optimizes the code heavily. But it looks to me like it gets in trouble when it optimizes the boxing conversion away. Pretty serious bug, frankly.


    This bug has been fixed, I can no longer repro it. My current version of clrjit.dll is 4.0.30319.237 dated May 17th 2011. I can't tell exactly what update repaired it. I got a security update on Aug 5th 2011 that updated clrjit.dll to revision 235 with a date of Apr 12, that would be the earliest.

提交回复
热议问题