Bug 58190 - Closure Generations difference
Summary: Closure Generations difference
Status: CONFIRMED
Alias: None
Product: Compilers
Classification: Mono
Component: C# (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2017-07-17 14:37 UTC by Stanislau
Modified: 2017-07-31 13:05 UTC (History)
1 user (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Source code of minimal application with issue to reproduce. (7.80 KB, application/x-7z-compressed)
2017-07-17 14:37 UTC, Stanislau
Details

Description Stanislau 2017-07-17 14:37:46 UTC
Created attachment 23600 [details]
Source code of minimal application with issue to reproduce.

We've got an issue using MvvmLight recently and we found out that C# compiler generate different IL code on Windows and Mac Os machines.

Environments:
Windows 10, VisualStudio 2015, .NET 4.6, msbuild.
OS X 10.12.2, xCode 8.3.3, Xamarin Studio Community 6.3 (build 863).

Here is the source code to be compiled on different platforms. What I expect is that _anonMethod lambda would be compiled as an anonymize method and _closure lambda would be compiled as closure class.

public class ClassWithClosureAndLambdaInOneMethod
	{
		private Action _anonMethod;

		private int _i;

		private Action _closure;

		public void LambdaAndClosure()
		{
			_anonMethod = () => _i++;

			int j = 0;

			_closure = () => j++;
		}
	}

Here is what generated by compiler on Windows machine.

public class ClassWithClosureAndLambdaInOneMethod
  {
    private Action _anonMethod;
    private int _i;
    private Action _closure;

    public ClassWithClosureAndLambdaInOneMethod()
    {
      base.\u002Ector();
    }

    public void LambdaAndClosure()
    {
      ClassWithClosureAndLambdaInOneMethod.\u003C\u003Ec__DisplayClass3_0 cDisplayClass30 = new ClassWithClosureAndLambdaInOneMethod.\u003C\u003Ec__DisplayClass3_0();
      // ISSUE: method pointer
      this._anonMethod = new Action((object) this, __methodptr(\u003CLambdaAndClosure\u003Eb__3_0));
      cDisplayClass30.j = 0;
      // ISSUE: method pointer
      this._closure = new Action((object) cDisplayClass30, __methodptr(\u003CLambdaAndClosure\u003Eb__1));
    }

    [CompilerGenerated]
    private void \u003CLambdaAndClosure\u003Eb__3_0()
    {
      this._i = this._i + 1;
    }

    [CompilerGenerated]
    private sealed class \u003C\u003Ec__DisplayClass3_0
    {
      public int j;

      public \u003C\u003Ec__DisplayClass3_0()
      {
        base.\u002Ector();
      }

      internal void \u003CLambdaAndClosure\u003Eb__1()
      {
        this.j = this.j + 1;
      }
    }
  }

And here is what generated using Mac OS's compiler.

  public class ClassWithClosureAndLambdaInOneMethod
  {
    private Action _anonMethod;
    private int _i;
    private Action _closure;

    public ClassWithClosureAndLambdaInOneMethod()
    {
      base.\u002Ector();
    }

    public void LambdaAndClosure()
    {
      ClassWithClosureAndLambdaInOneMethod.\u003CLambdaAndClosure\u003Ec__AnonStorey0 closureCAnonStorey0 = new ClassWithClosureAndLambdaInOneMethod.\u003CLambdaAndClosure\u003Ec__AnonStorey0();
      closureCAnonStorey0.\u0024this = this;
      // ISSUE: method pointer
      this._anonMethod = new Action((object) closureCAnonStorey0, __methodptr(\u003C\u003Em__0));
      closureCAnonStorey0.j = 0;
      // ISSUE: method pointer
      this._closure = new Action((object) closureCAnonStorey0, __methodptr(\u003C\u003Em__1));
    }

    [CompilerGenerated]
    private sealed class \u003CLambdaAndClosure\u003Ec__AnonStorey0
    {
      internal int j;
      internal ClassWithClosureAndLambdaInOneMethod \u0024this;

      public \u003CLambdaAndClosure\u003Ec__AnonStorey0()
      {
        base.\u002Ector();
      }

      internal void \u003C\u003Em__0()
      {
        ++this.\u0024this._i;
      }

      internal void \u003C\u003Em__1()
      {
        this.j = this.j + 1;
      }
    }
  }

As you could see _anonMethod's lambda is a part of closure class on Mac, but it is not the expected behavior.

This causes the issue with MvvmLight bindings, because any closure inside the method causes the closure class to be generated, but expected to be generated only for those methods which contains closure of some local variables.

The source code to reproduce the issue is attached.
Comment 1 Marek Safar 2017-07-31 13:05:31 UTC
This is more optimisation than actual bug. I agree it'd be nice to have it but it's quite tricky to do with correct codebase.

Enclosing work-in-progress patch


diff --git a/mcs/mcs/anonymous.cs b/mcs/mcs/anonymous.cs
index 6cec0e1..97911da 100644
--- a/mcs/mcs/anonymous.cs
+++ b/mcs/mcs/anonymous.cs
@@ -1774,7 +1774,9 @@ namespace Mono.CSharp {
 			if (src_block.HasCapturedVariable || src_block.HasCapturedThis) {
 				parent = storey = FindBestMethodStorey ();
 
-				if (storey == null) {
+				if (src_block.ParametersBlock.HasReferenceToStoreyForInstanceLambdas) {
+					src_block.ParametersBlock.StateMachine.AddParentStoreyReference (ec, storey);
+				} else if (storey == null || (!src_block.HasCapturedVariable && src_block.HasCapturedThis)) {
 					var top_block = src_block.ParametersBlock.TopBlock;
 					var sm = top_block.StateMachine;
 
@@ -1793,6 +1795,7 @@ namespace Mono.CSharp {
 
 						if (sm_parent == null) {
 							top_block.RemoveThisReferenceFromChildrenBlock (src_block);
+							parent = storey = null;
 						} else if (sm_parent.Kind == MemberKind.Struct) {
 							//
 							// Special case where parent class is used to emit instance method
@@ -1808,8 +1811,6 @@ namespace Mono.CSharp {
 							parent = storey = sm;
 						}
 					}
-				} else if (src_block.ParametersBlock.HasReferenceToStoreyForInstanceLambdas) {
-					src_block.ParametersBlock.StateMachine.AddParentStoreyReference (ec, storey);
 				}
 
 				modifiers = storey != null ? Modifiers.INTERNAL : Modifiers.PRIVATE;

Note You need to log in before you can comment on or make changes to this bug.