Bug 15505 - MCS doesn't resolve type inference correctly when given multiple covariant and contravariant interfaces with different type arguments
Summary: MCS doesn't resolve type inference correctly when given multiple covariant an...
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-10-18 17:31 UTC by Brett van Swelm
Modified: 2013-10-27 16:39 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 Brett van Swelm 2013-10-18 17:31:09 UTC
With "D:\Applications\Mono-3.0.9\bin" and
"C:\Windows\Microsoft.NET\Framework\v4.0.30319" in the path on my Windows 7
machine:

===== Test.cs =====
public class MyTestClass
{
    public interface ICovariant<out T> { }
    public interface IContravariant<in T> { }

    public class Base { }
    public class ChildA : Base { }
    public class ChildB : Base { }

    static void Test<T>(
        ICovariant<T> a,
        ICovariant<T> b,
        IContravariant<T> c) { }

    static void Main()
    {
        ICovariant<ChildA>   covChildA = null;
        ICovariant<ChildB>   covChildB = null;
        IContravariant<Base> conBase   = null;

        Test(covChildA, covChildB, conBase);
    }
}
===== end =====

$ csc Test.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.17929
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

$ mcs Test.cs
Test.cs(21,9): error CS0411: The type arguments for method `MyTestClass.Test<T>(MyTestClass.ICovariant<T>, MyTestClass.ICovariant<T>, MyTestClass.IContravariant<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly
Compilation failed: 1 error(s), 0 warnings

In inferring what type argument T is we should see that it must be "ChildA or parent" and "ChildB or parent" (due to the covariance) and also "Base or child" (due to the contravariance). The only possible inference is that T is Base.

Thanks,

Brett van Swelm | Senior Engineer
Coverity | 185 Berry Street | Suite 6500, Lobby 3 | San Francisco, CA 94107
The Leader in Development Testing
Read our profile in Forbes, Coverity Gets Code Right 25% Faster
Comment 1 Marek Safar 2013-10-27 16:39:09 UTC
Fixed in master