diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
index 53bd595..8089be5 100644
--- a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
+++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
@@ -110,6 +110,23 @@ private static MemberExpression CreateMemberAccess(Expression? expression, Membe
return null;
}
+ ///
+ /// Applies an implicit conversion of the specified expression to , as required for
+ /// the operands of && and || and the condition of ?:.
+ ///
+ /// The expression to convert.
+ ///
+ /// The converted expression.
+ ///
+ private static Expression ApplyBooleanConversion(Expression expression)
+ {
+ var converted = ApplyImplicitConversion(expression, typeof(bool));
+ if (converted is null)
+ Error.MissingImplicitConversion(expression.Type, typeof(bool));
+
+ return converted;
+ }
+
///
/// Converts the branches of a conditional expression to their common type.
///
@@ -776,26 +793,25 @@ private static Func ResolveBinaryOperatorFac
{
return @operator switch
{
- "??" => Expression.Coalesce,
- "&&" => Expression.AndAlso,
- "||" => Expression.OrElse,
- "==" => Expression.Equal,
- "!=" => Expression.NotEqual,
- "<=" => Expression.LessThanOrEqual,
- ">=" => Expression.GreaterThanOrEqual,
- ">>" => Expression.RightShift,
+ "*" => Expression.Multiply,
+ "/" => Expression.Divide,
+ "%" => Expression.Modulo,
+ "+" => Expression.Add,
+ "-" => Expression.Subtract,
"<<" => Expression.LeftShift,
+ ">>" => Expression.RightShift,
">>>" => Expression.RightShift,
"<" => Expression.LessThan,
">" => Expression.GreaterThan,
+ "<=" => Expression.LessThanOrEqual,
+ ">=" => Expression.GreaterThanOrEqual,
+ "==" => Expression.Equal,
+ "!=" => Expression.NotEqual,
"&" => Expression.And,
- "|" => Expression.Or,
"^" => Expression.ExclusiveOr,
- "+" => Expression.Add,
- "-" => Expression.Subtract,
- "*" => Expression.Multiply,
- "/" => Expression.Divide,
- "%" => Expression.Modulo,
+ "|" => Expression.Or,
+ "&&" => Expression.AndAlso,
+ "||" => Expression.OrElse,
_ => throw new NotSupportedException($"Operator '{@operator}' is not supported.")
};
}
diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
index c70982c..bfde8d9 100644
--- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
+++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
@@ -67,21 +67,11 @@ protected internal override Expression VisitBinary(Expr.Binary expr)
ApplyNullLiteralConversion(expr, ref lhs, ref rhs);
ApplyEnumZeroConversion(expr, ref lhs, ref rhs);
- var factory = ResolveBinaryOperatorFactory(expr.Operator.Name);
-
switch (expr.Operator.Name)
{
- case "&&":
- case "||":
- lhs = ApplyImplicitConversion(lhs, typeof(bool));
- rhs = ApplyImplicitConversion(rhs, typeof(bool));
-
- if (lhs is null)
- Error.MissingImplicitConversion(lhsType, typeof(bool));
-
- if (rhs is null)
- Error.MissingImplicitConversion(rhsType, typeof(bool));
-
+ case "&&" or "||":
+ lhs = ApplyBooleanConversion(lhs);
+ rhs = ApplyBooleanConversion(rhs);
break;
case "??":
@@ -91,6 +81,10 @@ protected internal override Expression VisitBinary(Expr.Binary expr)
return ApplyStringConcatenation(lhs, rhs);
}
+ // The factory is resolved outside the try block, so that an unknown operator fails with NotSupportedException
+ // rather than being reported as an operator that cannot be applied to its operands.
+ var factory = ResolveBinaryOperatorFactory(expr.Operator.Name);
+
try
{
return ApplyBinaryExpression(expr.Operator, factory, lhs, rhs);
@@ -394,10 +388,7 @@ protected internal override Expression VisitConditional(Expr.Conditional expr)
try
{
- var condition = ApplyImplicitConversion(test, typeof(bool));
- if (condition is null)
- Error.MissingImplicitConversion(test.Type, typeof(bool));
-
+ var condition = ApplyBooleanConversion(test);
var type = ApplyConditionalConversions(expr, ref ifTrue, ref ifFalse);
return Expression.Condition(condition, ifTrue, ifFalse, type);
}
diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
index eee2f3d..2795779 100644
--- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
+++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
@@ -769,6 +769,7 @@ public void Evaluate_ConditionalWithNullableBranch_ReturnsNullableType(string ex
[TestCase("+NullableDay", "Unary operator '+' cannot be applied to operand of type 'System.Nullable`1[System.DayOfWeek]'.")]
[TestCase("-NullableULong", "Unary operator '-' cannot be applied to operand of type 'System.Nullable`1[System.UInt64]'.")]
[TestCase("NullBool && true", "Cannot implicitly convert type 'System.Nullable`1[System.Boolean]' to 'System.Boolean'.")]
+ [TestCase("null && NullBool", "Cannot implicitly convert type 'System.Nullable`1[System.Boolean]' to 'System.Boolean'.")]
[TestCase("Flag ? NullableInt : 1L", "Operator '?:' cannot be applied to operands of type 'System.Nullable`1[System.Int32]' and 'System.Int64'.")]
public void Parse_NullableOperandsWithoutOperatorInCSharp_ReturnsError(string expression, string expected)
{