Avoid creating a temporary variable when target await expression is does not include...
[mono.git] / mcs / mcs / ecore.cs
index 5fdf8480a3eb3a1106c1db2bc040b2aa5b43aeeb..b61fc91782f0880fad06edc990d10bb8eeedfe25 100644 (file)
@@ -519,22 +519,33 @@ namespace Mono.CSharp {
                        if (IsSideEffectFree)
                                return this;
 
+                       bool needs_temporary = ContainsEmitWithAwait ();
+                       if (!needs_temporary)
+                               ec.EmitThis ();
+
                        // Emit original code
                        EmitToFieldSource (ec);
 
-                       // Create temporary local (we cannot load this before Emit)
-                       var temp = ec.GetTemporaryLocal (type);
-                       ec.Emit (OpCodes.Stloc, temp);
-
                        //
-                       // Store the result to temporary field
+                       // Store the result to temporary field when we
+                       // cannot load this directly
                        //
                        var field = ec.GetTemporaryField (type);
-                       ec.EmitThis ();
-                       ec.Emit (OpCodes.Ldloc, temp);
-                       field.EmitAssignFromStack (ec);
+                       if (needs_temporary) {
+                               //
+                               // Create temporary local (we cannot load this before Emit)
+                               //
+                               var temp = ec.GetTemporaryLocal (type);
+                               ec.Emit (OpCodes.Stloc, temp);
+
+                               ec.EmitThis ();
+                               ec.Emit (OpCodes.Ldloc, temp);
+                               field.EmitAssignFromStack (ec);
 
-                       ec.FreeTemporaryLocal (temp, type);
+                               ec.FreeTemporaryLocal (temp, type);
+                       } else {
+                               field.EmitAssignFromStack (ec);
+                       }
 
                        return field;
                }