Skip to content
Merged
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
18 changes: 10 additions & 8 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,34 +413,36 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
// A conversion takes the enum itself, so that converting it to object boxes the enum
// rather than its underlying value.
//
if (nonNullableType.IsEnum && expr.UnaryType is UnaryType.Negate or UnaryType.UnaryPlus)
Error.NonApplicableUnaryOperator(expr, operandType);

if (nonNullableType.IsEnum && expr.UnaryType != UnaryType.Convert)
{
if (expr.UnaryType is UnaryType.Negate or UnaryType.UnaryPlus)
Error.NonApplicableUnaryOperator(expr, operandType);

operand = ConvertOperand(operand, nonNullableType.GetEnumUnderlyingType(), lifted);
}

switch (expr.UnaryType, operand)
switch (expr.UnaryType)
{
//
// As in C#, the literals 2147483648 and 9223372036854775808 directly after a unary minus are the constants
// int.MinValue and long.MinValue. Parentheses or the U suffix keep their uint and ulong types.
// Other suffixes need no check: 2147483648 with L or UL is not a uint, and C# allows L for 9223372036854775808.
//
case (UnaryType.Negate, _) when expr.Operand is Expr.Literal { Value: 0x80000000u, HasUnsignedSuffix: false }:
case UnaryType.Negate when expr.Operand is Expr.Literal { Value: 0x80000000u, HasUnsignedSuffix: false }:
return Expression.Constant(int.MinValue);

case (UnaryType.Negate, _) when expr.Operand is Expr.Literal { Value: 0x8000000000000000ul, HasUnsignedSuffix: false }:
case UnaryType.Negate when expr.Operand is Expr.Literal { Value: 0x8000000000000000ul, HasUnsignedSuffix: false }:
return Expression.Constant(long.MinValue);

//
// C# has no negation operator for uint, so the operand is converted to long
// and then negated, which keeps the sign of the result.
//
case (UnaryType.Negate, _) when operand.Type.GetNonNullableType() == typeof(uint):
case UnaryType.Negate when operand.Type.GetNonNullableType() == typeof(uint):
operand = ConvertOperand(operand, typeof(long), lifted);
break;

case (not UnaryType.Convert, _):
case not UnaryType.Convert:
operand = ApplyUnaryNumericPromotion(operand);
break;
}
Expand Down