Skip to content

Commit dbb6bd1

Browse files
committed
Checks for ternary and coalesce operators
1 parent e89d411 commit dbb6bd1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Orm/Xtensive.Orm/Orm/Linq/Translator.Expressions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,23 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
190190
: Visit(binaryExpression.Right);
191191
}
192192
}
193+
else if (memberType == MemberType.Entity || memberType == MemberType.Structure) {
194+
if (binaryExpression.NodeType == ExpressionType.Coalesce) {
195+
if ((context.Evaluator.CanBeEvaluated(binaryExpression.Right) && !(binaryExpression.Right is ConstantExpression))
196+
|| (context.Evaluator.CanBeEvaluated(binaryExpression.Left) && !(binaryExpression.Left is ConstantExpression)))
197+
throw new NotSupportedException(
198+
string.Format("Coalesce expressions with constant values of {0} type are not supported", memberType.ToString()));
199+
200+
return Visit(Expression.Condition(
201+
Expression.NotEqual(binaryExpression.Left, Expression.Constant(null)),
202+
binaryExpression.Left,
203+
binaryExpression.Right));
204+
}
205+
else {
206+
left = Visit(binaryExpression.Left);
207+
right = Visit(binaryExpression.Right);
208+
}
209+
}
193210
else {
194211
left = Visit(binaryExpression.Left);
195212
right = Visit(binaryExpression.Right);
@@ -216,6 +233,19 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
216233
return resultBinaryExpression;
217234
}
218235

236+
protected override Expression VisitConditional(ConditionalExpression c)
237+
{
238+
var memberType = c.IfTrue.Type == typeof(object)
239+
? c.IfFalse.GetMemberType()
240+
: c.IfTrue.GetMemberType();
241+
if (memberType == MemberType.Entity || memberType == MemberType.Structure) {
242+
if ((context.Evaluator.CanBeEvaluated(c.IfFalse) && !(c.IfFalse is ConstantExpression))
243+
|| (context.Evaluator.CanBeEvaluated(c.IfTrue) && !(c.IfTrue is ConstantExpression)))
244+
throw new NotSupportedException(string.Format("Conditional expressions with constant values of {0} type are not supported.", memberType.ToString()));
245+
}
246+
return base.VisitConditional(c);
247+
}
248+
219249
private Expression ConvertEnum(Expression left)
220250
{
221251
var underlyingType = Enum.GetUnderlyingType(left.Type.StripNullable());

0 commit comments

Comments
 (0)