Skip to content

Commit

Permalink
Include method instantiation MethodTables in compilation (#66984)
Browse files Browse the repository at this point in the history
* Include method instantiation MethodTables in compilation

We'll need to load the `MethodTable` so that we can search for it in GVM tables.

* Regression test
  • Loading branch information
MichalStrehovsky authored Mar 22, 2022
1 parent 970a7e2 commit 96ef47b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ private IEnumerable<DependencyListEntry> GetGenericVirtualMethodDependencies(Nod
{
var dependencies = (DependencyList)base.GetStaticDependencies(factory);

dependencies.Add(factory.GVMDependencies(_method.GetCanonMethodTarget(CanonicalFormKind.Specific)), "Potential generic virtual method call");
MethodDesc canonMethod = _method.GetCanonMethodTarget(CanonicalFormKind.Specific);

dependencies.Add(factory.GVMDependencies(canonMethod), "Potential generic virtual method call");

// Variant generic virtual method calls at runtime might need to build the concrete version of the
// type we could be dispatching on to find the appropriate GVM entry.
Expand All @@ -262,6 +264,11 @@ private IEnumerable<DependencyListEntry> GetGenericVirtualMethodDependencies(Nod
GenericTypesTemplateMap.GetTemplateTypeDependencies(ref dependencies, factory, _method.OwningType.ConvertToCanonForm(CanonicalFormKind.Specific));
}

foreach (TypeDesc instArg in canonMethod.Instantiation)
{
dependencies.Add(factory.MaximallyConstructableType(instArg), "Type we need to look up for GVM dispatch");
}

return dependencies;
}

Expand Down
21 changes: 21 additions & 0 deletions src/tests/nativeaot/SmokeTests/UnitTests/Generics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal static int Run()
TestGenericRecursionFromNpgsql.Run();
TestRecursionInGenericVirtualMethods.Run();
TestRecursionThroughGenericLookups.Run();
TestGvmLookupDependency.Run();
#if !CODEGEN_CPP
TestNullableCasting.Run();
TestVariantCasting.Run();
Expand Down Expand Up @@ -3147,4 +3148,24 @@ public static void Run()
new RangeHandler<object>().Write(default);
}
}

static class TestGvmLookupDependency
{
struct SmallCat<T> { }

interface ITechnique
{
void CatSlaps<T>() { /* Cannot reference T or it stops testing the thing it should */ }
}

struct Technique : ITechnique { }

static void CatConcepts<T, U>() where T : ITechnique => default(T).CatSlaps<SmallCat<U>>();

public static void Run()
{
CatConcepts<Technique, int>();
CatConcepts<Technique, object>();
}
}
}

0 comments on commit 96ef47b

Please sign in to comment.