From 50898e351237ef27fb12f2149e7f22763b75b2ee Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 25 Nov 2026 01:51:49 +0500 Subject: [PATCH 1/3] parser: resolve the binary operator factory only where it is used VisitBinary resolved the factory before the switch, so the result was unused for ?? and the string + operator, and ResolveBinaryOperatorFactory kept an entry for ?? only because of that. Resolve the factory after the switch, still outside the try block, so that an unknown operator keeps failing with NotSupportedException, and remove the ?? entry. Order the remaining entries by precedence. No behavior change. --- .../ExpressionBuilder.Helpers.cs | 27 +++++++++---------- .../ExpressionBuilder.cs | 6 +++-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs index 53bd595..c295774 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs @@ -776,26 +776,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..6d486cd 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -67,8 +67,6 @@ 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 "&&": @@ -91,6 +89,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); From 66f68a62eb794db4b6d0fab2c79adeb210b33f18 Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 25 Nov 2026 01:52:16 +0500 Subject: [PATCH 2/3] parser: extract the implicit conversion to bool The operands of && and || and the condition of ?: were converted to bool by the same steps: ApplyImplicitConversion, a null check, and MissingImplicitConversion. Move them to ApplyBooleanConversion. The error now names the type of the operand after the null literal conversion rather than before it. The two differ only for the null literal on the left of && or || with an operand of a nullable value type on the right, where the literal takes the type of that operand: null && NullBool now reports 'System.Nullable`1[System.Boolean]' instead of 'System.Object'. --- .../ExpressionBuilder.Helpers.cs | 17 +++++++++++++++++ .../ExpressionBuilder.cs | 19 ++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs index c295774..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. /// diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index 6d486cd..bfde8d9 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -69,17 +69,9 @@ protected internal override Expression VisitBinary(Expr.Binary expr) 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 "??": @@ -396,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); } From 02cb44527cded6e46dbe13444c3b8678d64b64e9 Mon Sep 17 00:00:00 2001 From: rameel Date: Wed, 25 Nov 2026 02:06:44 +0500 Subject: [PATCH 3/3] parser: test the type reported for a null literal operand of && --- tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs | 1 + 1 file changed, 1 insertion(+) 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) {