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

Lambdas + custom delegate types + type inference = breaking change in C# 10 #57916

Closed
controlflow opened this issue Nov 22, 2021 · 4 comments
Closed
Labels
Area-Compilers Feature - Lambda Improvements untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@controlflow
Copy link

Version Used:

.NET 6.0 SDK RTM

Steps to Reproduce:

Compile the following code:

using System;

DataRules.AddRule(new Constant<MyDelegate>(), () => Function); // Error
DataRules.AddRule(new Constant<Func<string, int>>(), () => Function); // OK
DataRules.AddRule<MyDelegate>(new Constant<MyDelegate>(), () => Function); // OK

int Function(string s) => s.Length;

static class DataRules
{
  public static void AddRule<T>(Constant<T> constant, Func<T> factory) { }
  public static void AddRule<T>(Constant<T> constant, T value) { }
}

class Constant<T> { }
delegate int MyDelegate(string s);

Expected Behavior:

This code compiles in C# 9.0/.NET 5.0 SDK, all invocations are bound to AddRule<T>(Constant<T> constant, Func<T> factory) overload. I expect the same behavior in C# 10/.NET 6.0 SDK

Actual Behavior:

Failed build in C# 10/.NET 6.0 SDK:

CS1593 Delegate 'MyDelegate' does not take 0 arguments

If I understand correctly, the compiler should prefer overloads that don't infer "function type" for the type arguments.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 22, 2021
@controlflow
Copy link
Author

Probably related to #57713 (comment)

@svick
Copy link
Contributor

svick commented Nov 22, 2021

This looks like a duplicate of #57630, which has already been fixed. Indeed, when I build your code using a daily build (Microsoft.Net.Compilers.Toolset version 4.0.1-1.21569.2), it compiles successfully.

Edit: I see you already found the PR fixing that issue.

@cston
Copy link
Member

cston commented Nov 22, 2021

Thanks for reporting this issue @controlflow.

As @svick mentioned, this was also reported in #57630 and has been fixed.

@jcouv
Copy link
Member

jcouv commented Jan 4, 2022

@cston Should this issue be closed then?

@cston cston closed this as completed Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Lambda Improvements untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

4 participants