Bugzilla – Bug 322682
[GMCS] Constraints do not propagate correctly in derived generic methods
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by ben@joldersma.org 2006-11-19 13:43:24 MST ---- Description of Problem: When trying to use a generic type in a derived generic method that is constrained in the base method, it is not possible to perform at least two expected kinds of operations, both related to constraint specifications. The first is that if the base method is constrained with the public parameterless constructor (aka 'new()') constraint, it is not possible to use that type in the derived method to call another generic method with the 'new()' constraint. It is possible to initialize a variable of the type T and assign to the public parameterless constructor. The second bug I am seeing, probably the same bug, is that if you have a class constraint (say class Foo, with public property X) in the base method, you cannot access the property in derived method (although you can if the constraint is declared in the calling method, rather than a derived one.) Steps to reproduce the problem: Try to compile the following code using the mono 1.2 compiler (I am compiling inside of the MonoDevelop that is included with the mono 1.2 generic linux install) //BEGIN CODE FILE using System; namespace testConstraintsVirtuals { class MainClass { public static void Main(string[] args) { Console.WriteLine("Hello World!"); } } class Foo { public int X; } abstract class Base { public abstract void method<T>() where T : Foo, new(); } class Derived : Base { // the first method signature is the ideal one. this one compiles with ms csc. it's what you'd expect to do. // in mono 1.1.17, you could remove the override, and specify constraints, and it would compile, now no longer. // gives error: The type `T' must have a public parameterless constructor in order to use it as parameter `T' in the generic type or method `testConstraintsVirtuals.Derived.method2<T>()'(CS0310) public override void method<T>() // public override void method<T>() where T : new() // gives error: Cannot specify constraints for overrides or explicit interface implementation methods // public void method<T>() where T : new() // gives error: testConstraintsVirtuals.Derived.method<T>()' hides inherited abstract member // new public void method<T>() where T : new() // gives error: testConstraintsVirtuals.Derived.method<T>()' hides inherited abstract member { method2<T>(); // cannot see that T is constrained to new() T t = new T(); //works as expected System.Console.WriteLine(t.X); //cannot see that T is constrained to Foo. } public void method2<T>() where T : Foo, new() { T t = new T(); //works as expected System.Console.WriteLine(t.X); //works as expected } } } //END FILE Actual Results: compiler gives these errors: Line 39: The type `T' must have a public parameterless constructor in order to use it as parameter `T' in the generic type or method `testConstraintsVirtuals.Derived.method2<T>()'(CS0310) and Line 41: `T' does not contain a definition for `X'(CS0117) Expected Results: to successfully compile, and correctly inherit the constraints. How often does this happen? every time. Additional Information: NA ---- Additional Comments From martin@ximian.com 2007-03-15 14:45:01 MST ---- Fixed in SVN. Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>Ubuntu Edgy (6.10)</cf_op_sys_details> Unknown operating system unknown. Setting to default OS "Other".