Merge pull request #19 from koenbeuk/issue-18

Don't rewrite cast expressions
This commit is contained in:
Koen
2022-04-07 00:15:23 +08:00
committed by GitHub
4 changed files with 54 additions and 16 deletions
@@ -136,11 +136,15 @@ namespace EntityFrameworkCore.Projectables.Generator
if (node.Parent is MemberAccessExpressionSyntax parentMemberAccessNode)
{
var targetSymbolInfo = _semanticModel.GetSymbolInfo(parentMemberAccessNode.Expression);
if (targetSymbolInfo.Symbol is { Kind: SymbolKind.Parameter or SymbolKind.NamedType })
if (targetSymbolInfo.Symbol is null)
{
rewrite = false;
}
if (targetSymbolInfo.Symbol?.ContainingType is not null)
else if (targetSymbolInfo.Symbol is { Kind: SymbolKind.Parameter or SymbolKind.NamedType })
{
rewrite = false;
}
else if (targetSymbolInfo.Symbol?.ContainingType is not null)
{
if (!_compilation.HasImplicitConversion(targetSymbolInfo.Symbol.ContainingType, _targetTypeSymbol) ||
!SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol))
@@ -0,0 +1,15 @@
using EntityFrameworkCore.Projectables;
using Projectables.Repro;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
public static class Projectables_Repro_SomeExtensions_AsSomeResult
{
public static System.Linq.Expressions.Expression<System.Func<global::Projectables.Repro.SomeEntity, string>> Expression()
{
return (global::Projectables.Repro.SomeEntity e) =>
((global::Projectables.Repro.SuperEntity)e).Superpower;
}
}
}
@@ -1,14 +0,0 @@
using EntityFrameworkCore.Projectables;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
public static class _SomeExtensions_Test
{
public static System.Linq.Expressions.Expression<System.Func<global::SomeFlag, bool>> Expression()
{
return (global::SomeFlag f) =>
f == global::SomeFlag.Foo;
}
}
}
@@ -1045,6 +1045,39 @@ public static class SomeExtensions
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task Cast()
{
var compilation = CreateCompilation(@"
using EntityFrameworkCore.Projectables;
namespace Projectables.Repro;
public class SuperEntity : SomeEntity
{
public string Superpower { get; set; }
}
public class SomeEntity
{
public int Id { get; set; }
}
public static class SomeExtensions
{
[Projectable]
public static string AsSomeResult(this SomeEntity e) => ((SuperEntity)e).Superpower;
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
#region Helpers
Compilation CreateCompilation(string source, bool expectedToCompile = true)