Skip to content

Commit 02d9dc3

Browse files
committed
Fix #3323: Simplify cleanup in AwaitInFinallyTransform, ensuring that we do not miss any containers.
1 parent c84605a commit 02d9dc3

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

ICSharpCode.Decompiler/IL/ControlFlow/AsyncAwaitDecompiler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,12 @@ public void Run(ILFunction function, ILTransformContext context)
162162

163163
if (!isVisualBasicStateMachine)
164164
{
165+
context.StepStartGroup("AwaitInCatchTransform");
165166
AwaitInCatchTransform.Run(function, context);
167+
context.StepEndGroup();
168+
context.StepStartGroup("AwaitInFinallyTransform");
166169
AwaitInFinallyTransform.Run(function, context);
170+
context.StepEndGroup();
167171
}
168172

169173
awaitDebugInfos.SortBy(row => row.YieldOffset);

ICSharpCode.Decompiler/IL/ControlFlow/AwaitInFinallyTransform.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static void Run(ILFunction function, ILTransformContext context)
3131
{
3232
if (!context.Settings.AwaitInCatchFinally)
3333
return;
34-
HashSet<BlockContainer> changedContainers = new HashSet<BlockContainer>();
34+
bool needsUnreachableCodeCleanup = false;
3535

3636
// analyze all try-catch statements in the function
3737
foreach (var tryCatch in function.Descendants.OfType<TryCatch>().ToArray())
@@ -130,7 +130,7 @@ public static void Run(ILFunction function, ILTransformContext context)
130130

131131
context.StepStartGroup("Inline finally block with await", tryCatch.Handlers[0]);
132132
var cfg = new ControlFlowGraph(container, context.CancellationToken);
133-
changedContainers.Add(container);
133+
needsUnreachableCodeCleanup = true;
134134

135135
var finallyContainer = new BlockContainer().WithILRange(catchBlockContainer);
136136
tryCatch.ReplaceWith(new TryFinally(tryCatch.TryBlock, finallyContainer).WithILRange(tryCatch.TryBlock));
@@ -182,11 +182,16 @@ public static void Run(ILFunction function, ILTransformContext context)
182182

183183
context.Step("Clean up", function);
184184

185-
// clean up all modified containers
186-
foreach (var container in changedContainers)
187-
container.SortBlocks(deleteUnreachableBlocks: true);
188-
189-
((BlockContainer)function.Body).SortBlocks(deleteUnreachableBlocks: true);
185+
if (needsUnreachableCodeCleanup)
186+
{
187+
// Cleaning up only the modified containers is insufficient, deleting blocks in
188+
// any container can also cause other blocks in parent containers to become unreachable.
189+
// So we just clean up everything.
190+
foreach (var container in function.Body.Descendants.OfType<BlockContainer>())
191+
{
192+
container.SortBlocks(deleteUnreachableBlocks: true);
193+
}
194+
}
190195

191196
void MoveDominatedBlocksToContainer(Block newEntryPoint, Block endBlock, ControlFlowGraph graph,
192197
BlockContainer targetContainer)

0 commit comments

Comments
 (0)