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

Support inline expression with casting #1419

Merged
merged 3 commits into from
Aug 5, 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
37 changes: 37 additions & 0 deletions src/Riok.Mapperly/Descriptors/InlineExpressionRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,43 @@ public override SyntaxNode VisitTypeArgumentList(TypeArgumentListSyntax node)
return base.VisitMemberAccessExpression(node);
}

public override SyntaxNode? VisitCastExpression(CastExpressionSyntax node)
{
var result = base.VisitCastExpression(node);

if (
CanBeInlined
&& result is CastExpressionSyntax typedResult
&& semanticModel.GetSymbolInfo(node.Type).Symbol is ITypeSymbol namedTypeSymbol
)
{
var fullyQualifiedType = FullyQualifiedIdentifier(namedTypeSymbol);

return typedResult.WithType(fullyQualifiedType.WithTriviaFrom(typedResult.Type));
}

return result;
}

public override SyntaxNode? VisitBinaryExpression(BinaryExpressionSyntax node)
{
var result = base.VisitBinaryExpression(node);

if (
CanBeInlined
&& result is BinaryExpressionSyntax typedResult
&& typedResult.IsKind(SyntaxKind.AsExpression)
&& semanticModel.GetSymbolInfo(node.Right).Symbol is ITypeSymbol namedTypeSymbol
)
{
var fullyQualifiedType = FullyQualifiedIdentifier(namedTypeSymbol);

return typedResult.WithRight(fullyQualifiedType.WithTriviaFrom(typedResult.Right));
}

return result;
}

public override SyntaxNode VisitImplicitObjectCreationExpression(ImplicitObjectCreationExpressionSyntax node)
{
if (
Expand Down
98 changes: 98 additions & 0 deletions test/Riok.Mapperly.Tests/Helpers/InlineExpressionRewriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,104 @@ public static string FooBar(string s)
result.Should().Be(inlinedExpression ?? expression);
}

[Fact]
public void RewriteExpressionContainingCasting()
{
var (result, inlineOk) = Rewrite(
"""
using AnotherAssembly;

public class Test
{
public bool MapExpression(int value) => ((MyEnum) value & MyEnum.OptionA) > 0;
}

namespace AnotherAssembly {
enum MyEnum
{
OptionA = 1,
OptionB = 2,
}
}
"""
);

inlineOk.Should().BeTrue();
result.Should().Be("((global::AnotherAssembly.MyEnum) value & global::AnotherAssembly.MyEnum.OptionA) > 0");
}

[Fact]
public void RewriteExpressionContainingBinaryAnd()
{
var (result, inlineOk) = Rewrite(
"""
using AnotherAssembly;

public class Test
{
public bool MapExpression(MyEnum value) => (value & MyEnum.OptionA) > 0;
}

namespace AnotherAssembly {
enum MyEnum
{
OptionA = 1,
OptionB = 2,
}
}
"""
);

inlineOk.Should().BeTrue();
result.Should().Be("(value & global::AnotherAssembly.MyEnum.OptionA) > 0");
}

[Fact]
public void RewriteExpressionContainingAsStatement()
{
var (result, inlineOk) = Rewrite(
"""
using AnotherAssembly;

public class Test
{
public int MapExpression(A value) => (value as B).Value;
}

namespace AnotherAssembly {
record A;
record B(int Value) : A;
}
"""
);

inlineOk.Should().BeTrue();
result.Should().Be("(value as global::AnotherAssembly.B).Value");
}

[Fact]
public void RewriteExpressionContainingAsStatementAndCasting()
{
var (result, inlineOk) = Rewrite(
"""
using AnotherAssembly;

public class Test
{
public string MapExpression(A value) => (string)(object)(value as B).Value;
}

namespace AnotherAssembly {
record A;
record B(string Value) : A;
}
"""
);

inlineOk.Should().BeTrue();
result.Should().Be("(string)(object)(value as global::AnotherAssembly.B).Value");
}

private (string Result, bool CanBeInlined) Rewrite([StringSyntax(StringSyntax.CSharp)] string source)
{
var compilation = TestHelper.BuildCompilation(source);
Expand Down