[Mono-bugs] [Bug 79984][Wis] New - Constraints do not propagate correctly in derived generic methods

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Sun Nov 19 13:43:24 EST 2006


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by ben at joldersma.org.

http://bugzilla.ximian.com/show_bug.cgi?id=79984

--- shadow/79984	2006-11-19 13:43:24.000000000 -0500
+++ shadow/79984.tmp.21990	2006-11-19 13:43:24.000000000 -0500
@@ -0,0 +1,123 @@
+Bug#: 79984
+Product: Mono: Compilers
+Version: 1.2
+OS: 
+OS Details: Ubuntu Edgy (6.10)
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Wishlist
+Component: C#
+AssignedTo: rharinath at novell.com                            
+ReportedBy: ben at joldersma.org               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Constraints do not propagate correctly in derived generic methods
+
+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


More information about the mono-bugs mailing list