Bug 1905 - Generic parameters with constraints cannot be used as arguments (CS0314)
Summary: Generic parameters with constraints cannot be used as arguments (CS0314)
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2011-11-06 03:50 UTC by knittl89+bugs
Modified: 2011-11-07 12: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 knittl89+bugs 2011-11-06 03:50:18 UTC
This seems to be similar to the old bug [Bug 627488 - Generic constraints not (fully?) inherited on virtual generic method override](https://bugzilla.novell.com/show_bug.cgi?id=627488)

The source code in question can be found on stackoverflow: [No boxing or type parameter conversion for generic Type parameter with Mono](http://stackoverflow.com/q/8020053/112968)

I can confirm the suggested solution from stackoverflow, it compiles fine with mono 2.10.4. Changing all constraints from

    where T : ClientBase<I>, I

to
    where T : ClientBase<I>

fixes the problem, but I'm unsure if this will change behavior/functionality of the class.

I'm currently running bisect on mono.git to find the faulty commit. I'll let you know when I think I found the right one.
Comment 1 knittl89+bugs 2011-11-06 10:10:51 UTC
Here's the offending source code (minimal test case):

using System;

using System.ServiceModel;



public static class ClientFactory {

  public static T CreateClient<T, I>()

    /* error here */

    where T : ClientBase<I>, I

    where I : class {

    return CreateClient<T, I>(null, null);

  }



  public static T CreateClient<T, I>(string endpointConfigurationName, string remoteAddress)

    /* NO error here, this method compiles fine */

    where T : ClientBase<I>, I

    where I : class {



    T client;



    client = default(T);



    return client;

  } 

}
Comment 2 knittl89+bugs 2011-11-06 10:15:45 UTC
Self-contained code:

using System;

public static class ClientFactory {
	public class ClientBase<T> {
		
	}
	
	public static T CreateClient<T, I> ()
	/* error here */
	where T : ClientBase<I>, I
	where I : class {
		return CreateClient<T, I> (null, null);
	}

	public static T CreateClient<T, I> (string endpointConfigurationName, string remoteAddress)
	/* NO error here, this method compiles fine */
	where T : ClientBase<I>, I
	where I : class {

		T client;

		client = default(T);

		return client;
	} 
}
Comment 3 knittl89+bugs 2011-11-06 13:23:34 UTC
Reverting commit bf9a580867cd573a398ed5c3ea5668c57e5b9b9b (Better type parameter value type conversion checks) seems to fix it for me
Comment 4 Marek Safar 2011-11-07 12:39:13 UTC
Fixed in master