Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums in lambda expressions are compiled differently; consequence of overload resolution improvements?

While trying out the Visual Studio 2015 RC, I received a run-time error on previously working code. Given the lambda (x => x.CustomerStatusID == CustomerStatuses.Active) which was passed to a function as an Expression<>, the debugger shows a difference in the expression tree. Formerly it compiled as this:

.Lambda #Lambda1<System.Func`2[Services.DataClasses.CustomerDC,System.Boolean]>(Services.DataClasses.CustomerDC $x)
{
    (System.Int32)$x.CustomerStatusID == 0
}

But in C# 6.0 it now compiles as

.Lambda #Lambda1<System.Func`2[Services.DataClasses.CustomerDC,System.Boolean]>(Services.DataClasses.CustomerDC $x)
{
    (System.Int32)$x.CustomerStatusID == (System.Int32).Constant<Services.DataClasses.CustomerStatuses>(Active)
}

Although the fix to my tree-traversing code was straightforward and the additional detail is appreciated, does anyone know of any other gotchas floating around like this?

Alternately, does anyone have a link to information on the specifics of how overload resolution was improved? I cannot find any.

like image 488
Ron Newcomb Avatar asked May 29 '15 20:05

Ron Newcomb


1 Answers

This has nothing to do with overload resolution. The previous compiler prematurely optimized the right-hand-side of the comparison, thereby omitting the code corresponding to the source expression.

like image 69
Neal Gafter Avatar answered Sep 22 '22 00:09

Neal Gafter