[mcs] Emit side effects of reduced probing expression
authorMarek Safar <marek.safar@gmail.com>
Tue, 24 Jun 2014 11:52:31 +0000 (13:52 +0200)
committerMarek Safar <marek.safar@gmail.com>
Tue, 24 Jun 2014 12:20:15 +0000 (14:20 +0200)
mcs/mcs/expression.cs
mcs/tests/test-897.cs [new file with mode: 0644]
mcs/tests/ver-il-net_4_5.xml

index 6d4514a9c0b6935850c6ad9f02793db6c006a9f3..e50a4b5d40881c4a3e04c65e1827f901911c7ea2 100644 (file)
@@ -1449,6 +1449,11 @@ namespace Mono.CSharp
                        return this;
                }
 
+               public override void EmitSideEffect (EmitContext ec)
+               {
+                       expr.EmitSideEffect (ec);
+               }
+
                public override void FlowAnalysis (FlowAnalysisContext fc)
                {
                        expr.FlowAnalysis (fc);
@@ -1519,17 +1524,20 @@ namespace Mono.CSharp
                        }                       
                        ec.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
                }
-               
-               Expression CreateConstantResult (ResolveContext ec, bool result)
+
+               Expression CreateConstantResult (ResolveContext rc, bool result)
                {
                        if (result)
-                               ec.Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
+                               rc.Report.Warning (183, 1, loc, "The given expression is always of the provided (`{0}') type",
                                        probe_type_expr.GetSignatureForError ());
                        else
-                               ec.Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
+                               rc.Report.Warning (184, 1, loc, "The given expression is never of the provided (`{0}') type",
                                        probe_type_expr.GetSignatureForError ());
 
-                       return ReducedExpression.Create (new BoolConstant (ec.BuiltinTypes, result, loc), this);
+                       var c = new BoolConstant (rc.BuiltinTypes, result, loc);
+                       return expr.IsSideEffectFree ?
+                               ReducedExpression.Create (c, this) :
+                               new SideEffectConstant (c, this, loc);
                }
 
                protected override Expression DoResolve (ResolveContext ec)
diff --git a/mcs/tests/test-897.cs b/mcs/tests/test-897.cs
new file mode 100644 (file)
index 0000000..e5b3282
--- /dev/null
@@ -0,0 +1,26 @@
+using System;
+
+class X
+{
+       static int Foo ()
+       {
+               throw new ApplicationException ();
+       }
+
+       public static int Main ()
+       {
+               try {
+                       var b = Foo () is object;
+                       return 1;
+               } catch (ApplicationException) {
+               }
+
+               try {
+                       var b = Foo () as object;
+                       return 2;
+               } catch (ApplicationException) {
+               }
+
+               return 0;
+       }
+}
\ No newline at end of file
index ef70a42b7d9e98d39d775d9e34b51cd68ecf992f..9be929afc8a605db6b3535b727e2d8e3a9dd7911 100644 (file)
       </method>\r
     </type>\r
   </test>\r
+  <test name="test-897.cs">\r
+    <type name="X">\r
+      <method name="Int32 Foo()" attrs="145">\r
+        <size>7</size>\r
+      </method>\r
+      <method name="Int32 Main()" attrs="150">\r
+        <size>61</size>\r
+      </method>\r
+      <method name="Void .ctor()" attrs="6278">\r
+        <size>7</size>\r
+      </method>\r
+    </type>\r
+  </test>\r
   <test name="test-9.cs">\r
     <type name="X">\r
       <method name="Int32 Main(System.String[])" attrs="150">\r