From e4dbaf0831c3e761821814039856c65adeac614d Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Thu, 19 Nov 2026 19:40:42 +0000 Subject: [PATCH] fix: fully qualify explicit lambda return types in inlined expressions A lambda with an explicit return type, such as `.Select(DateTime? (x) => x.DateCreated)`, kept its return type as written when the user-implemented mapping was inlined into a projection. The generated code has no usings, so the type did not resolve. Co-Authored-By: Claude --- .../Descriptors/InlineExpressionRewriter.cs | 12 ++++++ .../Helpers/InlineExpressionRewriterTest.cs | 38 +++++++++++++++++++ .../QueryableProjectionUserImplementedTest.cs | 18 +++++++++ ...ithExplicitReturnType#Mapper.g.verified.cs | 20 ++++++++++ 4 files changed, 88 insertions(+) create mode 100644 test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionUserImplementedTest.ClassToClassUserImplementedLambdaWithExplicitReturnType#Mapper.g.verified.cs diff --git a/src/Riok.Mapperly/Descriptors/InlineExpressionRewriter.cs b/src/Riok.Mapperly/Descriptors/InlineExpressionRewriter.cs index 244cb120ee..0613185f4d 100644 --- a/src/Riok.Mapperly/Descriptors/InlineExpressionRewriter.cs +++ b/src/Riok.Mapperly/Descriptors/InlineExpressionRewriter.cs @@ -146,6 +146,18 @@ public override SyntaxNode VisitTypeOfExpression(TypeOfExpressionSyntax node) return node; } + public override SyntaxNode? VisitParenthesizedLambdaExpression(ParenthesizedLambdaExpressionSyntax node) + { + // the lambda symbol keeps the nullable annotation of an explicit return type, + // the type info of the return type syntax does not. + var returnType = node.ReturnType == null ? null : (semanticModel.GetSymbolInfo(node).Symbol as IMethodSymbol)?.ReturnType; + node = (ParenthesizedLambdaExpressionSyntax)base.VisitParenthesizedLambdaExpression(node)!; + if (returnType == null || node.ReturnType == null) + return node; + + return node.WithReturnType(FullyQualifiedIdentifier(returnType).WithTriviaFrom(node.ReturnType)); + } + public override SyntaxNode? VisitInvocationExpression(InvocationExpressionSyntax node) { // expand static method invocation type names diff --git a/test/Riok.Mapperly.Tests/Helpers/InlineExpressionRewriterTest.cs b/test/Riok.Mapperly.Tests/Helpers/InlineExpressionRewriterTest.cs index d7288cd596..c1352278ab 100644 --- a/test/Riok.Mapperly.Tests/Helpers/InlineExpressionRewriterTest.cs +++ b/test/Riok.Mapperly.Tests/Helpers/InlineExpressionRewriterTest.cs @@ -184,6 +184,44 @@ record A; result.ShouldBe("global::System.Array.Empty()"); } + [Theory] + [InlineData("DateTime? (x) => x.DateCreated", "global::System.DateTime? (x) => x.DateCreated")] + [InlineData("B (x) => x.Value", "global::AnotherAssembly.B (x) => x.Value")] + [InlineData("B? (x) => x.Value", "global::AnotherAssembly.B? (x) => x.Value")] + [InlineData("List (x) => x.Values", "global::System.Collections.Generic.List (x) => x.Values")] + [InlineData("B[] (x) => x.Values.ToArray()", "global::AnotherAssembly.B[] (x) => x.Values.ToArray()")] + [InlineData( + "List (x) => x.NullableValues", + "global::System.Collections.Generic.List (x) => x.NullableValues" + )] + [InlineData("object (x) => (B)x.Value", "object (x) => (global::AnotherAssembly.B)x.Value")] + [InlineData("(x) => x.DateCreated", "(x) => x.DateCreated")] + public void RewriteExpressionContainingLambdaWithExplicitReturnType(string lambda, string inlinedLambda) + { + var (result, inlineOk) = Rewrite( + $$""" + #nullable enable + using AnotherAssembly; + using System; + using System.Collections.Generic; + using System.Linq; + + public class Test + { + public int MapExpression(List values) => values.Select({{lambda}}).Count(); + } + + namespace AnotherAssembly { + record A(DateTime DateCreated, B Value, List Values, List NullableValues); + record B; + } + """ + ); + + inlineOk.ShouldBeTrue(); + result.ShouldBe($"global::System.Linq.Enumerable.Count(global::System.Linq.Enumerable.Select(values, {inlinedLambda}))"); + } + private (string Result, bool CanBeInlined) Rewrite([StringSyntax(StringSyntax.CSharp)] string source) { var compilation = TestHelper.BuildCompilation(source); diff --git a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionUserImplementedTest.cs b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionUserImplementedTest.cs index ec0ba7c3e0..51e8df93c1 100644 --- a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionUserImplementedTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionUserImplementedTest.cs @@ -171,6 +171,24 @@ private List Order(List v) return TestHelper.VerifyGenerator(source); } + [Fact] + public Task ClassToClassUserImplementedLambdaWithExplicitReturnType() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + """ + private partial System.Linq.IQueryable Map(System.Linq.IQueryable source); + + private DateTime? LastCreated(List v) + => v.Select(DateTime? (x) => x.DateCreated).Max(); + """, + "class A { public string StringValue { get; set; } public List Created { get; set; } }", + "class B { public string StringValue { get; set; } public DateTime? Created { get; set; } }", + "class C { public DateTime DateCreated { get; set; } }" + ); + + return TestHelper.VerifyGenerator(source); + } + [Fact] public Task ClassToClassUserImplementedOrderingWithMappingAndParameterHiding() { diff --git a/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionUserImplementedTest.ClassToClassUserImplementedLambdaWithExplicitReturnType#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionUserImplementedTest.ClassToClassUserImplementedLambdaWithExplicitReturnType#Mapper.g.verified.cs new file mode 100644 index 0000000000..0af4297683 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/QueryableProjectionUserImplementedTest.ClassToClassUserImplementedLambdaWithExplicitReturnType#Mapper.g.verified.cs @@ -0,0 +1,20 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] + private partial global::System.Linq.IQueryable Map(global::System.Linq.IQueryable source) + { +#nullable disable + return global::System.Linq.Queryable.Select( + source, + x => new global::B() + { + StringValue = x.StringValue, + Created = global::System.Linq.Enumerable.Max(global::System.Linq.Enumerable.Select(x.Created, global::System.DateTime? (x1) => x1.DateCreated)), + } + ); +#nullable enable + } +} \ No newline at end of file