2007-04-11 Martin Baulig <martin@ximian.com>
authorMartin Baulig <martin@novell.com>
Wed, 11 Apr 2007 16:29:14 +0000 (16:29 -0000)
committerMartin Baulig <martin@novell.com>
Wed, 11 Apr 2007 16:29:14 +0000 (16:29 -0000)
* expression.cs (As): Add support for nullable types; fixes #79371.

svn path=/trunk/mcs/; revision=75615

mcs/mcs/ChangeLog
mcs/mcs/expression.cs
mcs/tests/gtest-329.cs [new file with mode: 0755]

index d9309bba45407f658f5873a3be9a8912d9b392fe..307008ea71660cfe8fdf950d428e5c963b281950 100644 (file)
@@ -1,3 +1,7 @@
+2007-04-11  Martin Baulig  <martin@ximian.com>
+
+       * expression.cs (As): Add support for nullable types; fixes #79371.
+
 2007-04-11  Martin Baulig  <martin@ximian.com>
 
        * doc.cs (DocUtil.GetSignatureForDoc): Don't crash if
index 47031af3a172430fd93431efcaf823ec3c20d2d9..390a179632f8754ed76220c6df30aabc0e5b120b 100644 (file)
@@ -1256,6 +1256,11 @@ namespace Mono.CSharp {
 
                        if (do_isinst)
                                ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
+
+#if GMCS_SOURCE
+                       if (TypeManager.IsNullableType (type))
+                               ig.Emit (OpCodes.Unbox_Any, type);
+#endif
                }
 
                static void Error_CannotConvertType (Type source, Type target, Location loc)
@@ -1278,7 +1283,7 @@ namespace Mono.CSharp {
                        eclass = ExprClass.Value;
                        Type etype = expr.Type;
 
-                       if (type.IsValueType) {
+                       if (type.IsValueType && !TypeManager.IsNullableType (type)) {
                                Report.Error (77, loc, "The as operator must be used with a reference type (`" +
                                              TypeManager.CSharpName (type) + "' is a value type)");
                                return null;
@@ -1310,7 +1315,7 @@ namespace Mono.CSharp {
                                }
                        }
 #endif
-                       
+
                        Expression e = Convert.ImplicitConversion (ec, expr, type, loc);
                        if (e != null){
                                expr = e;
diff --git a/mcs/tests/gtest-329.cs b/mcs/tests/gtest-329.cs
new file mode 100755 (executable)
index 0000000..398ad91
--- /dev/null
@@ -0,0 +1,14 @@
+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);
+         }
+}