dotnet / dotnet/csharpstandard

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

Open
#1,072 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

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)

public class C
{
public static void M(Func 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](https://github.com/dotnet/csharpstandard/blob/draft-v8/standard/expressions.md#12647-better-conversion-target)) or 12.6.4.5 ([better conversion from expression](https://github.com/dotnet/csharpstandard/blob/draft-v8/standard/expressions.md#12645-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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.