Bug 23024 - Mono C# compiler rejects legal program with a bogus type inference error
Summary: Mono C# compiler rejects legal program with a bogus type inference error
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-09-15 16:34 UTC by emoriarty
Modified: 2014-09-18 08:56 UTC (History)
2 users (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 emoriarty 2014-09-15 16:34:05 UTC
Mono rejects the following program, giving errors on the last line:

---------------
Test File
---------------
using System;
class D<T>
{
    public void S<U, V>(Func<T, U> ftu, Func<T, U, V> ftuv) { }
}

class P
{
    static D<V> DByExample<V>(V v) { return new D<V>(); }
    static void M()
    {
        var danon = DByExample(new { q = 123 });
        danon.S(anon => "", (anon, str) => new { str });
    }
}

error CS0029: Cannot implicitly convert anonymous type to anonymous type
error CS1662: Cannot convert `lambda expression' to delegate type
`Func<anonymous type,string,anonymous type>' because some of the return types
in the block are not implicitly convertible to the delegate return type

This program is legal C#. Using { int q } as the anonymous type.

* "danon" is of type D< { int q } >, so T is anonymous type { int q } 
* We must deduce U and V in S.
* From the first argument we deduce that U is string.
* We now know that T is { int q } and U is string; therefore we can deduce that
V is { string str }.
* Now we have type arguments for U and V.


--------------------
CSC Command Line (From Visual Studio 2013 Developer Command Prompt)
--------------------
$ csc /t:library Test.cs
Microsoft (R) Visual C# Compiler version 12.0.21005.1
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

--------------------
Mono Command Line (3.8.0)
--------------------
$ mcs /t:library Test.cs
Test.cs(13,48): error CS0029: Cannot implicitly convert type `anonymous type [Test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]' to `anonymous type [Test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]'
Test.cs(13,48): error CS1662: Cannot convert `lambda expression' to delegate type `System.Func<anonymous type,string,anonymous type>' because some of the return types in the block are not implicitly convertible to the delegate return type
Compilation failed: 2 error(s), 0 warnings


Edward Moriarty | Junior Engineer
Coverity | The Leader in Development Testing
Comment 1 Marek Safar 2014-09-18 08:56:09 UTC
Fixed in master