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
7 changes: 6 additions & 1 deletion src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,13 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
case (UnaryType.Negate, ConstantExpression { Value: 0x8000000000000000ul }):
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 == typeof(uint):
return Expression.Convert(operand, typeof(long));
operand = Expression.Convert(operand, typeof(long));
break;

case (not UnaryType.Convert, _) when underlyingType == typeof(byte):
case (not UnaryType.Convert, _) when underlyingType == typeof(sbyte):
Expand Down
4 changes: 2 additions & 2 deletions tests/Ramstack.ExpressionParser.Tests/Data/Expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ byte.MaxValue + byte.MinValue
~(Convert(Convert(1, Int16), Int32))

-100u
Convert(100, Int64)
-Convert(100, Int64)

null ?? null ?? string.Empty
(null ?? (null ?? String.Empty))
Expand Down Expand Up @@ -487,7 +487,7 @@ IIF(True, Convert(Convert(1, Int64), Int32), 2)
-1

-1u
Convert(1, Int64)
-Convert(1, Int64)

-1L
-1
Expand Down
7 changes: 7 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ public void ContextTest()
public void EvaluateExpression(string expression, object result) =>
Assert.That(ExpressionParser.Evaluate(expression), Is.EqualTo(result));

[TestCase("-1u", -1L)]
[TestCase("-4294967295u", -4294967295L)]
[TestCase("-3000000000", -3000000000L)]
[TestCase("-(5 :> uint)", -5L)]
public void Evaluate_NegatedUInt32_ReturnsNegatedInt64(string expression, long expected) =>
Assert.That(ExpressionParser.Evaluate(expression), Is.TypeOf<long>().And.EqualTo(expected));

[TestCase("!Flag", false)]
[TestCase("!NullableFlag", true)]
[TestCase("!WithLogicalNot", true)]
Expand Down
Loading