Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3322: Add missing checks for equality comparison #3368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<Compile Include="ProjectDecompiler\TargetFrameworkTests.cs" />
<Compile Include="TestAssemblyResolver.cs" />
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />
<Compile Include="TestCases\Pretty\Comparisons.cs" />
<None Include="TestCases\VBPretty\VBAutomaticEvents.vb" />
<Compile Include="TestCases\VBPretty\VBAutomaticEvents.cs" />
<Compile Include="TestCases\VBPretty\VBNonGenericForEach.cs" />
Expand Down
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,12 @@ public async Task OptionalArguments([ValueSource(nameof(defaultOptions))] Compil
await RunForLibrary(cscOptions: cscOptions);
}

[Test]
public async Task Comparisons([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions);
}

[Test]
public async Task ConstantsTests([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
Expand Down
18 changes: 18 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/Comparisons.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
public class Comparisons
{
private class A
{
}

private class B
{
}

private bool CompareUnrelatedNeedsCast(A a, B b)
{
return (object)a == b;
}
}
}
6 changes: 5 additions & 1 deletion ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,11 @@ public ResolveResult ResolveBinaryOperator(BinaryOperatorType op, ResolveResult
}
if (op == BinaryOperatorType.Equality || op == BinaryOperatorType.InEquality)
{
if (lhsType.IsReferenceType == true && rhsType.IsReferenceType == true)
if (lhsType.IsReferenceType == true && rhsType.IsReferenceType == true
&& (conversions.IdentityConversion(lhsType, rhsType)
|| conversions.ExplicitConversion(lhsType, rhsType).IsReferenceConversion
|| conversions.IdentityConversion(rhsType, lhsType)
|| conversions.ExplicitConversion(rhsType, lhsType).IsReferenceConversion))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this condition coming from some spec?
I think this might be overly restrictive; wasn't if (obj == 42) also allowed (with a warning)?
Though I guess being overly restrictive here will just result in some harmless redundant casts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw identity conversions are always symmetric, so if you already checked IdentityConversion(lhsType, rhsType) you don't need to also check IdentityConversion(rhsType, lhsType), as that will always have the same result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
// If it's a reference comparison
if (op == BinaryOperatorType.Equality)
Expand Down
Loading