Skip to content

Commit 546a0cb

Browse files
authored
Revert changes introduced in #296 (#333)
* Fix format * Simplify ExpressionUtils.PromoteExpression * Revert changes done in #296
1 parent a9bdac0 commit 546a0cb

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

src/DynamicExpresso.Core/Parsing/Parser.cs

+10-12
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private Expression ParseAssignment()
228228
NextToken();
229229
var right = ParseAssignment();
230230

231-
var promoted = ExpressionUtils.PromoteExpression(right, left.Type, true);
231+
var promoted = ExpressionUtils.PromoteExpression(right, left.Type);
232232
if (promoted == null)
233233
throw ParseException.Create(_token.pos, ErrorMessages.CannotConvertValue,
234234
TypeUtils.GetTypeName(right.Type), TypeUtils.GetTypeName(left.Type));
@@ -336,8 +336,8 @@ private Expression ParseComparison()
336336
{
337337
var left = ParseTypeTesting();
338338
while (_token.id == TokenId.DoubleEqual || _token.id == TokenId.ExclamationEqual ||
339-
_token.id == TokenId.GreaterThan || _token.id == TokenId.GreaterThanEqual ||
340-
_token.id == TokenId.LessThan || _token.id == TokenId.LessThanEqual)
339+
_token.id == TokenId.GreaterThan || _token.id == TokenId.GreaterThanEqual ||
340+
_token.id == TokenId.LessThan || _token.id == TokenId.LessThanEqual)
341341
{
342342
var op = _token;
343343
NextToken();
@@ -424,7 +424,7 @@ private Expression ParseTypeTesting()
424424
{
425425
var left = ParseShift();
426426
while (_token.text == ParserConstants.KeywordIs
427-
|| _token.text == ParserConstants.KeywordAs)
427+
|| _token.text == ParserConstants.KeywordAs)
428428
{
429429
var typeOperator = _token.text;
430430

@@ -523,7 +523,7 @@ private Expression ParseMultiplicative()
523523
{
524524
var left = ParseUnary();
525525
while (_token.id == TokenId.Asterisk || _token.id == TokenId.Slash ||
526-
_token.id == TokenId.Percent)
526+
_token.id == TokenId.Percent)
527527
{
528528
var op = _token;
529529
NextToken();
@@ -1053,7 +1053,7 @@ private Expression ParseIdentifier()
10531053
return ParseMemberAccess(thisParameterExpression);
10541054
}
10551055
}
1056-
catch(ParseException)
1056+
catch (ParseException)
10571057
{
10581058
// ignore
10591059
}
@@ -1116,14 +1116,14 @@ private Expression ParseDefaultOperator()
11161116
private Expression GenerateConditional(Expression test, Expression expr1, Expression expr2, int errorPos)
11171117
{
11181118
if (IsDynamicExpression(test))
1119-
return GenerateConditionalDynamic(test, expr1, expr2,errorPos);
1119+
return GenerateConditionalDynamic(test, expr1, expr2, errorPos);
11201120

11211121
if (test.Type != typeof(bool))
11221122
throw ParseException.Create(errorPos, ErrorMessages.FirstExprMustBeBool);
11231123
if (expr1.Type != expr2.Type)
11241124
{
1125-
var expr1As2 = expr2 != ParserConstants.NullLiteralExpression ? ExpressionUtils.PromoteExpression(expr1, expr2.Type, true) : null;
1126-
var expr2As1 = expr1 != ParserConstants.NullLiteralExpression ? ExpressionUtils.PromoteExpression(expr2, expr1.Type, true) : null;
1125+
var expr1As2 = expr2 != ParserConstants.NullLiteralExpression ? ExpressionUtils.PromoteExpression(expr1, expr2.Type) : null;
1126+
var expr2As1 = expr1 != ParserConstants.NullLiteralExpression ? ExpressionUtils.PromoteExpression(expr2, expr1.Type) : null;
11271127
if (expr1As2 != null && expr2As1 == null)
11281128
{
11291129
expr1 = expr1As2;
@@ -1838,7 +1838,7 @@ private Expression ParseElementAccess(Expression expr)
18381838

18391839
for (int i = 0; i < args.Length; i++)
18401840
{
1841-
args[i] = ExpressionUtils.PromoteExpression(args[i], typeof(int), true);
1841+
args[i] = ExpressionUtils.PromoteExpression(args[i], typeof(int));
18421842
if (args[i] == null)
18431843
throw ParseException.Create(errorPos, ErrorMessages.InvalidIndex);
18441844
}
@@ -1959,8 +1959,6 @@ private Expression GenerateGreaterThan(Expression left, Expression right)
19591959
return GenerateBinary(ExpressionType.GreaterThan, left, right);
19601960
}
19611961

1962-
1963-
19641962
private Expression GenerateGreaterThanEqual(Expression left, Expression right)
19651963
{
19661964
if (left.Type == typeof(string))

src/DynamicExpresso.Core/Resolution/ExpressionUtils.cs

+8-16
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@ namespace DynamicExpresso.Resolution
77
{
88
internal static class ExpressionUtils
99
{
10-
public static Expression PromoteExpression(Expression expr, Type type, bool exact)
10+
public static Expression PromoteExpression(Expression expr, Type type)
1111
{
1212
if (expr.Type == type) return expr;
13-
if (expr is ConstantExpression)
13+
if (expr is ConstantExpression ce && ce == ParserConstants.NullLiteralExpression)
1414
{
15-
var ce = (ConstantExpression)expr;
16-
if (ce == ParserConstants.NullLiteralExpression)
17-
{
18-
if (type.ContainsGenericParameters)
19-
return null;
20-
if (!type.IsValueType || TypeUtils.IsNullableType(type))
21-
return Expression.Constant(null, type);
22-
}
15+
if (type.ContainsGenericParameters)
16+
return null;
17+
if (!type.IsValueType || TypeUtils.IsNullableType(type))
18+
return Expression.Constant(null, type);
2319
}
2420

2521
if (expr is InterpreterExpression ie)
@@ -40,13 +36,9 @@ public static Expression PromoteExpression(Expression expr, Type type, bool exac
4036
return Expression.Convert(expr, genericType);
4137
}
4238

43-
if (TypeUtils.IsCompatibleWith(expr.Type, type) || expr is DynamicExpression)
39+
if (TypeUtils.IsCompatibleWith(expr.Type, type))
4440
{
45-
if (type.IsValueType || exact)
46-
{
47-
return Expression.Convert(expr, type);
48-
}
49-
return expr;
41+
return Expression.Convert(expr, type);
5042
}
5143

5244
return null;

src/DynamicExpresso.Core/Resolution/MethodResolution.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq.Expressions;
43
using System.Linq;
4+
using System.Linq.Expressions;
55
using System.Reflection;
66
using System.Security;
7-
using System.Text;
87
using DynamicExpresso.Exceptions;
98
using DynamicExpresso.Parsing;
109
using DynamicExpresso.Reflection;
@@ -93,7 +92,7 @@ public static bool CheckIfMethodIsApplicableAndPrepareIt(MethodData method, Expr
9392
continue;
9493
}
9594

96-
var promoted = ExpressionUtils.PromoteExpression(currentArgument, parameterType, true);
95+
var promoted = ExpressionUtils.PromoteExpression(currentArgument, parameterType);
9796
if (promoted != null)
9897
{
9998
promotedArgs.Add(promoted);
@@ -111,7 +110,7 @@ public static bool CheckIfMethodIsApplicableAndPrepareIt(MethodData method, Expr
111110
continue;
112111
}
113112

114-
var promoted = ExpressionUtils.PromoteExpression(currentArgument, paramsArrayElementType, true);
113+
var promoted = ExpressionUtils.PromoteExpression(currentArgument, paramsArrayElementType);
115114
if (promoted != null)
116115
{
117116
paramsArrayPromotedArgument = paramsArrayPromotedArgument ?? new List<Expression>();

test/DynamicExpresso.UnitTest/GithubIssues.cs

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using DynamicExpresso.Exceptions;
2-
using NUnit.Framework;
31
using System;
42
using System.Collections;
53
using System.Collections.Generic;
4+
using System.Dynamic;
65
using System.Linq;
76
using System.Reflection;
87
using System.Text.RegularExpressions;
9-
using System.Dynamic;
8+
using DynamicExpresso.Exceptions;
9+
using NUnit.Framework;
1010

1111
// ReSharper disable SpecifyACultureInStringConversionExplicitly
1212

@@ -775,7 +775,9 @@ public void GitHub_Issue_292()
775775
}
776776

777777
[Test]
778-
public void GitHub_Issue_295() {
778+
[Ignore("The fix suggested in #296 break other use cases, so let's ignore this test for now")]
779+
public void GitHub_Issue_295()
780+
{
779781
var evaluator = new Interpreter();
780782

781783
// create path helper functions in expressions...
@@ -787,10 +789,10 @@ public void GitHub_Issue_295() {
787789
globalSettings.MyTestPath = "C:\\delme\\";
788790
evaluator.SetVariable("GlobalSettings", globalSettings);
789791

790-
var works = (string) evaluator.Eval("StringConcat((string)GlobalSettings.MyTestPath,\"test.txt\")");
792+
var works = (string)evaluator.Eval("StringConcat((string)GlobalSettings.MyTestPath,\"test.txt\")");
791793
Assert.That(works, Is.EqualTo("C:\\delme\\test.txt"));
792794

793-
var doesntWork = (string) evaluator.Eval("StringConcat(GlobalSettings.MyTestPath,\"test.txt\")");
795+
var doesntWork = (string)evaluator.Eval("StringConcat(GlobalSettings.MyTestPath,\"test.txt\")");
794796
Assert.That(doesntWork, Is.EqualTo("C:\\delme\\test.txt"));
795797
}
796798

@@ -848,6 +850,21 @@ public void GitHub_Issue_314()
848850
var exception2 = Assert.Throws<UnknownIdentifierException>(() => interpreter.Eval("b > 1"));
849851
Assert.AreEqual("b", exception2.Identifier);
850852
}
853+
854+
[Test]
855+
public void GitHub_Issue_325()
856+
{
857+
var options = InterpreterOptions.Default | InterpreterOptions.LateBindObject;
858+
var interpreter = new Interpreter(options);
859+
860+
var input = new
861+
{
862+
Prop1 = 4,
863+
};
864+
865+
var expressionDelegate = interpreter.ParseAsDelegate<Func<object, bool>>($"input.Prop1 == null", "input");
866+
Assert.IsFalse(expressionDelegate(input));
867+
}
851868
}
852869

853870
internal static class GithubIssuesTestExtensionsMethods

0 commit comments

Comments
 (0)