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

Infer delegate type from lambda expression with explicit return statements without expressions #56236

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/BoundTree/UnboundLambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ private static TypeWithAnnotations DelegateReturnTypeWithAnnotations(MethodSymbo
returnType = inferredReturnType.TypeWithAnnotations;
returnRefKind = inferredReturnType.RefKind;

if (!returnType.HasType && returnTypes.Count > 0)
if (!returnType.HasType && inferredReturnType.NumExpressions > 0)
Copy link
Contributor

@AlekseyTs AlekseyTs Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!returnType.HasType && inferredReturnType.NumExpressions > 0)

It looks like the change only affects scenarios that involve return statements without expressions. Is this correct? If so, consider adjusting commit's title/description as right now it feels too broad. #Closed

Copy link
Contributor

@AlekseyTs AlekseyTs Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inferredReturnType.NumExpressions > 0

It looks like all added tests are success tests. Do we have failure tests for this condition? Specifically when there is a mix of returns with and without an expression. #Closed

{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3804,6 +3804,51 @@ static void Main()
Assert.True(HaveMatchingSignatures(((INamedTypeSymbol)typeInfo.Type!).DelegateInvokeMethod!, method));
}

[WorkItem(55320, "https://github.com/dotnet/roslyn/issues/55320")]
[Fact]
public void InferredReturnType_01()
{
var source =
@"using System;
class Program
{
static void Main()
{
Report(() => { return; });
Report((bool b) => { if (b) return; });
Report((bool b) => { if (b) return; else return; });
}
static void Report(object obj) => Console.WriteLine(obj.GetType());
}";
CompileAndVerify(source, options: TestOptions.ReleaseExe, expectedOutput:
@"System.Action
System.Action`1[System.Boolean]
System.Action`1[System.Boolean]
");
}

[Fact]
public void InferredReturnType_02()
{
var source =
@"using System;
class Program
{
static void Main()
{
Report(async () => { return; });
Report(async (bool b) => { if (b) return; });
Report(async (bool b) => { if (b) return; else return; });
}
static void Report(object obj) => Console.WriteLine(obj.GetType());
}";
CompileAndVerify(source, options: TestOptions.ReleaseExe, expectedOutput:
@"System.Func`1[System.Threading.Tasks.Task]
System.Func`2[System.Boolean,System.Threading.Tasks.Task]
System.Func`2[System.Boolean,System.Threading.Tasks.Task]
");
}

[Fact]
public void TypeInference_Constraints_01()
{
Expand Down