diff --git a/src/coreclr/src/jit/lclvars.cpp b/src/coreclr/src/jit/lclvars.cpp index c4f8da0bc3c4b4..7de0f8682cb027 100644 --- a/src/coreclr/src/jit/lclvars.cpp +++ b/src/coreclr/src/jit/lclvars.cpp @@ -2666,7 +2666,7 @@ void Compiler::lvaSetClass(unsigned varNum, CORINFO_CLASS_HANDLE clsHnd, bool is // Notes: // Preferentially uses the tree's type, when available. Since not all // tree kinds can track ref types, the stack type is used as a -// fallback. +// fallback. If there is no stack type, then the class is set to object. void Compiler::lvaSetClass(unsigned varNum, GenTree* tree, CORINFO_CLASS_HANDLE stackHnd) { @@ -2682,6 +2682,10 @@ void Compiler::lvaSetClass(unsigned varNum, GenTree* tree, CORINFO_CLASS_HANDLE { lvaSetClass(varNum, stackHnd); } + else + { + lvaSetClass(varNum, impGetObjectClass()); + } } //------------------------------------------------------------------------ diff --git a/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_27923/GitHub_27923.cs b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_27923/GitHub_27923.cs new file mode 100644 index 00000000000000..9ac2283299c3e6 --- /dev/null +++ b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_27923/GitHub_27923.cs @@ -0,0 +1,34 @@ +// 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; +using System.Runtime.CompilerServices; + +class Writer +{ + public object Data { get; set; } + public int Position { get; set; } + + [MethodImpl(MethodImplOptions.NoInlining)] + Writer() + { + Data = new int[] { 100, -1, -2, -3 }; + Position = 4; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public static ArraySegment Test() + { + var writer = new Writer(); + object temp = writer.Data; + byte[] data = Unsafe.As(ref temp); + return new ArraySegment(data, 0, writer.Position); + } + + public static int Main() + { + var x = Test(); + return x[0]; + } +} diff --git a/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_27923/GitHub_27923.csproj b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_27923/GitHub_27923.csproj new file mode 100644 index 00000000000000..1100f420532dc8 --- /dev/null +++ b/src/coreclr/tests/src/JIT/Regression/JitBlue/GitHub_27923/GitHub_27923.csproj @@ -0,0 +1,10 @@ + + + Exe + None + True + + + + +