Skip to content
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
26 changes: 26 additions & 0 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,32 @@ private static Expr SkipParentheses(Expr expr)
return expr;
}

/// <summary>
/// Converts the null literal opposite an operand of a nullable value type to that type.
/// For the <c>+</c> operator with an operand of a nullable enum type, the literal is converted
/// to the nullable form of the underlying type of the enum.
/// </summary>
/// <param name="expr">The binary expression.</param>
/// <param name="lhs">The expression of the left operand.</param>
/// <param name="rhs">The expression of the right operand.</param>
private static void ApplyNullLiteralConversion(Expr.Binary expr, ref Expression lhs, ref Expression rhs)
{
//
// The null literal converts to a nullable value type, so opposite an operand of such a type
// it takes that type, as in 'x == null' or 'x ?? null'. For an enum type E, only E + U and U + E
// are defined, so in 'x + null' it takes the nullable form of the underlying type U instead.
//
if (lhs.Type.IsNullable() && IsNullLiteral(expr.Right))
rhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, lhs.Type));
else if (rhs.Type.IsNullable() && IsNullLiteral(expr.Left))
lhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, rhs.Type));

static Type GetNullLiteralType(Identifier op, Type operandType) =>
op.Name == "+" && operandType.GetNonNullableType() is { IsEnum: true } enumType
? enumType.GetEnumUnderlyingType().MakeNullableType()
: operandType;
}

/// <summary>
/// Converts the integer literal 0 opposite an operand of an enum type to that enum type
/// if the operator is defined for two operands of the enum type. As in the C# compiler, a literal
Expand Down
16 changes: 1 addition & 15 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,7 @@ protected internal override Expression VisitBinary(Expr.Binary expr)
var lhsType = lhs.Type;
var rhsType = rhs.Type;

//
// The null literal converts to a nullable value type, so opposite an operand of such a type
// it takes that type, as in 'x == null' or 'x ?? null'. For an enum type E, only E + U and U + E
// are defined, so in 'x + null' it takes the nullable form of the underlying type U instead.
//
if (lhsType.IsNullable() && IsNullLiteral(expr.Right))
rhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, lhsType));
else if (rhsType.IsNullable() && IsNullLiteral(expr.Left))
lhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, rhsType));

ApplyNullLiteralConversion(expr, ref lhs, ref rhs);
ApplyEnumZeroConversion(expr, ref lhs, ref rhs);

var factory = ResolveBinaryOperatorFactory(expr.Operator.Name);
Expand Down Expand Up @@ -173,11 +164,6 @@ void Flatten(Expression e)
}

return null;

static Type GetNullLiteralType(Identifier op, Type operandType) =>
op.Name == "+" && operandType.GetNonNullableType() is { IsEnum: true } enumType
? enumType.GetEnumUnderlyingType().MakeNullableType()
: operandType;
}

/// <inheritdoc />
Expand Down