Bugzilla – Bug 322080
[GMCS] Compiler error when using "as" operator with int?
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by michael@synthesyssolutions.com 2006-09-11 17:33:22 MST ---- Description of Problem: The following code compiles and executes using MS.NET, but not Mono: using System; public class NullableInt { public static void Main() { object x = null; int? y = x as int?; /* Causes CS0077 */ Console.WriteLine("y: '{0}'", y); Console.WriteLine("y.HasValue: '{0}'", y.HasValue); } } Steps to reproduce the problem: 1. Copy and paste code above into a source code file 2. Attempt to compile source file with gmcs 3. Receive compiler error CS0077 Actual Results: test.cs(9,14): error CS0077: The as operator must be used with a reference type (`int?' is a value type) Compilation failed: 1 error(s), 0 warnings Expected Results: Successful compilation. The program outputs the following using MS.NET: y: '' y.HasValue: 'False' How often does this happen? Every time. Additional Information: Using svn TRUNK ---- Additional Comments From marek.safar@seznam.cz 2006-09-23 17:38:08 MST ---- It looks like csc issue or C# standard was not updated correctly. Based on the sections 11.4 and 14.9.11 it should not be allowed. ---- Additional Comments From marek.safar@seznam.cz 2006-10-03 10:46:15 MST ---- *** https://bugzilla.novell.com/show_bug.cgi?id=MONO79566 has been marked as a duplicate of this bug. *** ---- Additional Comments From marek.safar@seznam.cz 2007-03-24 05:39:49 MST ---- This bit is specified in Microsoft version of the standard. 24.3.6 The as operator The as operator (§7.9.10) is extended to support nullable types. In an operation of the form e as T, e must be an expression and T must be a reference type, a type parameter known to be a reference type, or a nullable type. Furthermore, at least one of the following must be true, or otherwise a compile-time error occurs: • An identity (§6.1.1), implicit reference (§6.1.4), boxing (§6.1.5, §24.2.3), explicit reference (§6.2.3), or unboxing (§6.2.4, §24.2.3) conversion exists from the type of e to T. • The type of e or T is an open type. • e is the null literal. The operation e as T produces the same result as e is T ? (T)(e) : (T)null except that e is only evaluated once. The compiler can be expected to optimize e as T to perform at most one dynamic type check as opposed to the two dynamic type checks implied by the expansion above. ---- Additional Comments From martin@ximian.com 2007-04-11 14:18:23 MST ---- Fixed in SVN. Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>Debian Etch</cf_op_sys_details>