From d1090ef57e66c531e43731c3ccf50ad05d30ddd7 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Thu, 19 Nov 2020 13:39:47 -0800 Subject: [PATCH] Annotate IsNiladic for nullability on Arguments --- src/EFCore.Relational/Query/QuerySqlGenerator.cs | 2 +- .../Query/SqlExpressions/SqlFunctionExpression.cs | 8 +++++--- src/EFCore.Relational/Query/SqlNullabilityProcessor.cs | 5 ++--- .../SearchConditionConvertingExpressionVisitor.cs | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/EFCore.Relational/Query/QuerySqlGenerator.cs b/src/EFCore.Relational/Query/QuerySqlGenerator.cs index 9f159d7752b..91949ff4cf4 100644 --- a/src/EFCore.Relational/Query/QuerySqlGenerator.cs +++ b/src/EFCore.Relational/Query/QuerySqlGenerator.cs @@ -292,7 +292,7 @@ protected override Expression VisitSqlFunction(SqlFunctionExpression sqlFunction if (!sqlFunctionExpression.IsNiladic) { _relationalCommandBuilder.Append("("); - GenerateList(sqlFunctionExpression.Arguments!, e => Visit(e)); + GenerateList(sqlFunctionExpression.Arguments, e => Visit(e)); _relationalCommandBuilder.Append(")"); } diff --git a/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs b/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs index 6f5855a5c4f..8f360cf2ec4 100644 --- a/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs +++ b/src/EFCore.Relational/Query/SqlExpressions/SqlFunctionExpression.cs @@ -9,6 +9,7 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.EntityFrameworkCore.Utilities; +using CA = System.Diagnostics.CodeAnalysis; #nullable enable namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions @@ -22,7 +23,6 @@ namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions /// not used in application code. /// /// - // TODO - NULLABLE for C# 9 (MemberNotNullIf on IsNiladic) public class SqlFunctionExpression : SqlExpression { /// @@ -231,6 +231,7 @@ private SqlFunctionExpression( /// /// A bool value indicating if the function is niladic. /// + [CA.MemberNotNullWhen(false, nameof(Arguments), nameof(ArgumentsPropagateNullability))] public virtual bool IsNiladic { get; } /// @@ -276,7 +277,7 @@ protected override Expression VisitChildren(ExpressionVisitor visitor) SqlExpression[]? arguments = default; if (!IsNiladic) { - arguments = new SqlExpression[Arguments!.Count]; + arguments = new SqlExpression[Arguments.Count]; for (var i = 0; i < arguments.Length; i++) { arguments[i] = (SqlExpression)visitor.Visit(Arguments[i]); @@ -367,7 +368,7 @@ protected override void Print(ExpressionPrinter expressionPrinter) if (!IsNiladic) { expressionPrinter.Append("("); - expressionPrinter.VisitCollection(Arguments!); + expressionPrinter.VisitCollection(Arguments); expressionPrinter.Append(")"); } } @@ -381,6 +382,7 @@ public override bool Equals(object? obj) private bool Equals(SqlFunctionExpression sqlFunctionExpression) => base.Equals(sqlFunctionExpression) + && IsNiladic == sqlFunctionExpression.IsNiladic && Name == sqlFunctionExpression.Name && Schema == sqlFunctionExpression.Schema && ((Instance == null && sqlFunctionExpression.Instance == null) diff --git a/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs b/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs index aca2234f270..ae66ec72efb 100644 --- a/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs +++ b/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs @@ -1002,7 +1002,7 @@ protected virtual SqlExpression VisitSqlFunction( return sqlFunctionExpression.Update(instance, sqlFunctionExpression.Arguments); } - var arguments = new SqlExpression[sqlFunctionExpression.Arguments!.Count]; + var arguments = new SqlExpression[sqlFunctionExpression.Arguments.Count]; for (var i = 0; i < arguments.Length; i++) { arguments[i] = Visit(sqlFunctionExpression.Arguments[i], out _); @@ -1687,8 +1687,7 @@ private SqlExpression ProcessNullNotNull(SqlUnaryExpression sqlUnaryExpression, nullabilityPropagationElements.Add(sqlFunctionExpression.Instance); } - if (sqlFunctionExpression.Arguments != null - && sqlFunctionExpression.ArgumentsPropagateNullability != null) + if (!sqlFunctionExpression.IsNiladic) { for (var i = 0; i < sqlFunctionExpression.Arguments.Count; i++) { diff --git a/src/EFCore.SqlServer/Query/Internal/SearchConditionConvertingExpressionVisitor.cs b/src/EFCore.SqlServer/Query/Internal/SearchConditionConvertingExpressionVisitor.cs index cb64d328a74..36fe2dc7adc 100644 --- a/src/EFCore.SqlServer/Query/Internal/SearchConditionConvertingExpressionVisitor.cs +++ b/src/EFCore.SqlServer/Query/Internal/SearchConditionConvertingExpressionVisitor.cs @@ -453,7 +453,7 @@ protected override Expression VisitSqlFunction(SqlFunctionExpression sqlFunction SqlExpression[]? arguments = default; if (!sqlFunctionExpression.IsNiladic) { - arguments = new SqlExpression[sqlFunctionExpression.Arguments!.Count]; + arguments = new SqlExpression[sqlFunctionExpression.Arguments.Count]; for (var i = 0; i < arguments.Length; i++) { arguments[i] = (SqlExpression)Visit(sqlFunctionExpression.Arguments[i]);