Merge pull request #2720 from mono/fix-39325
[mono.git] / mcs / mcs / statement.cs
index e5c8bb4316c6b5af4ce075f1b939559bf7efd40f..9c84398ee3e4d52c01e8a3d25fd83adff75e96e3 100644 (file)
@@ -1291,10 +1291,6 @@ namespace Mono.CSharp {
                                                // Special case hoisted return value (happens in try/finally scenario)
                                                //
                                                if (ec.TryFinallyUnwind != null) {
-                                                       if (storey.HoistedReturnValue is VariableReference) {
-                                                               storey.HoistedReturnValue = ec.GetTemporaryField (storey.HoistedReturnValue.Type);
-                                                       }
-
                                                        exit_label = TryFinally.EmitRedirectedReturn (ec, async_body);
                                                }
 
@@ -3119,6 +3115,7 @@ namespace Mono.CSharp {
        public class ExplicitBlock : Block
        {
                protected AnonymousMethodStorey am_storey;
+               int debug_scope_index;
 
                public ExplicitBlock (Block parent, Location start, Location end)
                        : this (parent, (Flags) 0, start, end)
@@ -3226,8 +3223,10 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       if (Parent != null)
-                               ec.BeginScope ();
+                       if (Parent != null) {
+                               // TODO: It's needed only when scope has variable (normal or lifted)
+                               ec.BeginScope (GetDebugSymbolScopeIndex ());
+                       }
 
                        EmitScopeInitialization (ec);
 
@@ -3429,6 +3428,14 @@ namespace Mono.CSharp {
                        storey.Parent.PartialContainer.AddCompilerGeneratedClass (storey);
                }
 
+               public int GetDebugSymbolScopeIndex ()
+               {
+                       if (debug_scope_index == 0)
+                               debug_scope_index = ++ParametersBlock.debug_scope_index;
+
+                       return debug_scope_index;
+               }
+
                public void RegisterAsyncAwait ()
                {
                        var block = this;
@@ -7786,7 +7793,11 @@ namespace Mono.CSharp {
 
                                for_each.variable.Type = var_type;
 
+                               var prev_block = ec.CurrentBlock;
+                               ec.CurrentBlock = variables_block;
                                var variable_ref = new LocalVariableReference (for_each.variable, loc).Resolve (ec);
+                               ec.CurrentBlock = prev_block;
+
                                if (variable_ref == null)
                                        return false;
 
@@ -8075,7 +8086,10 @@ namespace Mono.CSharp {
                                                return false;
                                }
 
+                               var prev_block = ec.CurrentBlock;
+                               ec.CurrentBlock = for_each.variable.Block;
                                var variable_ref = new LocalVariableReference (variable, loc).Resolve (ec);
+                               ec.CurrentBlock = prev_block;
                                if (variable_ref == null)
                                        return false;
 
@@ -8233,7 +8247,7 @@ namespace Mono.CSharp {
                        ec.LoopEnd = ec.DefineLabel ();
 
                        if (!(Statement is Block))
-                               ec.BeginCompilerScope ();
+                               ec.BeginCompilerScope (variable.Block.Explicit.GetDebugSymbolScopeIndex ());
 
                        variable.CreateBuilder (ec);