Browse Source

Fix #2156: range syntax not being detected correctly in some cases

pull/2176/head
Daniel Grunwald 5 years ago
parent
commit
e8b35a481a
  1. 14
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/IndexRangeTest.cs
  2. 6
      ICSharpCode.Decompiler/IL/Transforms/IndexRangeTransform.cs

14
ICSharpCode.Decompiler.Tests/TestCases/Pretty/IndexRangeTest.cs

@ -102,6 +102,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -102,6 +102,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine(GetList()[GetIndex()]);
Console.WriteLine(GetSpan()[GetIndex()]);
Console.WriteLine(GetString()[GetIndex()]);
Console.WriteLine(GetString()?[GetIndex()]);
Console.WriteLine(new CustomList()[GetIndex()]);
Console.WriteLine(new CustomList2()[GetIndex()]);
}
@ -112,6 +113,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -112,6 +113,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine(GetList()[^GetInt()]);
Console.WriteLine(GetSpan()[^GetInt()]);
Console.WriteLine(GetString()[^GetInt()]);
Console.WriteLine(GetString()?[^GetInt()]);
Console.WriteLine(new CustomList()[^GetInt()]);
Console.WriteLine(new CustomList2()[^GetInt()]);
}
@ -141,6 +143,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -141,6 +143,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[GetRange()]); // fails to compile
Console.WriteLine(GetSpan()[GetRange()].ToString());
Console.WriteLine(GetString()[GetRange()]);
Console.WriteLine(GetString()?[GetRange()]);
Console.WriteLine(new CustomList()[GetRange()]);
Console.WriteLine(new CustomList2()[GetRange()]);
}
@ -150,6 +153,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -150,6 +153,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[GetIndex(1)..GetIndex(2)]); // fails to compile
Console.WriteLine(GetSpan()[GetIndex(1)..GetIndex(2)].ToString());
Console.WriteLine(GetString()[GetIndex(1)..GetIndex(2)]);
Console.WriteLine(GetString()?[GetIndex(1)..GetIndex(2)]);
Console.WriteLine(new CustomList()[GetIndex(1)..GetIndex(2)]);
Console.WriteLine(new CustomList2()[GetIndex(1)..GetIndex(2)]);
}
@ -159,6 +163,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -159,6 +163,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[GetInt()..GetInt()]); // fails to compile
Console.WriteLine(GetSpan()[GetInt(1)..GetInt(2)].ToString());
Console.WriteLine(GetString()[GetInt(1)..GetInt(2)]);
Console.WriteLine(GetString()?[GetInt(1)..GetInt(2)]);
Console.WriteLine(new CustomList()[GetInt(1)..GetInt(2)]);
Console.WriteLine(new CustomList2()[GetInt(1)..GetInt(2)]);
}
@ -168,6 +173,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -168,6 +173,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[^GetInt()..^GetInt()]); // fails to compile
Console.WriteLine(GetSpan()[^GetInt(1)..^GetInt(2)].ToString());
Console.WriteLine(GetString()[^GetInt(1)..^GetInt(2)]);
Console.WriteLine(GetString()?[^GetInt(1)..^GetInt(2)]);
Console.WriteLine(new CustomList()[^GetInt(1)..^GetInt(2)]);
Console.WriteLine(new CustomList2()[^GetInt(1)..^GetInt(2)]);
}
@ -177,6 +183,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -177,6 +183,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[GetInt()..^GetInt()]); // fails to compile
Console.WriteLine(GetSpan()[GetInt(1)..^GetInt(2)].ToString());
Console.WriteLine(GetString()[GetInt(1)..^GetInt(2)]);
Console.WriteLine(GetString()?[GetInt(1)..^GetInt(2)]);
Console.WriteLine(new CustomList()[GetInt(1)..^GetInt(2)]);
Console.WriteLine(new CustomList2()[GetInt(1)..^GetInt(2)]);
}
@ -186,6 +193,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -186,6 +193,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[^GetInt()..GetInt()]); // fails to compile
Console.WriteLine(GetSpan()[^GetInt(1)..GetInt(2)].ToString());
Console.WriteLine(GetString()[^GetInt(1)..GetInt(2)]);
Console.WriteLine(GetString()?[^GetInt(1)..GetInt(2)]);
Console.WriteLine(new CustomList()[^GetInt(1)..GetInt(2)]);
Console.WriteLine(new CustomList2()[^GetInt(1)..GetInt(2)]);
}
@ -196,6 +204,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -196,6 +204,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[..GetInt()]); // fails to compile
Console.WriteLine(GetSpan()[..GetInt(2)].ToString());
Console.WriteLine(GetString()[..GetInt(2)]);
Console.WriteLine(GetString()?[..GetInt(2)]);
Console.WriteLine(new CustomList()[..GetInt(2)]);
Console.WriteLine(new CustomList2()[..GetInt(2)]);
}
@ -206,6 +215,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -206,6 +215,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[..^GetInt()]); // fails to compile
Console.WriteLine(GetSpan()[..^GetInt(2)].ToString());
Console.WriteLine(GetString()[..^GetInt(2)]);
Console.WriteLine(GetString()?[..^GetInt(2)]);
Console.WriteLine(new CustomList()[..^GetInt(2)]);
Console.WriteLine(new CustomList2()[..^GetInt(2)]);
}
@ -216,6 +226,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -216,6 +226,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[GetInt()..]); // fails to compile
Console.WriteLine(GetSpan()[GetInt(1)..].ToString());
Console.WriteLine(GetString()[GetInt(1)..]);
Console.WriteLine(GetString()?[GetInt(1)..]);
Console.WriteLine(new CustomList()[GetInt(1)..]);
Console.WriteLine(new CustomList2()[GetInt(1)..]);
}
@ -226,6 +237,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -226,6 +237,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
//Console.WriteLine(GetList()[^GetInt()..]); // fails to compile
Console.WriteLine(GetSpan()[^GetInt(1)..].ToString());
Console.WriteLine(GetString()[^GetInt(1)..]);
Console.WriteLine(GetString()?[^GetInt(1)..]);
Console.WriteLine(new CustomList()[^GetInt(1)..]);
Console.WriteLine(new CustomList2()[^GetInt(1)..]);
}
@ -237,7 +249,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -237,7 +249,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine(GetString()[1..2]);
Console.WriteLine(GetString()[1..^1]);
Console.WriteLine(GetString()[^2..^1]);
Console.WriteLine(GetString()[..1]);
Console.WriteLine(GetString()[..^1]);
Console.WriteLine(GetString()[1..]);

6
ICSharpCode.Decompiler/IL/Transforms/IndexRangeTransform.cs

@ -16,7 +16,6 @@ @@ -16,7 +16,6 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Linq;
@ -176,15 +175,14 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -176,15 +175,14 @@ namespace ICSharpCode.Decompiler.IL.Transforms
}
// stloc startOffsetVar(call GetOffset(startIndexLoad, ldloc length))
if (!block.Instructions[pos].MatchStLoc(out ILVariable startOffsetVar, out ILInstruction startOffsetVarInit))
if (!(block.Instructions[pos].MatchStLoc(out ILVariable startOffsetVar, out ILInstruction startOffsetVarInit)
&& startOffsetVar.IsSingleDefinition && startOffsetVar.StackType == StackType.I4))
{
// Not our primary indexing/slicing pattern.
// However, we might be dealing with a partially-transformed pattern that needs to be extended.
ExtendSlicing();
return;
}
if (!(startOffsetVar.IsSingleDefinition && startOffsetVar.StackType == StackType.I4))
return;
var startIndexKind = MatchGetOffset(startOffsetVarInit, out ILInstruction startIndexLoad, containerLengthVar, ref containerVar);
pos++;
if (startOffsetVar.LoadCount == 1)

Loading…
Cancel
Save