Bug 318610 (MONO75668) - [PATCH][GMCS] Incorrect 'as' Operator Behavior With Class Constraint
Summary: [PATCH][GMCS] Incorrect 'as' Operator Behavior With Class Constraint
Status: RESOLVED FIXED
Alias: MONO75668
Product: Mono: Compilers
Classification: Mono
Component: C# (show other bugs)
Version: 1.0
Hardware: Other Other
: P3 - Medium : Enhancement
Target Milestone: ---
Assignee: Mono Bugs
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-07-30 19:12 UTC by Stephen Quattlebaum
Modified: 2007-09-15 21:24 UTC (History)
1 user (show)

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments
proposed fix (359 bytes, patch)
2005-09-05 09:59 UTC, Thomas Wiest
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Wiest 2007-09-15 19:26:06 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".