From 4f3c7d6703001f920ee702ec96a6c6e214964a44 Mon Sep 17 00:00:00 2001 From: Carol Eidt Date: Mon, 19 Nov 2018 19:43:03 -0800 Subject: [PATCH] Add asserts for the types of the Span indexers. (dotnet/coreclr#21037) Also, add a test for dotnet/coreclr#20958 Commit migrated from https://github.com/dotnet/coreclr/commit/398c3bc97d5297df9773284a03e425f90c40bcf0 --- src/coreclr/src/jit/importer.cpp | 2 + .../JitBlue/GitHub_20958/GitHub_20958.cs | 61 ++++++++++++++++++++++ .../JitBlue/GitHub_20958/GitHub_20958.csproj | 17 ++++++ 3 files changed, 80 insertions(+) create mode 100644 src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_20958/GitHub_20958.cs create mode 100644 src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_20958/GitHub_20958.csproj diff --git a/src/coreclr/src/jit/importer.cpp b/src/coreclr/src/jit/importer.cpp index 6aba974..6062511 100644 --- a/src/coreclr/src/jit/importer.cpp +++ b/src/coreclr/src/jit/importer.cpp @@ -3821,6 +3821,8 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis, GenTree* ptrToSpan = impPopStack().val; GenTree* indexClone = nullptr; GenTree* ptrToSpanClone = nullptr; + assert(varTypeIsIntegral(index)); + assert(ptrToSpan->TypeGet() == TYP_BYREF); #if defined(DEBUG) if (verbose) diff --git a/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_20958/GitHub_20958.cs b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_20958/GitHub_20958.cs new file mode 100644 index 0000000..74ea90e --- /dev/null +++ b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_20958/GitHub_20958.cs @@ -0,0 +1,61 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +// This test was extracted from the Indexer tests in the corefx System.Memory.Tests. +// The JIT was trying to expand the form of indexer that takes a range, but was not +// correctly expanding it, as it was expecting only the scalar index form. + +public class GitHub_20958 +{ + + public static int IndexerWithRangeTest() + { + int returnVal = 100; + + ReadOnlySpan span = "Hello".AsSpan(); + ReadOnlySpan sliced = span[Range.Create(new Index(1, fromEnd: false), new Index(1, fromEnd: true))]; + if (span.Slice(1, 3) != sliced) + { + returnVal = -1; + } + try + { + ReadOnlySpan s = "Hello".AsSpan()[Range.Create(new Index(1, fromEnd: true), new Index(1, fromEnd: false))]; + returnVal = -1; + } + catch (ArgumentOutOfRangeException) + { + } + catch (Exception e) + { + returnVal = -1; + } + Span span1 = new Span(new char[] { 'H', 'e', 'l', 'l', 'o' }); + Span sliced1 = span1[Range.Create(new Index(2, fromEnd: false), new Index(1, fromEnd: true))]; + if (span1.Slice(2, 2) != sliced1) + { + returnVal = -1; + } + try + { + Span s = new Span(new char[] { 'H', 'i' })[Range.Create(new Index(0, fromEnd: true), new Index(1, fromEnd: false))]; + returnVal = -1; + } + catch (ArgumentOutOfRangeException) + { + } + catch (Exception e) + { + returnVal = -1; + } + return returnVal; + } + + public static int Main() + { + return IndexerWithRangeTest(); + } +} diff --git a/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_20958/GitHub_20958.csproj b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_20958/GitHub_20958.csproj new file mode 100644 index 0000000..d86ed9f --- /dev/null +++ b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_20958/GitHub_20958.csproj @@ -0,0 +1,17 @@ + + + + + Release + AnyCPU + $(MSBuildProjectName) + Exe + + True + + + + + + + \ No newline at end of file -- 2.7.4