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

Query: Avoid incorrect cast when simplifying null check #26004

Merged
merged 1 commit into from
Sep 14, 2021
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
17 changes: 16 additions & 1 deletion src/EFCore/Query/Internal/NullCheckRemovingExpressionVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
Expand Down Expand Up @@ -65,6 +66,20 @@ protected override Expression VisitConditional(ConditionalExpression conditional
? conditionalExpression.IfFalse
: conditionalExpression.IfTrue;

if (accessOperation is UnaryExpression outerUnary
&& (outerUnary.NodeType == ExpressionType.Convert
|| outerUnary.NodeType == ExpressionType.ConvertChecked)
&& accessOperation.Type.IsNullableType()
&& accessOperation.Type.UnwrapNullableType() == outerUnary.Operand.Type
&& outerUnary.Operand is UnaryExpression innerUnary
&& (innerUnary.NodeType == ExpressionType.Convert
|| innerUnary.NodeType == ExpressionType.ConvertChecked))
{
// If expression is of type Convert(Convert(a, type), type?)
// then we convert it to Convert(a, type?) since a can be nullable after removing check
accessOperation = outerUnary.Update(innerUnary.Operand);
}

if (_nullSafeAccessVerifyingExpressionVisitor.Verify(caller, accessOperation))
{
return accessOperation;
Expand Down Expand Up @@ -153,7 +168,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)

var operand = Visit(unaryExpression.Operand);
if ((unaryExpression.NodeType == ExpressionType.Convert
|| unaryExpression.NodeType == ExpressionType.ConvertChecked)
|| unaryExpression.NodeType == ExpressionType.ConvertChecked)
&& _nullSafeAccesses.Contains(operand))
{
_nullSafeAccesses.Add(unaryExpression);
Expand Down
2 changes: 0 additions & 2 deletions src/Microsoft.Data.Sqlite.Core/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.



// ReSharper disable once CheckNamespace
namespace System
{
Expand Down
24 changes: 24 additions & 0 deletions test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ public virtual async Task Comparing_byte_column_to_enum_in_vb_creating_double_ca
: query.ToList();
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Null_check_removal_in_ternary_maintain_appropriate_cast(bool async)
{
var contextFactory = await InitializeAsync<Context21770>();
using var context = contextFactory.CreateContext();

var query = from f in context.Food
select new
{
Bar = f.Taste != null ? (Taste)f.Taste : (Taste?)null
};

var bitterFood = async
? await query.ToListAsync()
: query.ToList();
}

protected class Context21770 : DbContext
{
public Context21770(DbContextOptions options)
Expand All @@ -194,6 +212,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
new IceCream { IceCreamId = 2, Name = "Chocolate", Taste = (byte)Taste.Sweet },
new IceCream { IceCreamId = 3, Name = "Match", Taste = (byte)Taste.Bitter });
});

modelBuilder.Entity<Food>(
entity =>
{
entity.HasData(new Food { Id = 1, Taste = null });
});
}
}

Expand Down