[mcs] Null operator on value type extension argument. Fixes #33754
authorMarek Safar <marek.safar@gmail.com>
Tue, 8 Sep 2015 08:54:29 +0000 (10:54 +0200)
committerMarek Safar <marek.safar@gmail.com>
Tue, 8 Sep 2015 08:59:52 +0000 (10:59 +0200)
mcs/mcs/codegen.cs
mcs/tests/test-null-operator-18.cs [new file with mode: 0644]
mcs/tests/ver-il-net_4_x.xml

index d86bf2c3d1eb1b81dbbabfad537bcef677935dc3..c224b0d636a7056fa2632b123768c36e69db10b3 100644 (file)
@@ -1260,10 +1260,12 @@ namespace Mono.CSharp
                                        instance_address.AddressOf (ec, AddressOp.Load);
                                } else if (unwrap != null) {
                                        unwrap.Emit (ec);
-                                       var tmp = ec.GetTemporaryLocal (unwrap.Type);
-                                       ec.Emit (OpCodes.Stloc, tmp);
-                                       ec.Emit (OpCodes.Ldloca, tmp);
-                                       ec.FreeTemporaryLocal (tmp, unwrap.Type);
+                                       if (addressRequired) {
+                                               var tmp = ec.GetTemporaryLocal (unwrap.Type);
+                                               ec.Emit (OpCodes.Stloc, tmp);
+                                               ec.Emit (OpCodes.Ldloca, tmp);
+                                               ec.FreeTemporaryLocal (tmp, unwrap.Type);
+                                       }
                                } else if (!conditional_access_dup) {
                                        instance.Emit (ec);
                                }
diff --git a/mcs/tests/test-null-operator-18.cs b/mcs/tests/test-null-operator-18.cs
new file mode 100644 (file)
index 0000000..ad0d72e
--- /dev/null
@@ -0,0 +1,20 @@
+using System;
+
+static class MainClass
+{
+       public static void Main()
+       {
+               TestBug();
+       }
+
+       public static void TestBug()
+       {
+               int? value = null;
+               value?.Test();
+       }
+
+       public static void Test(this int value)
+       {
+               Console.WriteLine("Not null");
+       }
+}
index 2f2884795e597e51fa3306233a034960d56c0595..cda463fd51f18f8b8f060437016487485b9a5f7c 100644 (file)
     </type>
   </test>
   <test name="test-null-operator-08.cs">
+    <type name="S">
+      <method name="Void Foo(Boolean)" attrs="486">
+        <size>38</size>
+      </method>
+    </type>
     <type name="Program">
       <method name="Void M[T](T)" attrs="145">
         <size>88</size>
       <method name="Void M3[T](System.Nullable`1[T])" attrs="145">
         <size>106</size>
       </method>
-      <method name="Void Main()" attrs="145">
-        <size>165</size>
-      </method>
-      <method name="Void .ctor()" attrs="6278">
-        <size>7</size>
-      </method>
-    </type>
-    <type name="S">
-      <method name="Void Foo(Boolean)" attrs="486">
-        <size>38</size>
-      </method>
-    </type>
-    <type name="Program">
       <method name="Void TestAddress_1[T](T)" attrs="145">
         <size>56</size>
       </method>
       <method name="Void TestAddress_2[T](T[])" attrs="145">
         <size>82</size>
       </method>
+      <method name="Void Main()" attrs="145">
+        <size>165</size>
+      </method>
+      <method name="Void .ctor()" attrs="6278">
+        <size>7</size>
+      </method>
     </type>
   </test>
   <test name="test-null-operator-09.cs">
       </method>
     </type>
   </test>
+  <test name="test-null-operator-18.cs">
+    <type name="MainClass">
+      <method name="Void Main()" attrs="150">
+        <size>7</size>
+      </method>
+      <method name="Void TestBug()" attrs="150">
+        <size>38</size>
+      </method>
+      <method name="Void Test(Int32)" attrs="150">
+        <size>12</size>
+      </method>
+    </type>
+  </test>
   <test name="test-partial-01.cs">
     <type name="Foo.Hello">
       <method name="Void .ctor()" attrs="6278">