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

Better conversion from expression should prefer delegate types with non-void return type #1072

Open
jcouv opened this issue Apr 5, 2024 · 0 comments

Comments

@jcouv
Copy link
Member

jcouv commented Apr 5, 2024

The following code is disallowed by the spec, but allowed by the Microsoft 4.0 spec and the roslyn compiler.

using System;
C.M(null); // bound to M(Func<int>)

public class C
{
    public static void M(Func<int> func) {}
    public static void M(Action action) {}
}

In that version, the “Better conversion from Expression” contains this as part of the tie breakers to determine that D1 is better than D2:

  • E is an anonymous function, T1 is either a delegate type D1 or an expression tree type Expression, T2 is either a delegate type D2 or an expression tree type Expression and one of the following holds:
    • D1 is a better conversion target than D2.
    • D1 and D2 have identical parameter lists, and one of the following holds:
      • D1 has a return type Y1, and D2 has a return type Y2, an inferred return type X exists for E in the context of that parameter list, and the conversion from X to Y1 is better than the conversion from X to Y2.
      • D1 has a return type Y, and D2 is void returning.

That final bullet is no longer in 12.6.4.7 (better conversion target) or 12.6.4.5 (better conversion from expression). I wonder if it was dropped erroneously these rules were broken up between “exactly matching expression” and “Better conversion from expression”?

Thanks @BillWagner for digging up this reference.

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

No branches or pull requests

1 participant