Browse Source

Avoid user-defined operators when reference comparison is desired.

pull/728/merge
Daniel Grunwald 9 years ago
parent
commit
b558f0c0bd
  1. 7
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  2. 2
      ICSharpCode.Decompiler/Tests/RoundtripAssembly.cs
  3. 30
      ICSharpCode.Decompiler/Tests/TestCases/Correctness/Comparisons.cs

7
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -466,7 +466,12 @@ namespace ICSharpCode.Decompiler.CSharp
if (rr == null || rr.IsError || rr.UserDefinedOperatorMethod != null if (rr == null || rr.IsError || rr.UserDefinedOperatorMethod != null
|| rr.Operands[0].Type.GetStackType() != inst.InputType) || rr.Operands[0].Type.GetStackType() != inst.InputType)
{ {
var targetType = TypeUtils.GetLargerType(left.Type, right.Type); IType targetType;
if (inst.InputType == StackType.O) {
targetType = compilation.FindType(KnownTypeCode.Object);
} else {
targetType = TypeUtils.GetLargerType(left.Type, right.Type);
}
if (targetType.Equals(left.Type)) { if (targetType.Equals(left.Type)) {
right = right.ConvertTo(targetType, this); right = right.ConvertTo(targetType, this);
} else { } else {

2
ICSharpCode.Decompiler/Tests/RoundtripAssembly.cs

@ -52,7 +52,7 @@ namespace ICSharpCode.Decompiler.Tests
{ {
try { try {
RunWithTest("NRefactory", "ICSharpCode.NRefactory.CSharp.dll", "ICSharpCode.NRefactory.Tests.dll"); RunWithTest("NRefactory", "ICSharpCode.NRefactory.CSharp.dll", "ICSharpCode.NRefactory.Tests.dll");
} catch (CompilationFailedException ex) { } catch (TestRunFailedException ex) {
Assert.Ignore(ex.Message); Assert.Ignore(ex.Message);
} }
} }

30
ICSharpCode.Decompiler/Tests/TestCases/Correctness/Comparisons.cs

@ -26,7 +26,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases
{ {
public static int Main() public static int Main()
{ {
// disable CompareOfFloatsByEqualityOperator #pragma warning disable RECS0018 // Comparison of floating point numbers with equality operator
TestFloatOp("==", (a, b) => a == b); TestFloatOp("==", (a, b) => a == b);
TestFloatOp("!=", (a, b) => a != b); TestFloatOp("!=", (a, b) => a != b);
TestFloatOp("<", (a, b) => a < b); TestFloatOp("<", (a, b) => a < b);
@ -42,6 +42,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases
TestUInt(uint.MaxValue); TestUInt(uint.MaxValue);
TestUShort(0); TestUShort(0);
TestUShort(ushort.MaxValue); TestUShort(ushort.MaxValue);
Console.WriteLine(IsNotNull(new OverloadedOperators()));
Console.WriteLine(IsNull(new OverloadedOperators()));
return 0; return 0;
} }
@ -75,5 +78,30 @@ namespace ICSharpCode.Decompiler.Tests.TestCases
Console.WriteLine("uint: {0} == -1 = {1}", i, i == -1); Console.WriteLine("uint: {0} == -1 = {1}", i, i == -1);
Console.WriteLine("uint: {0} == Id(-1) = {1}", i, i == Id(-1)); Console.WriteLine("uint: {0} == Id(-1) = {1}", i, i == Id(-1));
} }
static bool IsNull(OverloadedOperators oo)
{
return (object)oo == null;
}
static bool IsNotNull(OverloadedOperators oo)
{
return (object)oo != null;
}
}
#pragma warning disable CS0660 // Type defines operator == or operator != but does not override Object.Equals(object o)
#pragma warning disable CS0661 // Type defines operator == or operator != but does not override Object.GetHashCode()
class OverloadedOperators
{
public static bool operator ==(OverloadedOperators oo, object b)
{
throw new NotSupportedException("Not supported to call the user-defined operator");
}
public static bool operator !=(OverloadedOperators oo, object b)
{
throw new NotSupportedException("Not supported to call the user-defined operator");
}
} }
} }

Loading…
Cancel
Save