Bugzilla – Bug 318610
[PATCH][GMCS] Incorrect 'as' Operator Behavior With Class Constraint
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by stephen@covidimus.net 2005-07-30 12:12:48 MST ---- Description of Problem: Gmcs appears to recognize and accept a class constraint (where T:class) on a generic method, but the 'as' operator doesn't work as it should when the class constraint is applied. Code to Reproduce the Problem: public static T QueryInterface<T>(object val) where T : class { if (val == null) return null; // First, see if the given object can be directly cast // to the requested type. This will be a common case, // especially when checking for standard behavior interface // implementations (like IXrcDataElement). T tval = val as T; if (tval != null) return tval; // ... rest of method unimportant and omitted ... return null; } Actual Results: Xircle.Core/src/Core/XrcConvert.cs(118) error CS0077: The as operator should be used with a reference type only (T is a value type) Expected Results: The 'as' operator should compile without errors b/c the 'class' constraint (where T:class) on the method should cause T to be assumed to be a reference type. Visual Studio beta 2 accepts this code without error. How often does this happen? Every time. Additional Information: https://bugzilla.novell.com/show_bug.cgi?id=MONO75368 appeared to me to be related, but even though it's marked as fixed, the problem still appears in SVN head, so perhaps it is not. ---- Additional Comments From atsushi@ximian.com 2005-09-05 02:59:02 MST ---- Created an attachment (id=168283) proposed fix ---- Additional Comments From martin@ximian.com 2005-09-05 13:41:46 MST ---- Fixed in SVN (the patch is a big no-go, used a different one). ---- Additional Comments From miguel@ximian.com 2005-09-05 15:09:17 MST ---- can we get a test case for this, to integrate into the build? ---- Additional Comments From atsushi@ximian.com 2005-09-05 15:29:50 MST ---- added gtest-200.cs (r49503). Imported an attachment (id=168283) Unknown operating system unknown. Setting to default OS "Other".