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

C# compiler should report why a delegate conversion failed #24675

Closed
gafter opened this issue Feb 6, 2018 · 0 comments
Closed

C# compiler should report why a delegate conversion failed #24675

gafter opened this issue Feb 6, 2018 · 0 comments
Assignees
Labels
3 - Working Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. Feature Request
Milestone

Comments

@gafter
Copy link
Member

gafter commented Feb 6, 2018

I was recently implementing support for dotnet/csharplang#98 and discovered a regression in the quality of diagnostic for the following situation

delegate void D();
class Program
{
    static void Main()
    {
        D d = new D(Program.Method);
    }
    void Method() {}
}

We used to report

error CS0120: An object reference is required for the non-static field, method, or property 'Program.Method()'

It used to be that Program.Method was a perfectly good method group that converts to the delegate type D with an error. However, under the new proposed rules for dotnet/csharplang#98 it remains a perfectly good method group that no longer converts to the delegate type D, as no method in the group survives the new overload resolution rules. The compiler, under the new rules, reports

error CS0123: No overload for 'Method' matches delegate 'D'

The compiler has enough information internally to know why overload resolution failed here (it is because "An object reference is required for the non-static field, method, or property 'Program.Method()'"). However, the C# compiler currently never reports the underlying reason for a method group conversion failure.

I propose that some or all causes of a method group conversion failure should be reported. That would restore the quality of diagnostic for this particular case.

@gafter gafter added Area-Compilers Feature Request Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. labels Feb 6, 2018
@gafter gafter self-assigned this Feb 6, 2018
@gafter gafter added this to the 16.0 milestone Feb 6, 2018
gafter added a commit to gafter/roslyn that referenced this issue Feb 10, 2018
- Refine candidate set based on static/instance receiver, or static context
- Refine candidate methods whose type parameter constraints are violated.
- For a method group conversion, remove methods with the wrong return type or ref mode.

Implements dotnet/csharplang#98

See also dotnet#24675 for a proposal to improve the quality of diagnostics for the method group conversion. This change occasionally exposes this opportunity for diagnostic improvement (search for references to dotnet#24675 in the source to find examples).
gafter added a commit to gafter/roslyn that referenced this issue Feb 12, 2018
gafter added a commit to gafter/roslyn that referenced this issue Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 - Working Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. Feature Request
Projects
None yet
Development

No branches or pull requests

1 participant