[mcs] Explicit user conversion from nullable types does not have to lift the result...
authorMarek Safar <marek.safar@gmail.com>
Fri, 31 Jan 2014 12:30:49 +0000 (13:30 +0100)
committerMarek Safar <marek.safar@gmail.com>
Fri, 31 Jan 2014 12:30:49 +0000 (13:30 +0100)
mcs/mcs/convert.cs
mcs/tests/gtest-603.cs [new file with mode: 0644]

index 5035226d5280fd9b44611401b151b48e85dee149..6fc03b11d9e033c387ef9ec73c5b60561c4d1d3a 100644 (file)
@@ -1235,7 +1235,7 @@ namespace Mono.CSharp {
                                //
                                // User operator is of T?
                                //
-                               if (t_x.IsNullableType && target.IsNullableType) {
+                               if (t_x.IsNullableType && (target.IsNullableType || !implicitOnly)) {
                                        //
                                        // User operator return type does not match target type we need
                                        // yet another conversion. This should happen for promoted numeric
@@ -1251,7 +1251,8 @@ namespace Mono.CSharp {
                                                if (source == null)
                                                        return null;
 
-                                               source = new Nullable.LiftedConversion (source, unwrap, target).Resolve (ec);
+                                               if (target.IsNullableType)
+                                                       source = new Nullable.LiftedConversion (source, unwrap, target).Resolve (ec);
                                        }
                                } else {
                                        source = implicitOnly ?
diff --git a/mcs/tests/gtest-603.cs b/mcs/tests/gtest-603.cs
new file mode 100644 (file)
index 0000000..dcaf7a2
--- /dev/null
@@ -0,0 +1,33 @@
+using System;
+
+public class A<T>
+{
+       T value;
+
+       public A (T value)
+       {
+               this.value = value;
+       }
+
+       public static explicit operator T (A<T> source)
+       {
+               return source.value;
+       }
+}
+
+public class Test
+{
+       public static int Main ()
+       {
+               var source = new A<int?> (3);
+               if (N ((int)source) != 3)
+                       return 1;
+
+               return 0;
+       }
+
+       static int N (int value)
+       {
+               return value;
+       }
+}
\ No newline at end of file