Skip to content

Commit

Permalink
Disable nullable comparison optimization for decimals (#73536)
Browse files Browse the repository at this point in the history
* Disable nullable comparison optimization for decimals

* Update a test

* Add more tests and cleanup the list
  • Loading branch information
jjonescz authored May 18, 2024
1 parent 3dfab18 commit 40811c8
Show file tree
Hide file tree
Showing 3 changed files with 1,683 additions and 26 deletions.
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

0 comments on commit 40811c8

Please sign in to comment.