[Mono-bugs] [Bug 691531] New: Weird variance and constraint bug.

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue May 3 16:57:49 EDT 2011



           Summary: Weird variance and constraint bug.
    Classification: Mono
           Product: Mono: Compilers
           Version: SVN
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: C#
        AssignedTo: msafar at novell.com
        ReportedBy: rkumpera at novell.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---
           Blocker: ---

using System;
using System.Collections.Generic;

public class Driver {
    static void Bla<T,U> () where T : U {
        T[] ta = null;
        IEnumerable<T> ita = ta;
        IEnumerable<U> itu = ita;

static void Main() {
    Bla<string, object> ();

There are 2 issues here:

-mcs compiles this to invalid code. IEnumerable<T> is not assignable to
-csc fails to compile the above code due to:

test.cs(16,24): error CS0266: Cannot implicitly convert type
        'System.Collections.Generic.IEnumerable<T>' to
        'System.Collections.Generic.IEnumerable<U>'. An explicit conversion
        exists (are you missing a cast?)

Looks like variance checks are generic constraint oblivious.

This this issue behind #687193.

What's more awesome is that this doesn't happen at reflection time:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Driver {
    static void Bla<T,U> () where T : U {
        var t = typeof (T[]);
        var it = typeof (IEnumerable<T>); 
        var iu = typeof (IEnumerable<U>);
        Console.WriteLine (it.IsAssignableFrom (t));
        Console.WriteLine (iu.IsAssignableFrom (t));
        Console.WriteLine (iu.IsAssignableFrom (it));

static void Main() {
    Bla<string, object> ();

This has to be statically respected since Bla<T,U> () where T : U can be
instantiated with <int,object> but IEnumerable<int> !> IEnumerable<object>.


Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

More information about the mono-bugs mailing list