diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index 634790b..58df7a5 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -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); diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index 6cde52d..f340493 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -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 diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 0247661..2f8f087 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -36,6 +36,7 @@ private static List 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; } }