Skip to content

Commit

Permalink
Emit instantiated types as external references if possible (#105816)
Browse files Browse the repository at this point in the history
Fixes #105397.

The repro case hits an interesting problem in native layout - we emit the `IEnumerable<IEnumerable<double?>>` type as a constructed type, however the components of it are only generated as necessary. Because native layout expresses it as a decomposed instantiation, we're not able to find the type because the component of it is not constructed and we don't really keep track of those.

This can be fixed by simply not generating types as composed out of various components.
  • Loading branch information
MichalStrehovsky authored Aug 2, 2024
1 parent 30b34e6 commit e60db07
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public EETypeNode(NodeFactory factory, TypeDesc type)
else if (type.IsCanonicalSubtype(CanonicalFormKind.Any))
Debug.Assert((this is CanonicalEETypeNode) || (this is NecessaryCanonicalEETypeNode));

Debug.Assert(!type.IsGenericParameter);
Debug.Assert(!type.IsRuntimeDeterminedSubtype);
_type = type;
_optionalFieldsNode = new EETypeOptionalFieldsNode(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ internal sealed class NativeLayoutMethodLdTokenVertexNode : NativeLayoutMethodEn
protected override string GetName(NodeFactory factory) => "NativeLayoutMethodLdTokenVertexNode_" + factory.NameMangler.GetMangledMethodName(_method);

public NativeLayoutMethodLdTokenVertexNode(NodeFactory factory, MethodDesc method)
: base(factory, method, 0)
: base(factory, method, method.IsRuntimeDeterminedExactMethod || method.IsGenericMethodDefinition ? 0 : MethodEntryFlags.CreateInstantiatedSignature)
{
}

Expand Down
26 changes: 26 additions & 0 deletions src/tests/nativeaot/SmokeTests/UnitTests/Generics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal static int Run()
Test99198Regression.Run();
Test102259Regression.Run();
Test104913Regression.Run();
Test105397Regression.Run();
TestInvokeMemberCornerCaseInGenerics.Run();
TestRefAny.Run();
TestNullableCasting.Run();
Expand Down Expand Up @@ -3629,6 +3630,31 @@ public static void Run()
}
}

class Test105397Regression
{
interface IEnumerable<T> { }

interface ITest<TResult>
{
TReturn UsingDatabaseResult<TState, TReturn>(TState state, Func<TResult, TState, TReturn> @using);
}
class Test<TResult> : ITest<TResult>
{
public TReturn UsingDatabaseResult<TState, TReturn>(TState state, Func<TResult, TState, TReturn> @using)
{
return default;
}
}

struct GenStruct<T> { }

public static void Run()
{
ITest<object> t = new Test<object>();
t.UsingDatabaseResult<IEnumerable<IEnumerable<GenStruct<double>>>, int>(null, (x, y) => 1);
}
}

class TestInvokeMemberCornerCaseInGenerics
{
class Generic<T>
Expand Down

0 comments on commit e60db07

Please sign in to comment.