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

Overload resolution for method groups and Action/Func<Task> overloads #26077

Closed
lt-gerjan opened this issue Apr 10, 2018 · 8 comments
Closed

Comments

@lt-gerjan
Copy link

lt-gerjan commented Apr 10, 2018

Version Used:
Latest

Steps to Reproduce:

    void LocalFunction(Action action) // this doesn't work
    {
        var capture = "Captured";
        void closure()
        {
            var local = capture;
            
            action();
        }
        
        Task.Run(closure); // error CS0121: The call is ambiguous between the following methods or properties: 'Task.Run(Action)' and 'Task.Run(Func<Task>)'
    }
    
    void ActionDelegate(Action action) // this works
    {
        var capture = "Captured";
        Action closure = () =>
        {
            var local = capture;
            
            action();
        };
        
        Task.Run(closure);
    } 

See:
https://sharplab.io/#v2:CYLg1APgAgDABFAjAbgLAChYMQVjZgZgQCY4BhODAbwwEgM5GEAWOAGQHsBjAQwBsoOAC4BLDgDsAFElI8uoiQEoGTGuiYa4ANx4AnOLwAOQgK66ApnAC8cAERkexs+eC38mplFZc+HAM7OksrqHnBqoZo6+r68fNYGjqYW7hFMKqlwcgpSiikeAL7pmkUaAPSlUABsAHQASiZSPv7OuXDlcA1cHAC23ebiQnBCABYifkWFIYxFXtjEACLmfOYA5jxC5tKIsvJi4sEa4aFRCU4W8faJzq55mjIGvgHnNkHWAHwlqp8eJzH88UYkuZbhFvposnsgiCmPlodMpncavVGo8Wrd8pQERh8kA

Expected Behavior:
I would expect that the two examples are identical and both should compile

Actual Behavior:
error CS0121: The call is ambiguous between the following methods or properties: 'Task.Run(Action)' and 'Task.Run(Func<Task>)'

What I am not getting is that the compiler says that is has to choose between Task.Run(Action) and Task.Run(Func<Task>) but my closure clearly has the same signature as an Action right? (no param, no return)

Edit: the fun thing is that Visual Studio (analyzer + fix) hints for using a local function, which results in a compiler error @kuhlenh

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Apr 10, 2018

tagging @agocke . IT does seem weird to have ambiguity, given that one of the overloads expect Func<Task>, and i don't see how void closure() would ever be considered for that...

@lt-gerjan
Copy link
Author

@CyrusNajmabadi Yes, Im not getting the ambiguity part of the error message

@lt-gerjan lt-gerjan changed the title Local function to Action (type inference / Local function to Action Apr 10, 2018
@agocke
Copy link
Member

agocke commented Apr 10, 2018

Doesn't look like this has anything to do with local functions:

using System.Threading.Tasks;
public class C 
{
    public void M() 
    {
        Task.Run(M2);
    }
    
    void M2()
    {

    }
}

Output: error CS0121: The call is ambiguous between the following methods or properties: 'Task.Run(Action)' and 'Task.Run(Func<Task>)'

Plain old overload resolution.

@agocke agocke changed the title Local function to Action Overload resolution for method groups and Action/Func<Task> overloads Apr 10, 2018
@agocke
Copy link
Member

agocke commented Apr 10, 2018

cc @gafter

@lt-gerjan
Copy link
Author

lt-gerjan commented Apr 10, 2018

@agocke Thanks, so is this considered as a bug or more a (known) design issue? (in the overload resolution space)

@agocke
Copy link
Member

agocke commented Apr 10, 2018

This appears to be a bug, since I don't think there's a conversion from void M() to Func<Task> but maybe I'm missing something in the spec. @gafter, what do you think?

@alrz
Copy link
Member

alrz commented Apr 10, 2018

Doesn't repro on master. worth to add a test maybe

@agocke
Copy link
Member

agocke commented Apr 10, 2018

It looks like this was part of the "bestest betterness" feature implemented for C# 7.3 (which is why it does not repro in master).

See https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.3/improved-overload-candidates.md for the specification, specifically number (3), which addresses this scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants