Skip to content

Commit 8e3e9d8

Browse files
committed
[mcs] Use reference from closest storey which holds captured this. Fixes #29506
1 parent 691f540 commit 8e3e9d8

File tree

3 files changed

+140
-3
lines changed

3 files changed

+140
-3
lines changed

mcs/mcs/statement.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -3352,12 +3352,26 @@ protected void DefineStoreyContainer (EmitContext ec, AnonymousMethodStorey stor
33523352
}
33533353

33543354
if (parent_storey_block.AnonymousMethodStorey == null) {
3355-
pb.StateMachine.AddCapturedThisField (ec, null);
3356-
b.HasCapturedThis = true;
3355+
if (pb.StateMachine.HoistedThis == null) {
3356+
pb.StateMachine.AddCapturedThisField (ec, null);
3357+
b.HasCapturedThis = true;
3358+
}
3359+
33573360
continue;
33583361
}
33593362

3360-
pb.StateMachine.AddParentStoreyReference (ec, storey);
3363+
var parent_this_block = pb;
3364+
while (parent_this_block.Parent != null) {
3365+
parent_this_block = parent_this_block.Parent.ParametersBlock;
3366+
if (parent_this_block.StateMachine != null) {
3367+
break;
3368+
}
3369+
}
3370+
3371+
//
3372+
// Add reference to closest storey which holds captured this
3373+
//
3374+
pb.StateMachine.AddParentStoreyReference (ec, parent_this_block.StateMachine ?? storey);
33613375
}
33623376

33633377
//

mcs/tests/test-async-78.cs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
public static class ExceptionHelper
8+
{
9+
public static async Task ConditionalCatchExceptThreadAbortAsync (Func<Task>
10+
funcTask, Action<Exception> conditionalCatchAction)
11+
{
12+
funcTask ();
13+
return;
14+
}
15+
}
16+
17+
class ATask
18+
{
19+
readonly object _asyncTaskCancellationSource = new object ();
20+
21+
readonly object aname;
22+
23+
public async Task<bool> OnDoWorkAsync ()
24+
{
25+
await ExceptionHelper.ConditionalCatchExceptThreadAbortAsync (
26+
async () => {
27+
if (_asyncTaskCancellationSource != null) {
28+
string item = null;
29+
30+
await ExceptionHelper.ConditionalCatchExceptThreadAbortAsync (
31+
async () => {
32+
Console.WriteLine (aname);
33+
},
34+
(e) => {
35+
Console.WriteLine (item);
36+
}
37+
);
38+
}
39+
},
40+
null
41+
);
42+
43+
return true;
44+
}
45+
}
46+
47+
public class Tests
48+
{
49+
public static void Main ()
50+
{
51+
var a = new ATask ();
52+
var res = a.OnDoWorkAsync ().Result;
53+
}
54+
}

mcs/tests/ver-il-net_4_5.xml

+69
Original file line numberDiff line numberDiff line change
@@ -64978,6 +64978,75 @@
6497864978
</method>
6497964979
</type>
6498064980
</test>
64981+
<test name="test-async-78.cs">
64982+
<type name="ExceptionHelper">
64983+
<method name="System.Threading.Tasks.Task ConditionalCatchExceptThreadAbortAsync(System.Func`1[System.Threading.Tasks.Task], System.Action`1[System.Exception])" attrs="150">
64984+
<size>41</size>
64985+
</method>
64986+
</type>
64987+
<type name="ATask">
64988+
<method name="System.Threading.Tasks.Task`1[System.Boolean] OnDoWorkAsync()" attrs="134">
64989+
<size>41</size>
64990+
</method>
64991+
<method name="System.Threading.Tasks.Task &lt;OnDoWorkAsync&gt;m__0()" attrs="129">
64992+
<size>41</size>
64993+
</method>
64994+
<method name="Void .ctor()" attrs="6278">
64995+
<size>18</size>
64996+
</method>
64997+
</type>
64998+
<type name="Tests">
64999+
<method name="Void Main()" attrs="150">
65000+
<size>20</size>
65001+
</method>
65002+
<method name="Void .ctor()" attrs="6278">
65003+
<size>7</size>
65004+
</method>
65005+
</type>
65006+
<type name="ExceptionHelper+&lt;ConditionalCatchExceptThreadAbortAsync&gt;c__async0">
65007+
<method name="Void MoveNext()" attrs="486">
65008+
<size>73</size>
65009+
</method>
65010+
<method name="Void SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)" attrs="486">
65011+
<size>13</size>
65012+
</method>
65013+
</type>
65014+
<type name="ATask+&lt;OnDoWorkAsync&gt;c__async0">
65015+
<method name="Void MoveNext()" attrs="486">
65016+
<size>182</size>
65017+
</method>
65018+
<method name="Void SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)" attrs="486">
65019+
<size>13</size>
65020+
</method>
65021+
</type>
65022+
<type name="ATask+&lt;OnDoWorkAsync&gt;c__async4">
65023+
<method name="Void MoveNext()" attrs="486">
65024+
<size>248</size>
65025+
</method>
65026+
<method name="Void SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)" attrs="486">
65027+
<size>13</size>
65028+
</method>
65029+
</type>
65030+
<type name="ATask+&lt;OnDoWorkAsync&gt;c__async4+&lt;OnDoWorkAsync&gt;c__AnonStorey7">
65031+
<method name="System.Threading.Tasks.Task &lt;&gt;m__0()" attrs="131">
65032+
<size>46</size>
65033+
</method>
65034+
<method name="Void &lt;&gt;m__1(System.Exception)" attrs="131">
65035+
<size>13</size>
65036+
</method>
65037+
<method name="Void .ctor()" attrs="6278">
65038+
<size>7</size>
65039+
</method>
65040+
</type>
65041+
<type name="ATask+&lt;OnDoWorkAsync&gt;c__async4+&lt;OnDoWorkAsync&gt;c__AnonStorey7+&lt;OnDoWorkAsync&gt;c__async6">
65042+
<method name="Void MoveNext()" attrs="486">
65043+
<size>82</size>
65044+
</method>
65045+
<method name="Void SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)" attrs="486">
65046+
<size>13</size>
65047+
</method>
65048+
</type>
65049+
</test>
6498165050
<test name="test-cls-00.cs">
6498265051
<type name="CLSCLass_6">
6498365052
<method name="Void add_Disposed(Delegate)" attrs="2182">

0 commit comments

Comments
 (0)