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
5 changes: 4 additions & 1 deletion src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,13 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
: operandType;

//
// Arithmetic and bitwise operators apply to the underlying type of an enum.
// C# does not define the unary '-' and '+' operators for an enum, and '~' applies to its underlying type.
// A conversion takes the enum itself, so that converting it to object boxes the enum
// rather than its underlying value.
//
if (operandType.IsEnum && expr.UnaryType is UnaryType.Negate or UnaryType.UnaryPlus)
Error.NonApplicableUnaryOperator(expr, operandType);

if (operandType.IsEnum && expr.UnaryType != UnaryType.Convert)
operand = Expression.Convert(operand, underlyingType);

Expand Down
9 changes: 9 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ Operator '+' cannot be applied to operands of type 'System.UInt64' and 'System.I
-1ul
Unary operator '-' cannot be applied to operand of type 'System.UInt64'.

-DayOfWeek.Friday
Unary operator '-' cannot be applied to operand of type 'System.DayOfWeek'.

+DayOfWeek.Friday
Unary operator '+' cannot be applied to operand of type 'System.DayOfWeek'.

-StringSplitOptions.RemoveEmptyEntries
Unary operator '-' cannot be applied to operand of type 'System.StringSplitOptions'.

#######################################
# ! operator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ private static List<object[]> EvaluatesSource
list.Add(["""Guid.Empty""", Guid.Empty]);
list.Add(["""1 :> DayOfWeek :> object""", DayOfWeek.Monday]);
list.Add(["""(DayOfWeek.Friday :> object).ToString()""", "Friday"]);
list.Add(["""-(DayOfWeek.Friday :> int)""", -5]);
return list;
}
}
Expand Down
Loading