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
20 changes: 19 additions & 1 deletion src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
12 changes: 12 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,15 @@ Convert(1, Int64)

-1L
-1

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

!true
Not(True)

!(1 > 0)
Not((1 > 0))

!!false
Not(Not(False))
46 changes: 46 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}
Loading