An expression that accepts a DateTimeOffset raises a Visual Studio internal compiler error

I tried to mock an interface that accepts DateTimeOffset? as one of its parameters. Suddenly, Visual Studio started reporting "Internal Compiler Error" and that it "stopped working." After many tests, I started deleting files one by one, and then code by line. It comes down to the code below that reproduces this error:

 public class testClass { public interface ITest { void Test(DateTimeOffset? date); } public void test2() { var mock = new Mock<ITest>(); mock.Setup(x => x.Test(new DateTime(2012, 1, 1))); } } 

The problem seems to be in the line:

 mock.Setup(x => x.Test(new DateTime(2012, 1, 1))); 

If I comment on this, the compiler works fine. Also, the problem is that I am setting up a new DateTime() that fits in a DateTimeOffset .

Is this a bug in Moq or VS2012 ? Has anyone ever received this error before?

UPDATE

The following code example also leads to a compilation error, both with the regular Visual Studio 2012 compiler and with Roslyn CTP September 2012:

 using System; using System.Linq.Expressions; public interface ITest { void Test(DateTimeOffset? date); } public class TestClass { Expression<Action<ITest>> t = x => x.Test(new DateTime(2012, 1, 1)); } 

Mistake:

1> CSC: error CS0583: Internal compiler error (0xc0000005 at address 00D77AFB): the likely culprit is "BIND".

This code has nothing to do with Moq.

+7
source share
2 answers

This is impressive, the C # compiler crashes, as it is a very rare feat. You can report this to connect.microsoft.com, although Microsoft should have received many WER reports from it. Somewhat from me anyway :)

You can work around the problem by rewriting the code. Or:

 static DateTimeOffset? arg = new DateTime(2012, 1, 1); Expression<Action<ITest>> t = x => x.Test(arg); 

Or using a cleaner:

 public class TestClass { Expression<Action<ITest>> t; public TestClass() { DateTimeOffset? arg = new DateTime(2012, 1, 1); t = x => x.Test(arg); } } 
+4
source

This is clearly a mistake in the semantic analyzer. (The text “The likely culprit is BIND” is typical for errors in the semantic analyzer, which is internally called the “binder.”) The scenario here is that we have a user-transformed user-transformed transformation into a lambda, which is converted to an expression tree. This code was a bug farm. I thought I wrote a script for this exact script, but maybe I didn’t.

In any case, the problem is most likely my failure, so I'm sorry about that. Although I can not do it now.

What is really strange is that the error is allegedly reproduced on both Roslyn and the C # 5 compiler. This is a crazy coincidence because the compilers of Roslyn and C # 5 have completely different code for this part of the semantic analysis. We rewrote most of this from scratch. It is strange that we made the same mistake twice.

In any case, Kevin will see this, since you checked it with Roslyn, and if you want to enter an error on the Connect website, this will be appreciated by the team, I'm sure.

UPDATE:

Wait, you have exactly the same error in Roslyn? Then it is likely that the IDE is still using the C # 5 analysis library. If you write code that loads the violating code into the Roslyn compilation and parses it, you probably won't get an error. Correctly?

+7
source

All Articles