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
44 changes: 30 additions & 14 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ private static MemberExpression CreateMemberAccess(Expression? expression, Membe
return null;
}

/// <summary>
/// Applies an implicit conversion of the specified expression to <see cref="bool" />, as required for
/// the operands of <c>&amp;&amp;</c> and <c>||</c> and the condition of <c>?:</c>.
/// </summary>
/// <param name="expression">The expression to convert.</param>
/// <returns>
/// The converted expression.
/// </returns>
private static Expression ApplyBooleanConversion(Expression expression)
{
var converted = ApplyImplicitConversion(expression, typeof(bool));
if (converted is null)
Error.MissingImplicitConversion(expression.Type, typeof(bool));

return converted;
}

/// <summary>
/// Converts the branches of a conditional expression to their common type.
/// </summary>
Expand Down Expand Up @@ -776,26 +793,25 @@ private static Func<Expression, Expression, Expression> 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.")
};
}
Expand Down
25 changes: 8 additions & 17 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 "??":
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down