From 8a067f3c56e8bc27a763abb04fb65a93356fe653 Mon Sep 17 00:00:00 2001 From: rameel Date: Tue, 17 Nov 2026 21:09:39 +0500 Subject: [PATCH] parser: restrict '!' to logical negation The '!' operator was built with Expression.Not, which performs a bitwise complement on integers and falls back to op_OnesComplement for types without op_LogicalNot. As a result, '!' silently worked as '~': '!1' evaluated to -2, '!DayOfWeek.Friday' to -6, and '!x' called 'operator ~' on a type that defined only that operator. Apply '!' only to bool, bool?, and types that define 'operator !', as C# does, and report an error for other operands. A user-defined operator is now also found when it is declared in a base class, and it is still lifted for nullable operands. --- .../ExpressionBuilder.cs | 20 +++++++- .../Data/Errors.txt | 15 ++++++ .../Data/Expressions.txt | 12 +++++ .../ExpressionParserTest.cs | 46 +++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index 7c8e1cf..5c38f27 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -407,7 +407,25 @@ protected internal override Expression VisitUnary(Expr.Unary expr) switch (expr.UnaryType) { case UnaryType.Not: - return Expression.Not(operand); + // + // Expression.Not performs a bitwise complement on integers and falls back to op_OnesComplement + // when a type does not define op_LogicalNot, so '!' would silently work as '~'. + // Allow only the logical negation, as C# does. For a nullable operand, + // Expression.Not lifts the negation of the underlying type. + // + var nonNullableType = Nullable.GetUnderlyingType(operandType) ?? operandType; + if (nonNullableType == typeof(bool)) + return Expression.Not(operand); + + var logicalNot = nonNullableType.GetMethod( + "op_LogicalNot", + BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy, + [nonNullableType]); + + if (logicalNot is null) + Error.NonApplicableUnaryOperator(expr, operandType); + + return Expression.Not(operand, logicalNot); case UnaryType.Negate: return Expression.Negate(operand); diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index 1613a8b..c298fc6 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -136,3 +136,18 @@ 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'. + +####################################### +# ! operator + +!1 +Unary operator '!' cannot be applied to operand of type 'System.Int32'. + +!1L +Unary operator '!' cannot be applied to operand of type 'System.Int64'. + +!byte.MaxValue +Unary operator '!' cannot be applied to operand of type 'System.Byte'. + +!DayOfWeek.Friday +Unary operator '!' cannot be applied to operand of type 'System.DayOfWeek'. diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt index 5618939..c6a4dbe 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt @@ -491,3 +491,15 @@ Convert(1, Int64) -1L -1 + +####################################### +# ! operator + +!true +Not(True) + +!(1 > 0) +Not((1 > 0)) + +!!false +Not(Not(False)) diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index f959a58..791ff96 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -124,6 +124,25 @@ public void ContextTest() public void EvaluateExpression(string expression, object result) => Assert.That(ExpressionParser.Evaluate(expression), Is.EqualTo(result)); + [TestCase("!Flag", false)] + [TestCase("!NullableFlag", true)] + [TestCase("!WithLogicalNot", true)] + [TestCase("!WithInheritedLogicalNot", true)] + [TestCase("!NullableWithLogicalNot", true)] + public void LogicalNotTest(string expression, bool expected) => + Assert.That(ExpressionParser.Evaluate(expression, new LogicalNotContext()), Is.EqualTo(expected)); + + [Test] + public void LogicalNotOnOnesComplementOnlyTest() + { + var parameter = Expression.Parameter(typeof(LogicalNotContext), "c"); + var result = ExpressionParser.Parse("!WithOnesComplement", new ContextAwareBinder(parameter)); + + Assert.That(result.Success, Is.False); + Assert.That(result.ErrorMessage, Is.EqualTo( + $"Unary operator '!' cannot be applied to operand of type '{typeof(OnesComplementOperand)}'.")); + } + [TestCaseSource(nameof(ErrorsSource))] public void ParseErrorExpression(string expression, string expected) { @@ -280,4 +299,31 @@ public class UserInfo public string? Name { get; set; } public string? LastName { get; set; } } + + public class LogicalNotContext + { + public bool Flag { get; } = true; + public bool? NullableFlag { get; } = false; + public LogicalNotOperand WithLogicalNot { get; } = new(); + public DerivedLogicalNotOperand WithInheritedLogicalNot { get; } = new(); + public LogicalNotStruct? NullableWithLogicalNot { get; } = new LogicalNotStruct(); + public OnesComplementOperand WithOnesComplement { get; } = new(); + } + + public class LogicalNotOperand + { + public static bool operator !(LogicalNotOperand _) => true; + } + + public class DerivedLogicalNotOperand : LogicalNotOperand; + + public struct LogicalNotStruct + { + public static bool operator !(LogicalNotStruct _) => true; + } + + public class OnesComplementOperand + { + public static bool operator ~(OnesComplementOperand _) => true; + } }