Open AlekseyTs opened 6 months ago
CC @cston
CC @jjonescz
It looks like the behavior was introduced in https://github.com/dotnet/roslyn/pull/64861. There could be a similar issue around optional parameters as well. Basically any aspect that is not considered as a difference by MemberSignatureComparer.MethodGroupSignatureComparer
is a suspect.
I think the expected behavior here are errors:
var x1 = new Program().Test1; // error CS8917: The delegate type could not be inferred.
var x2 = new Program().Test2; // error CS8917: The delegate type could not be inferred.
This is what also happens with ref
instead of params
, for example. And it matches the speclets:
A method group has a natural type if all candidate methods in the method group have a common signature. (If the method group may include extension methods, the candidates include the containing type and all extension method scopes.)
The natural type of an anonymous function expression or method group is a function_type. A function_type represents a method signature: the parameter types, default values, ref kinds,
params
modifiers, and return type and ref kind. Anonymous function expressions or method groups with the same signature have the same function_type.
I.e., the candidate methods differ in signature, hence the method group has no natural type and CS8917 should be reported.
@jjonescz
I think params
modifier and default values are not part of the signature from the language point of view. One cannot overload on them. In my opinion, the fact that function_type mentions them doesn't automatically make them part of the signature for the purpose of the "if all candidate methods in the method group have a common signature" part. I think what you are proposing to do is a viable option, but, judging by the quotes above, that would require a spec change for the feature.
Observed: Type inferred for
x1
isAction<long[]>
, type inferred forx2
isvoid <anonymous delegate>(params long[] a)
Expected: The same type is inferred, they differ only by order of candidates.