Bug 19938

Summary: Enum?/Enum comparision in Expressions generate wrong initialisation bytecodes
Product: [Mono] Compilers Reporter: David Schmitt <david>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: mono 3.2.8 compiled exe
complete source
.net compiled exe

Description David Schmitt 2014-05-20 13:02:04 UTC
Created attachment 6848 [details]
mono 3.2.8 compiled exe

Example code:

    qry.Count(t => t.Property == TestEnum.A)

where "t.Property" is of type "TestEnum?".

Expected result: the result is the number of "t"s where t.Property equals TestEnum.A

Actual result:

Unhandled Exception:
System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32'.
  at System.Linq.Expressions.Expression.GetEqualityComparisonOperator (ExpressionType binaryType, System.String opName, System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, Boolean liftToNull) [0x00000] in <filename unknown>:0
  at System.Linq.Expressions.Expression.Equal (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, Boolean liftToNull, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0
  at System.Linq.Expressions.Expression.Equal (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right) [0x00000] in <filename unknown>:0
  at ConsoleApplication2.Program.TestQry (IQueryable`1 qry) [0x00000] in <filename unknown>:0
  at ConsoleApplication2.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32'.
  at System.Linq.Expressions.Expression.GetEqualityComparisonOperator (ExpressionType binaryType, System.String opName, System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, Boolean liftToNull) [0x00000] in <filename unknown>:0
  at System.Linq.Expressions.Expression.Equal (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, Boolean liftToNull, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0
  at System.Linq.Expressions.Expression.Equal (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right) [0x00000] in <filename unknown>:0
  at ConsoleApplication2.Program.TestQry (IQueryable`1 qry) [0x00000] in <filename unknown>:0
  at ConsoleApplication2.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0


Analysis

reverse engineering the function with ILSpy shows mono converting this to "qry.Count((Test t) => (int?)t.Property == 0);" while CSC.EXE compiles this as "qry.Count((Test t) => (int?)t.Property == (int?)0);"


Please find attached the complete source and the mono compiled exe and the .net compiled exe.

I'm running 

Mono JIT compiler version 3.2.8 (Debian 3.2.8+dfsg-4~dasz1)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen
Comment 1 David Schmitt 2014-05-20 13:02:26 UTC
Created attachment 6849 [details]
complete source
Comment 2 David Schmitt 2014-05-20 13:03:07 UTC
Created attachment 6850 [details]
.net compiled exe
Comment 3 Marek Safar 2014-05-27 10:15:27 UTC
Fixed in master