Browse Source

Fix #2395: Do not use for->foreach transform if collection is not an array.

pull/2408/head
Siegfried Pammer 4 years ago
parent
commit
16134e52e4
  1. 3
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  2. 26
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs
  3. 4
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

3
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -238,7 +238,8 @@ namespace ICSharpCode.Decompiler.Tests @@ -238,7 +238,8 @@ namespace ICSharpCode.Decompiler.Tests
{
RunForLibrary(cscOptions: cscOptions, decompilerSettings: new DecompilerSettings {
// legacy csc generates a dead store in debug builds
RemoveDeadStores = (cscOptions == CompilerOptions.None)
RemoveDeadStores = (cscOptions == CompilerOptions.None),
UseExpressionBodyForCalculatedGetterOnlyProperties = false
});
}

26
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Loops.cs

@ -269,6 +269,23 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -269,6 +269,23 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
public class NonEnumerableArrayLike
{
private readonly int length;
public Item this[int index] {
get {
return null;
}
}
public int Length {
get {
return length;
}
}
}
private IEnumerable<string> alternatives;
private object someObject;
@ -469,6 +486,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -469,6 +486,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
public void ForOverNonArray(NonEnumerableArrayLike array)
{
for (int i = 0; i < array.Length; i++)
{
Item item = array[i];
Console.WriteLine(item.ToString() + item.ToString());
}
}
public unsafe void ForEachOverArrayOfPointers(int*[] array)
{
foreach (int* value in array)

4
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -351,6 +351,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -351,6 +351,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
var loopContainer = forStatement.Annotation<IL.BlockContainer>();
if (itemVariable == null || indexVariable == null || arrayVariable == null)
return null;
if (arrayVariable.Type.Kind != TypeKind.Array)
return null;
if (!VariableCanBeUsedAsForeachLocal(itemVariable, forStatement))
return null;
if (indexVariable.StoreCount != 2 || indexVariable.LoadCount != 3 || indexVariable.AddressCount != 0)
@ -462,6 +464,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -462,6 +464,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
lowerBounds[i] = indexVariable;
i++;
}
if (collection.Type.Kind != TypeKind.Array)
return false;
var m2 = foreachVariableOnMultArrayAssignPattern.Match(stmt);
if (!m2.Success)
return false;

Loading…
Cancel
Save