Bugzilla – Bug 319013
[GMCS] CS0030: "this" is regarded as never be compatible with generic type parameter
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by atsushi@ximian.com 2005-09-16 16:48:37 MST ---- The example below fails to compile because "this" is regarded as incompatible with T. However T might be Test so it should pass. using System.Collections.Generic; class Test { public IEnumerable<T> Foo <T> (IEnumerator<object> e) { List<T> l = new List<T> (); if (this is T) l.Add ((T) this); return l; } } Actual Results: $ !gmcs gmcs generic-t.cs generic-t.cs(8,12): warning CS0184: The given expression is never of the provided (`T') type generic-t.cs(9,12): error CS0030: Cannot convert type 'Test' to 'T' Compilation failed: 1 error(s), 1 warnings Expected Results: No CS0030 error. How often does this happen? consistently. Additional Information: MS csc also fails to compile it (the same CS0030 error). Here are the corresponding sections from C# 2.1 spec. I think this "T" is an open type. -------- 14.9.10 is operator The is operator is used to dynamically check if the run-time type of an object is compatible with a given type. The result of the operation e is T, where e is an expression and T is a type, is a Boolean value indicating whether e can successfully be converted to type T by a reference conversion, a boxing conversion, or an unboxing conversion. The operation is evaluated as follows: • If the compile-time type of e is the same as T, or if an implicit reference conversion (§13.1.4) or boxing conversion (§13.1.5) exists from the compile-time type of e to T: o If e is of a reference type, the result of the operation is equivalent to evaluating e != null. o If e is of a value type, the result of the operation is true. • Otherwise, if an explicit reference conversion (§13.2.3) or unboxing conversion (§13.2.4) exists from the compile-time type of e to T, a dynamic type check is performed: o If the value of e is null, the result is false. o Otherwise, let R be the run-time type of the instance referenced by e. If R and T are the same type, if R is a reference type and an implicit reference conversion from R to T exists, or if R is a value type and T is an interface type that is implemented by R, the result is true. o Otherwise, the result is false. • Otherwise, if the compile-time type of e or T is an open type (§25.5.2), a dynamic type check is performed. -------- 25.5.2 Open and closed types All types can be classified as open types or closed types. An open type is a type that involves type parameters. More specifically: • A type parameter defines an open type. ---- Additional Comments From atsushi@ximian.com 2005-09-18 06:14:43 MST ---- http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?FeedbackID=FDBK36922 ---- Additional Comments From atsushi@ximian.com 2005-09-20 00:57:56 MST ---- It turned out to be invalid since there is cast expression at line 9, not "is". Unknown operating system unknown. Setting to default OS "Other".