Skip to content

Commit

Permalink
[mcs] Use reference from closest storey which holds captured this. Fi…
Browse files Browse the repository at this point in the history
…xes #29506
  • Loading branch information
marek-safar committed May 5, 2015
1 parent 691f540 commit 8e3e9d8
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 3 deletions.
20 changes: 17 additions & 3 deletions mcs/mcs/statement.cs
Expand Up @@ -3352,12 +3352,26 @@ protected void DefineStoreyContainer (EmitContext ec, AnonymousMethodStorey stor
}

if (parent_storey_block.AnonymousMethodStorey == null) {
pb.StateMachine.AddCapturedThisField (ec, null);
b.HasCapturedThis = true;
if (pb.StateMachine.HoistedThis == null) {
pb.StateMachine.AddCapturedThisField (ec, null);
b.HasCapturedThis = true;
}

continue;
}

pb.StateMachine.AddParentStoreyReference (ec, storey);
var parent_this_block = pb;
while (parent_this_block.Parent != null) {
parent_this_block = parent_this_block.Parent.ParametersBlock;
if (parent_this_block.StateMachine != null) {
break;
}
}

//
// Add reference to closest storey which holds captured this
//
pb.StateMachine.AddParentStoreyReference (ec, parent_this_block.StateMachine ?? storey);
}

//
Expand Down
54 changes: 54 additions & 0 deletions mcs/tests/test-async-78.cs
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

public static class ExceptionHelper
{
public static async Task ConditionalCatchExceptThreadAbortAsync (Func<Task>
funcTask, Action<Exception> conditionalCatchAction)
{
funcTask ();
return;
}
}

class ATask
{
readonly object _asyncTaskCancellationSource = new object ();

readonly object aname;

public async Task<bool> OnDoWorkAsync ()
{
await ExceptionHelper.ConditionalCatchExceptThreadAbortAsync (
async () => {
if (_asyncTaskCancellationSource != null) {
string item = null;
await ExceptionHelper.ConditionalCatchExceptThreadAbortAsync (
async () => {
Console.WriteLine (aname);
},
(e) => {
Console.WriteLine (item);
}
);
}
},
null
);

return true;
}
}

public class Tests
{
public static void Main ()
{
var a = new ATask ();
var res = a.OnDoWorkAsync ().Result;
}
}
69 changes: 69 additions & 0 deletions mcs/tests/ver-il-net_4_5.xml
Expand Up @@ -64978,6 +64978,75 @@
</method>
</type>
</test>
<test name="test-async-78.cs">
<type name="ExceptionHelper">
<method name="System.Threading.Tasks.Task ConditionalCatchExceptThreadAbortAsync(System.Func`1[System.Threading.Tasks.Task], System.Action`1[System.Exception])" attrs="150">
<size>41</size>
</method>
</type>
<type name="ATask">
<method name="System.Threading.Tasks.Task`1[System.Boolean] OnDoWorkAsync()" attrs="134">
<size>41</size>
</method>
<method name="System.Threading.Tasks.Task &lt;OnDoWorkAsync&gt;m__0()" attrs="129">
<size>41</size>
</method>
<method name="Void .ctor()" attrs="6278">
<size>18</size>
</method>
</type>
<type name="Tests">
<method name="Void Main()" attrs="150">
<size>20</size>
</method>
<method name="Void .ctor()" attrs="6278">
<size>7</size>
</method>
</type>
<type name="ExceptionHelper+&lt;ConditionalCatchExceptThreadAbortAsync&gt;c__async0">
<method name="Void MoveNext()" attrs="486">
<size>73</size>
</method>
<method name="Void SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)" attrs="486">
<size>13</size>
</method>
</type>
<type name="ATask+&lt;OnDoWorkAsync&gt;c__async0">
<method name="Void MoveNext()" attrs="486">
<size>182</size>
</method>
<method name="Void SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)" attrs="486">
<size>13</size>
</method>
</type>
<type name="ATask+&lt;OnDoWorkAsync&gt;c__async4">
<method name="Void MoveNext()" attrs="486">
<size>248</size>
</method>
<method name="Void SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)" attrs="486">
<size>13</size>
</method>
</type>
<type name="ATask+&lt;OnDoWorkAsync&gt;c__async4+&lt;OnDoWorkAsync&gt;c__AnonStorey7">
<method name="System.Threading.Tasks.Task &lt;&gt;m__0()" attrs="131">
<size>46</size>
</method>
<method name="Void &lt;&gt;m__1(System.Exception)" attrs="131">
<size>13</size>
</method>
<method name="Void .ctor()" attrs="6278">
<size>7</size>
</method>
</type>
<type name="ATask+&lt;OnDoWorkAsync&gt;c__async4+&lt;OnDoWorkAsync&gt;c__AnonStorey7+&lt;OnDoWorkAsync&gt;c__async6">
<method name="Void MoveNext()" attrs="486">
<size>82</size>
</method>
<method name="Void SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)" attrs="486">
<size>13</size>
</method>
</type>
</test>
<test name="test-cls-00.cs">
<type name="CLSCLass_6">
<method name="Void add_Disposed(Delegate)" attrs="2182">
Expand Down

0 comments on commit 8e3e9d8

Please sign in to comment.