@@ -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