Skip to content

Compile lambda for null access expressions #3157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@ public void StaticPropertyInstanceMethodCall()

Assert.AreEqual(expected, actual);
}

[Test]
public void NullableNullHasValue()
{
int? v = null;
var actual = GetValue(() => v.HasValue);
var expected = v.HasValue;

Assert.AreEqual(expected, actual);
}

[Test]
public void NullableNullGetValueOrDefault()
{
int? v = null;
var actual = GetValue(() => v.GetValueOrDefault());
var expected = v.GetValueOrDefault();

Assert.AreEqual(expected, actual);
}

[Test]
public void NullableNullValue()
{
int? v = null;
Expression<Func<int>> expression = () => v.Value;

Assert.Throws<InvalidCastException>(() => GetValue(expression));
Assert.Throws<InvalidCastException>(() => expression.Compile().Invoke());
}
Copy link
Member

@fredericDelaporte fredericDelaporte Sep 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about e => v.HasValue || e.SomeProperty = v.Value? I wonder if it is supported or if it would throw.

(There is not much value in supporting this, excepted "feature parity" with linq-to-object: the .Value is not needed in any way in such a query and can just be dropped. But still, it is better to know what how it behaves with NHibernate.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It throws on v.Value evaluation:

    NHibernate.HibernateException : Evaluation failure on null.Value
  ----> System.InvalidOperationException : Nullable object must have a value.

Maybe we shouldn't try to evaluate it in this context. But it's a different story not related to this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure, it would be beneficial if nullable.Value works (with the SQL parameter becoming false, true, or NULL). The value of supporting this would be that users don't have to remember to adapt their expression specially.

No idea what the effort would be to support it though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be beneficial if nullable.Value works

Just to clarify things. This PR fixes regression caused by optimizations made in #2948, #2957. nullableNullParam.HasValue or nullableNullParam.GetValueOrDefault() works in 5.3 but throws on master (5.4-dev).

nullable.Value fails the same in 5.3. So it's more like another feature request.


[Test]
public void StaticPropertyInstanceMultipleMethodCall()
Expand Down
11 changes: 9 additions & 2 deletions src/NHibernate/Impl/ExpressionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,25 @@ public static object FindValue(Expression expression)
return constantExpression.Value;
case ExpressionType.MemberAccess:
var memberExpression = (MemberExpression) expression;
var instance = findValue(memberExpression.Expression);
if (instance == null && memberExpression.Expression != null)
break;

switch (memberExpression.Member.MemberType)
{
case MemberTypes.Field:
return ((FieldInfo) memberExpression.Member).GetValue(findValue(memberExpression.Expression));
return ((FieldInfo) memberExpression.Member).GetValue(instance);
case MemberTypes.Property:
return ((PropertyInfo) memberExpression.Member).GetValue(findValue(memberExpression.Expression));
return ((PropertyInfo) memberExpression.Member).GetValue(instance);
}
break;
case ExpressionType.Call:
var methodCallExpression = (MethodCallExpression) expression;
var args = methodCallExpression.Arguments.ToArray(arg => FindValue(arg));
var callingObject = findValue(methodCallExpression.Object);
if (callingObject == null && methodCallExpression.Object != null)
break;

return methodCallExpression.Method.Invoke(callingObject, args);
case ExpressionType.Convert:
var unaryExpression = (UnaryExpression) expression;
Expand Down