From 539925f25915a3ffb7e468e6d367e93542cfe087 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 18 May 2023 13:39:47 +0200 Subject: [PATCH] Fix #2983: display-class locals should be named uniquely per top-level ILFunction. Add assertion to ResolveCollisions: ensure that colliding/merged variables have the same type. --- ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs | 1 + ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs b/ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs index b146e85a4..b17960e36 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs @@ -476,6 +476,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms Debug.Assert(point1.level == point2.level); if (point1.nextNode.Parent == point2.nextNode.Parent) { + Debug.Assert(prev.Type.Equals(v.Type)); // We found a collision! v.InvolvedInCollision = true; prev.ReplacementDueToCollision = v; diff --git a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs index 4a8342cd7..e4331c74b 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs @@ -58,6 +58,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms Dictionary localFunctionMapping; HashSet loopCounters; const char maxLoopVariableName = 'n'; + int numDisplayClassLocals; public void Run(ILFunction function, ILTransformContext context) { @@ -151,6 +152,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms AddExistingName(reservedVariableNames, p.Name); } } + numDisplayClassLocals = 0; foreach (ILFunction f in function.Descendants.OfType().Reverse()) { PerformAssignment(f); @@ -195,7 +197,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms { // remove unused variables before assigning names function.Variables.RemoveDead(); - int numDisplayClassLocals = 0; Dictionary assignedLocalSignatureIndices = new Dictionary(); foreach (var v in function.Variables.OrderBy(v => v.Name)) {