Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8604,18 +8604,18 @@ bool CEEInfo::resolveVirtualMethodHelper(CORINFO_DEVIRTUALIZATION_INFO * info)

if (pObjMT->IsArray())
{
if (pBaseMT->IsSharedByGenericInstantiations())
{
info->detail = CORINFO_DEVIRTUALIZATION_FAILED_CANON;
return false;
}

// Does the array implicitly implement this interface?
//
isArrayImplicitInterface = pBaseMT->HasInstantiation() && IsImplicitInterfaceOfSZArray(pBaseMT);

if (!isArrayImplicitInterface)
{
if (pBaseMT->IsSharedByGenericInstantiations())
{
info->detail = CORINFO_DEVIRTUALIZATION_FAILED_CANON;
return false;
}

// Ensure we can cast the array to the interface type
//
if (!TypeHandle(pObjMT).CanCastTo(TypeHandle(pBaseMT)))
Expand All @@ -8641,15 +8641,12 @@ bool CEEInfo::resolveVirtualMethodHelper(CORINFO_DEVIRTUALIZATION_INFO * info)
{
_ASSERTE(pObjMT->IsArray());

// We cannot devirtualize unless we know the exact array element type
//
TypeHandle elemType = pObjMT->GetArrayElementTypeHandle();
if (elemType.IsCanonicalSubtype())
{
info->detail = CORINFO_DEVIRTUALIZATION_FAILED_LOOKUP;
return false;
}
pDevirtMD = GetActualImplementationForArrayGenericIListOrIReadOnlyListMethod(pBaseMD, elemType);
// The instantiation we want is based on the interface element type, not the
// array element type.
TypeHandle resultElemType = pBaseMT->GetInstantiation()[0];
// We should have ruled this out above.
_ASSERTE(!resultElemType.IsCanonicalSubtype());
pDevirtMD = GetActualImplementationForArrayGenericIListOrIReadOnlyListMethod(pBaseMD, resultElemType);
}
else if (pObjMT->IsSharedByGenericInstantiations() || pBaseMT->IsSharedByGenericInstantiations())
{
Expand Down
95 changes: 95 additions & 0 deletions src/tests/JIT/Regression/JitBlue/Runtime_120270/Runtime_120270.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Xunit;

class Base{}
class Derived : Base {}

public class Runtime_120270
{
[Fact]
public static int TestEntryPoint()
{
int i = 0;
bool failed = false;
try
{
while (i < 100_000)
{
i++;
var values = new[] { DayOfWeek.Saturday }.Cast<int>();
foreach (var value in values)
{
}
}
}
catch (Exception ex)
{
Console.WriteLine(i);
Console.WriteLine(ex);
failed = true;
}

return failed ? -1 : 100;
}

[Fact]
public static int TestEntryPoint2()
{
bool failed = false;
try
{
Foo<Base>([new Derived()]);
}
catch (Exception ex)
{
Console.WriteLine(ex);
failed = true;
}
return failed ? -1 : 100;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void Foo<T>(object[] x)
{
int i = 0;
while (i < 100_000)
{
i++;
var values = x.Cast<T>();
foreach (var value in values)
{
}
}
}

[Fact]
public static int TestEntryPoint3()
{
for (int i = 0; i < 100_000; i++)
{
Derived[] d = [new Derived()];
IEnumerable<Base> e = d;
IEnumerator<Base> en = e.GetEnumerator();
string s = en.GetType().ToString();

if (i == 0)
{
Console.WriteLine($"Enumerator type: {s}");
}

if (!s.Equals("System.SZGenericArrayEnumerator`1[Base]"))
{
Console.WriteLine($"Enumerator type changed at {i} to {s}");
return -1;
}
}
return 100;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>
Loading