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

[17.10] Disable nullable comparison optimization for decimals #73572

Merged
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 @@ -4,6 +4,7 @@

using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
Expand Down Expand Up @@ -998,7 +999,7 @@ private BoundExpression LowerLiftedBuiltInComparisonOperator(
// Optimization: If one side is non-default constant, checking HasValue is not needed.
if (kind.Operator() is BinaryOperatorKind.Equal or BinaryOperatorKind.NotEqual)
{
if (xNonNull?.ConstantValueOpt is { IsDefaultValue: false })
if (canNotBeEqualToDefaultValue(xNonNull?.ConstantValueOpt))
{
Debug.Assert(yNonNull is null, "Handled by trivial optimization above; otherwise we should use yNonNull here.");
return MakeBinaryOperator(
Expand All @@ -1011,7 +1012,7 @@ private BoundExpression LowerLiftedBuiltInComparisonOperator(
constrainedToTypeOpt: null);
}

if (yNonNull?.ConstantValueOpt is { IsDefaultValue: false })
if (canNotBeEqualToDefaultValue(yNonNull?.ConstantValueOpt))
{
Debug.Assert(xNonNull is null, "Handled by trivial optimization above; otherwise we should use xNonNull here.");
return MakeBinaryOperator(
Expand Down Expand Up @@ -1098,6 +1099,27 @@ private BoundExpression LowerLiftedBuiltInComparisonOperator(
sideEffects: ImmutableArray.Create<BoundExpression>(tempAssignmentX, tempAssignmentY),
value: binaryExpression,
type: boolType);

static bool canNotBeEqualToDefaultValue(
[NotNullWhen(returnValue: true)] ConstantValue? constantValue)
{
// This is an explicit list so new constant values are not accidentally supported without consideration.
// Decimal is not in the list because it is possible to have a non-default decimal constant
// which is equal to the default decimal (0.0m == default(decimal)).
return constantValue is
{
IsDefaultValue: false,
Discriminator: ConstantValueTypeDiscriminator.Boolean
or ConstantValueTypeDiscriminator.Double
or ConstantValueTypeDiscriminator.Int32
or ConstantValueTypeDiscriminator.Int64
or ConstantValueTypeDiscriminator.NInt
or ConstantValueTypeDiscriminator.NUInt
or ConstantValueTypeDiscriminator.Single
or ConstantValueTypeDiscriminator.UInt32
or ConstantValueTypeDiscriminator.UInt64
};
}
}

private BoundExpression LowerLiftedUserDefinedComparisonOperator(
Expand Down
Loading