Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Roslyn crash when trying to rewrite this lambda? (Visual Studio 2015 update 1)

I've just upgraded to VS2015.1 and got a compiler crash when trying to compile one of my projects. If you put the following repo code in a console application (and add a reference to moq.dll) the code in line 12 crashes my compiler. It seems to happen during a Roslyn lamdba rewrite call.

using System.Collections.Generic;
using System.Linq;
using Moq;

namespace RoslynError
{
  class Program
  {
    static void Main(string[] args)
    {
      var mockRepo = new MockRepository(MockBehavior.Strict);
      var obj = mockRepo.OneOf<DTO>(x => x.Value == (OptionEnum?)null);
    }
  }

  class DTO 
  {
    public DTO(OptionEnum? enumVal)
    {
      Value = enumVal;
    }    

    public OptionEnum? Value;
  }

  enum OptionEnum
  {
    NotSpecified    
  }
}

Anyone know why the crash occurs?

like image 632
Jes Bak Hansen Avatar asked Dec 04 '15 09:12

Jes Bak Hansen


1 Answers

The following simpler example also reproduces the problem, which is related to rewriting type conversion nodes in expression trees:

using System;
using System.Linq.Expressions;

namespace Bug461
{
  class Program
  {
    enum Test { }

    static void Main()
    {
      Expression<Func<Test?, bool>> x = t => t == (Test?)null;
    }
  }
}

Edit: I edited the code slightly to avoid a warning.

Edit 2: The bug is caused by https://github.com/dotnet/roslyn/commit/5c602fc6 where the demoted enum operand (which is the null literal) doesn't have an associated type.

Edit 3: I made a pull request with a proposed fix: https://github.com/dotnet/roslyn/pull/7227

like image 198
Sune Foldager Avatar answered Oct 30 '22 00:10

Sune Foldager