Skip to content
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

null propagation: Fix Throw expression bug #1787

Merged
merged 1 commit into from
Feb 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,11 @@ protected Expression UnwrapNullableType(Expression expression, Type expectedType
return expression;
else if (expression.Type == typeof(Nullable<>).MakeGenericType(expectedType))
{
var tmp = Expression.Parameter(expression.Type);
var nreCtor = typeof(NullReferenceException).GetConstructor(new [] { typeof(string) })!;
return Expression.Block(new [] { tmp },
Expression.Assign(tmp, expression),
Expression.Condition(
Expression.Property(tmp, "HasValue"),
Expression.Property(tmp, "Value"),
Expression.Throw(Expression.New(nreCtor, Expression.Constant($"Binding expression '{formattedExpression}' of type '{expectedType}' has evaluated to null.")), expectedType)
)
return Expression.Call(
ThrowHelpers.UnwrapNullableMethod.MakeGenericMethod(expectedType),
expression,
Expression.Constant(formattedExpression, typeof(string)),
Expression.Constant(expectedType.FullName, typeof(string))
);
}
else
Expand Down Expand Up @@ -283,5 +279,17 @@ public static Expression PropagateNulls(Expression expr, Func<Expression, bool>
var v = new ExpressionNullPropagationVisitor(canBeNull);
return v.Visit(expr);
}


internal class ThrowHelpers
{
public static readonly MethodInfo ThrowNullReferenceMethod = typeof(ThrowHelpers).GetMethod(nameof(ThrowNullReference))!;
public static T ThrowNullReference<T>(string expression, string type) =>
throw new NullReferenceException($"Binding expression '{expression}' of type '{type}' has evaluated to null.");

public static readonly MethodInfo UnwrapNullableMethod = typeof(ThrowHelpers).GetMethod(nameof(UnwrapNullable))!;
public static T UnwrapNullable<T>(T? value, string expression, string type) where T : struct =>
value ?? ThrowNullReference<T>(expression, type);
}
}
}
4 changes: 2 additions & 2 deletions src/Tests/Binding/NullPropagationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private void TestExpression(Random rnd, Expression expression, ParameterExpressi
Func<TestViewModel[], object> compile(Expression e) =>
Expression.Lambda<Func<TestViewModel[], object>>(Expression.Convert(e, typeof(object)), parameter)
// .CompileFast();
.Compile(preferInterpretation: true);
.Compile(preferInterpretation: false);

var withNullChecks = compile(exprWithChecks);
var withoutNullChecks = compile(expr);
Expand Down Expand Up @@ -224,7 +224,7 @@ Func<TestViewModel[], object> compile(Expression e) =>
private object EvalExpression<T>(Expression<Func<T, object>> a, T val)
{
var nullChecked = ExpressionNullPropagationVisitor.PropagateNulls(a.Body, _ => true);
var d = a.Update(body: nullChecked, a.Parameters).Compile(preferInterpretation: true);
var d = a.Update(body: nullChecked, a.Parameters).Compile(preferInterpretation: false);
return d(val);
}

Expand Down
Loading