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

11.8 Method group conversions requires error even if the conversion isn't used #188

Closed
gafter opened this issue Mar 18, 2020 · 3 comments · Fixed by #263
Closed

11.8 Method group conversions requires error even if the conversion isn't used #188

gafter opened this issue Mar 18, 2020 · 3 comments · Fixed by #263
Assignees
Labels
status: gnarly This issue is likely to require very significant work to resolve type: bug The Standard does not describe the language as intended or implemented

Comments

@gafter
Copy link
Member

gafter commented Mar 18, 2020

Consider this little test program

public class Program1
{
    delegate void MyAction<T>(T x);
 
    void Y(long x) { }
 
    void D(MyAction<int> o, short s) { }
    void D(MyAction<long> o, int i) { }
 
    void T()
    {
        D(Y, 1);
    }
}

The first argument in the call D(Y, 1) is a method group. During overload resolution, we will seek the existence of a method group conversion to see which methods are applicable.

There is an implicit conversion from argument Y to the first parameter of the first D method, according to 11.8:

  • A conversion is considered to exist if the algorithm of §12.7.6.2 produces a single best method M having the same number of parameters as D.
  • Even if the conversion exists, a compile-time error occurs if the selected method M is not compatible (§20.4) with the delegate type D.

There certainly is a best method, as there is only one Y. But since Y is not compatible with the delegate type MyAction<int>, a compile-time error occurs. This is even though the compiler would not even try to apply that conversion, instead selecting the other overload of D as the one to be called!

Actual compilers do not report this compile-time error unless the conversion is attempted to be used. This test program compiles and runs.

@jskeet
Copy link
Contributor

jskeet commented Aug 19, 2020

This looks like it's worth doing, and may be reasonably small. It's possible that it's already fixed in a later version of C# (as overload resolution has definitely changed).

@jskeet jskeet transferred this issue from another repository Jan 12, 2021
@jskeet jskeet added status: gnarly This issue is likely to require very significant work to resolve type: bug The Standard does not describe the language as intended or implemented labels Jan 12, 2021
@jskeet
Copy link
Contributor

jskeet commented Jan 12, 2021

(Applied the "gnarly" label on general principle, as it's to do with overload resolution.)

@jskeet jskeet self-assigned this Oct 5, 2022
@jskeet
Copy link
Contributor

jskeet commented Oct 5, 2022

Note to self: see if #263 fixes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: gnarly This issue is likely to require very significant work to resolve type: bug The Standard does not describe the language as intended or implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants