Browse Source

Fix #2195: foreach loop conversion accidentally removes end container label.

pull/2201/head
Siegfried Pammer 5 years ago
parent
commit
4e07c952ab
  1. 28
      ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

28
ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

@ -725,8 +725,9 @@ namespace ICSharpCode.Decompiler.CSharp @@ -725,8 +725,9 @@ namespace ICSharpCode.Decompiler.CSharp
}
// Convert the modified body to C# AST:
var whileLoop = (WhileStatement)ConvertAsBlock(container).First();
BlockStatement foreachBody = (BlockStatement)whileLoop.EmbeddedStatement.Detach();
var whileLoopBlock = ConvertAsBlock(container);
var whileLoop = (WhileStatement)whileLoopBlock.First();
var foreachBody = (BlockStatement)whileLoop.EmbeddedStatement.Detach();
// Remove the first statement, as it is the foreachVariable = enumerator.Current; statement.
Statement firstStatement = foreachBody.Statements.First();
@ -752,14 +753,27 @@ namespace ICSharpCode.Decompiler.CSharp @@ -752,14 +753,27 @@ namespace ICSharpCode.Decompiler.CSharp
foreachStmt.AddAnnotation(new ForeachAnnotation(inst.ResourceExpression, conditionInst, singleGetter));
foreachStmt.CopyAnnotationsFrom(whileLoop);
// If there was an optional return statement, return it as well.
if (optionalReturnAfterLoop != null)
// If there were labels or any other statements in the whileLoopBlock, move them after the foreach
// loop.
if (optionalReturnAfterLoop != null || whileLoopBlock.Statements.Count > 1)
{
return new BlockStatement {
var block = new BlockStatement {
Statements = {
foreachStmt,
optionalReturnAfterLoop.AcceptVisitor(this)
foreachStmt
}
};
if (optionalReturnAfterLoop != null)
{
block.Statements.Add(optionalReturnAfterLoop.AcceptVisitor(this));
}
if (whileLoopBlock.Statements.Count > 1)
{
block.Statements.AddRange(whileLoopBlock.Statements
.Skip(1)
.SkipWhile(s => s.Annotations.Any(a => a == optionalReturnAfterLoop))
.Select(SyntaxExtensions.Detach));
}
return block;
}
return foreachStmt;
}
@ -1327,7 +1341,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1327,7 +1341,7 @@ namespace ICSharpCode.Decompiler.CSharp
}
else
{
blockStatement.Add(stmt);
blockStatement.Add(stmt.Detach());
}
}
if (block.FinalInstruction.OpCode != OpCode.Nop)

Loading…
Cancel
Save