Bug 17755 - mcs incorrectly breaks tie between ambiguous methods
Summary: mcs incorrectly breaks tie between ambiguous methods
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2014-02-13 12:56 UTC by Richard Cook
Modified: 2014-02-25 13:17 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Richard Cook 2014-02-13 12:56:22 UTC
===== Test.cs =====
class C
  static void M(int x, string y, string z = null){}
  static void M<T>(T t, int? u, string z = null) {}
  static void Main() { M(123, null); }
===== End of Test.cs =====

This program should not compile due to the ambiguity between the two methods named "M". The correct behaviour is exhibited by csc, which gives an ambiguity error. The reasoning is:

* Type inference infers int for T.
* Both M(int, string, string) and M<int>(int, int?, string) are applicable. Which is better?
* Betterness looks at the effective parameter types, which are (int, string) and (int, int?)
* int is neither better nor worse than int.
* string is neither better nor worse than int?
* Therefore neither method is better than the other.

Can we go to the tie-breaking round?

"In case the parameter type sequences {P1, P2, …, PN} and {Q1, Q2, …, QN} are equivalent (i.e. each Pi has an identity conversion to the corresponding Qi), the following tie-breaking rules are applied..."

The parameter type sequences are not equivalent since string != int?. So the tie-breaking round does not apply. mcs's error is that it goes to the tie-breaking round if the last formal parameter is optional (in OverloadResolver.BetterFunction). The tie-breaking round chooses the non-generic method.

Here is output from csc and a recent build of mcs:

===== csc output =====
> csc /target:library Test.cs
Microsoft (R) Visual C# Compiler version 12.0.21005.1
Copyright (C) Microsoft Corporation. All rights reserved.

Test.cs(5,24): error CS0121: The call is ambiguous between the following methods or properties: 'C.M(int, string,
        string)' and 'C.M<int>(int, int?, string)'
===== End of csc output =====

===== mcs output =====
> mcs --version
Mono C# compiler version
> mcs /target:library Test.cs
[No errors]
===== End of mcs output =====

I have tested against five versions of mcs including and and all exhibit the same behaviour.

Thanks, Richard.

Richard Cook | Principal engineer
Coverity | Columbia Center Tower | 701 Fifth Avenue, Suite 1220 | Seattle, WA
The Leader in Development Testing
Read our profile in Forbes, Coverity Gets Code Right 25% Faster
Comment 1 Marek Safar 2014-02-20 14:35:07 UTC
Fixed in master