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 #1462: Inconsistent enum flag check #1476

Merged
merged 3 commits into from
May 1, 2019
Merged
Changes from 1 commit
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
21 changes: 18 additions & 3 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014 Daniel Grunwald
// Copyright (c) 2014 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
Expand Down Expand Up @@ -699,8 +699,8 @@ TranslatedExpression TranslateCeq(Comp inst, out bool negateOutput)
}

// Special case comparisons with enum and char literals
left = AdjustConstantExpressionToType(left, right.Type);
right = AdjustConstantExpressionToType(right, left.Type);
left = TryUniteEqualityOperandType(left, right);
right = TryUniteEqualityOperandType(right, left);

if (IsSpecialCasedReferenceComparisonWithNull(left, right)) {
// When comparing a string/delegate with null, the C# compiler generates a reference comparison.
Expand Down Expand Up @@ -763,6 +763,21 @@ TranslatedExpression TranslateCeq(Comp inst, out bool negateOutput)
.WithRR(rr);
}

TranslatedExpression TryUniteEqualityOperandType(TranslatedExpression operand, TranslatedExpression otherOperand)
Copy link
Member

Choose a reason for hiding this comment

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

Using names like left and right instead of operand and otherOperand will improve readability of the code.

{
// Special case for enum flag check "(enum & EnumType.SomeValue) == 0"
// so that the const 0 value is printed as 0 integer and not as enum type, e.g. EnumType.None
if (operand.ResolveResult.IsCompileTimeConstant &&
operand.ResolveResult.Type.IsCSharpPrimitiveIntegerType() &&
(int)operand.ResolveResult.ConstantValue == 0 &&
Copy link
Member

Choose a reason for hiding this comment

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

operand.ResolveResult.ConstantValue might be something else than a boxed int, so this can go wrong. This makes the unit tests fail. See https://ci.appveyor.com/project/icsharpcode/ilspy/build/job/wnhtrte5t1i7g5u9/tests

NullableType.GetUnderlyingType(otherOperand.Type).Kind == TypeKind.Enum &&
otherOperand.Expression is BinaryOperatorExpression binaryExpr &&
binaryExpr.Operator == BinaryOperatorType.BitwiseAnd)
return AdjustConstantExpressionToType(operand, compilation.FindType(KnownTypeCode.Int32));
else
return AdjustConstantExpressionToType(operand, otherOperand.Type);
}

bool IsSpecialCasedReferenceComparisonWithNull(TranslatedExpression lhs, TranslatedExpression rhs)
{
if (lhs.Type.Kind == TypeKind.Null)
Expand Down