Browse Source

Fix #2378: DeconstructionTransform produced invalid ILAst with some optimized deconstruction patterns.

pull/2542/head
Siegfried Pammer 4 years ago
parent
commit
6bc0abc336
  1. 16
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs
  2. 23
      ICSharpCode.Decompiler/IL/Transforms/DeconstructionTransform.cs

16
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DeconstructionTests.cs

@ -219,6 +219,22 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -219,6 +219,22 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine(value2);
}
public void Issue2378(Tuple<object, object> tuple)
{
var (value, value2) = tuple;
Console.WriteLine(value2);
Console.WriteLine(value);
}
public void Issue2378_IntToLongConversion(Tuple<int, int> tuple)
{
int value;
long value2;
(value, value2) = tuple;
Console.WriteLine(value2);
Console.WriteLine(value);
}
public void LocalVariable_IntToLongConversion_Custom()
{
int value;

23
ICSharpCode.Decompiler/IL/Transforms/DeconstructionTransform.cs

@ -378,6 +378,28 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -378,6 +378,28 @@ namespace ICSharpCode.Decompiler.IL.Transforms
previousIndex = index;
}
AddMissingAssignmentsForConversions(int.MaxValue, ref delayedActions);
if (deconstructionResults != null)
{
int i = previousIndex + 1;
while (i < deconstructionResults.Length)
{
var v = deconstructionResults[i];
// this should only happen in release mode, where usually the last deconstruction element
// is not stored to a temporary, if it is used directly (and only once!)
// after the deconstruction.
if (v?.LoadCount == 1)
{
delayedActions += (DeconstructInstruction deconstructInst) => {
var freshVar = context.Function.RegisterVariable(VariableKind.StackSlot, v.Type);
deconstructInst.Assignments.Instructions.Add(new StLoc(freshVar, new LdLoc(v)));
v.LoadInstructions[0].Variable = freshVar;
};
}
i++;
}
}
return startPos != pos;
void AddMissingAssignmentsForConversions(int index, ref Action<DeconstructInstruction> delayedActions)
@ -397,6 +419,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -397,6 +419,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
stLoc.Variable = freshVar;
};
}
previousIndex = conversionResultIndex;
conversionStLocIndex++;
}
}

Loading…
Cancel
Save