Skip to content

Commit

Permalink
Fix #2378: DeconstructionTransform produced invalid ILAst with some o…
Browse files Browse the repository at this point in the history
…ptimized deconstruction patterns.
  • Loading branch information
siegfriedpammer committed Nov 7, 2021
1 parent 22c9801 commit 6bc0abc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,22 @@ public void LocalVariable_NoConversion_Tuple_ReferenceTypes()
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;
Expand Down
23 changes: 23 additions & 0 deletions ICSharpCode.Decompiler/IL/Transforms/DeconstructionTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,28 @@ bool MatchAssignments(Block block, ref int pos,
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)
Expand All @@ -397,6 +419,7 @@ void AddMissingAssignmentsForConversions(int index, ref Action<DeconstructInstru
stLoc.Variable = freshVar;
};
}
previousIndex = conversionResultIndex;
conversionStLocIndex++;
}
}
Expand Down

0 comments on commit 6bc0abc

Please sign in to comment.