Bug 6056 - mcs Should not consider generic type constraints when searching for applicable function members for overloaded methods
Summary: mcs Should not consider generic type constraints when searching for applicabl...
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2012-07-09 22:07 UTC by cod3monk3y
Modified: 2012-07-10 10:08 UTC (History)
1 user (show)

Tags:
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:
Status:
RESOLVED FIXED

Description cod3monk3y 2012-07-09 22:07:26 UTC
Behavior between csc and mcs differs when resolving overloaded methods when a generic method with constraints is available. It appears that mcs is considering the generic type constraints when searching for applicable function members, but this is incorrect according to both the spec and Eric Lippert (see references below).

Consider this code:

using System;

class Bug
{
    static void Main(string[] args) {
        new Bug().Run();
    }
    
    void Run()
    {
        foo(1);
        bar(1);
    }
    
    void foo(int a, int b = 0) { print("A"); }
    void foo<T>(T t) { print("B"); }
    
    void bar(int a, int b=0) { print("X"); }
    void bar<T>(T t) where T : IFace { print("Y"); }
    
    interface IFace {
        void foo();
    }

    void print(string s) {
        Console.Write(s);
    }
}

Actual Behavior:

csc generates a compiler error:

"The type 'int' cannot be used as type parameter 'T' in the generic type or method 'Bug.bar<T>(T)'. There is no implicit reference conversion from 'int' to 'Bug.IFace'."

Actual Behavior:
mcs compiles and outputs "BX"

Expected Behavior:

mcs should match csc, generating a compiler error

Details and References:

Long discussion of bug discovery: http://stackoverflow.com/questions/11404544/c-sharp-monotouch-method-overload-with-generic-method-and-non-generic-method-wit

Eric Lippert "Constraints are not Part of the signature": http://blogs.msdn.com/b/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx

C# 4 Spec 7.5.3.2 - "Otherwise if all parameters of MP have a corresponding argument whereas default arguments need to be substituted for at least one optional parameter in MQ then MP is better than MQ."
Comment 1 Marek Safar 2012-07-10 10:08:50 UTC
Fixed in master