Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NativeAOT] Fix Activator.CreateInstance for shared generic structs #101021

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static partial class Activator
else
{
t = default!;
RawCalliHelper.Call(defaultConstructor, ref Unsafe.As<T, byte>(ref t));
((delegate* <ref byte, void>)defaultConstructor)(ref Unsafe.As<T, byte>(ref t));
jkotas marked this conversation as resolved.
Show resolved Hide resolved

// Debugger goo so that stepping in works. Only affects debug info generation.
// The call gets optimized away.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,19 @@ internal override IntPtr Create(TypeBuilder builder)
{
IntPtr result = TypeLoaderEnvironment.TryGetDefaultConstructorForType(Type);

if (result != IntPtr.Zero)
{
if (Type.IsValueType)
Environment.FailFast("Here!");

if (result == IntPtr.Zero)
// TODO
// result = TypeLoaderEnvironment.Instance.ConvertUnboxingFunctionPointerToUnderlyingNonUnboxingPointer(result, new RuntimeTypeHandle(pElementEEType));
}
else
{
result = RuntimeAugments.GetFallbackDefaultConstructor();
}

return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public ISymbolNode ComputeConstantLookup(ReadyToRunHelperId lookupKind, object t
{
var type = (TypeDesc)targetOfLookup;
MethodDesc ctor = GetConstructorForCreateInstanceIntrinsic(type);
return NodeFactory.CanonicalEntrypoint(ctor);
return type.IsValueType ? NodeFactory.ExactCallableAddress(ctor) : NodeFactory.CanonicalEntrypoint(ctor);
}
case ReadyToRunHelperId.ObjectAllocator:
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ public override ISymbolNode GetTarget(NodeFactory factory, GenericLookupResultCo
{
TypeDesc instantiatedType = _type.GetNonRuntimeDeterminedTypeFromRuntimeDeterminedSubtypeViaSubstitution(dictionary.TypeInstantiation, dictionary.MethodInstantiation);
MethodDesc defaultCtor = Compilation.GetConstructorForCreateInstanceIntrinsic(instantiatedType);
return factory.CanonicalEntrypoint(defaultCtor);
return instantiatedType.IsValueType ? factory.ExactCallableAddress(defaultCtor) : factory.CanonicalEntrypoint(defaultCtor);
}

public override void AppendMangledName(NameMangler nameMangler, Utf8StringBuilder sb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,9 @@ private void ImportCall(ILOpcode opcode, int token)
}
else
{
MethodDesc ctor = Compilation.GetConstructorForCreateInstanceIntrinsic(method.Instantiation[0]);
_dependencies.Add(_factory.CanonicalEntrypoint(ctor), reason);
TypeDesc type = method.Instantiation[0];
MethodDesc ctor = Compilation.GetConstructorForCreateInstanceIntrinsic(type);
_dependencies.Add(type.IsValueType ? _factory.ExactCallableAddress(ctor) : _factory.CanonicalEntrypoint(ctor), reason);
}

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ public void CreateInstanceT_StructWithoutDefaultConstructor_InvokesConstructor()
public void CreateInstanceT_StructWithDefaultConstructorThatThrows_ThrowsTargetInvocationException() =>
Assert.Throws<TargetInvocationException>(() => Activator.CreateInstance<StructWithDefaultConstructorThatThrows>());

[Fact]
public void CreateInstanceT_GenericTypes()
{
TestGenericClassWithDefaultConstructor<string>();
TestGenericClassWithDefaultConstructor<int>();

TestGenericStructWithDefaultConstructor<string>();
TestGenericStructWithDefaultConstructor<int>();

void TestGenericClassWithDefaultConstructor<T>()
=> Assert.Equal(typeof(T), Activator.CreateInstance<GenericClassWithDefaultConstructor<T>>().TypeOfT);

void TestGenericStructWithDefaultConstructor<T>()
=> Assert.Equal(typeof(T), Activator.CreateInstance<GenericStructWithDefaultConstructor<T>>().TypeOfT);
}

private interface IInterface
{
}
Expand Down Expand Up @@ -96,5 +112,27 @@ private class ClassWithDefaultConstructorThatThrows
public ClassWithDefaultConstructorThatThrows() =>
throw new Exception();
}

public class GenericClassWithDefaultConstructor<T>
{
public GenericClassWithDefaultConstructor() =>
TypeOfT = typeof(T);

public Type TypeOfT { get; }
}

public struct StructWithDefaultConstructorThatThrows
{
public StructWithDefaultConstructorThatThrows() =>
throw new Exception();
}

public struct GenericStructWithDefaultConstructor<T>
{
public GenericStructWithDefaultConstructor() =>
TypeOfT = typeof(T);

public Type TypeOfT { get; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@
ret
}
}

.class public sequential sealed StructWithDefaultConstructorThatThrows
jkotas marked this conversation as resolved.
Show resolved Hide resolved
extends [System.Runtime]System.ValueType
{
.size 1

.method public hidebysig specialname rtspecialname instance void .ctor () cil managed
{
newobj instance void [System.Runtime]System.Exception::.ctor()
throw
}
}
}

.class public sequential sealed '.GlobalStructStartingWithDot'
Expand Down
14 changes: 14 additions & 0 deletions src/tests/nativeaot/SmokeTests/DynamicGenerics/activation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ public override string ToString()
return memberVar;
}
}
struct StructToString<T>
{
string memberVar;
public StructToString()
{
memberVar = typeof(T).Name;
}
public override string ToString()
{
return memberVar;
}
}
class SomeUnrealtedType<T>
{
string memberVar;
Expand Down Expand Up @@ -195,5 +207,7 @@ public static void TestDefaultCtorInLazyGenerics()
AllocViaGVMBase typeWithGVM = AllocViaGVMDerived.Alloc();
Assert.AreEqual("ToStringIsInteresting1", typeWithGVM.Alloc<ToStringIsInteresting1>().ToString());
Assert.AreEqual("ToStringIsInteresting2", typeWithGVM.Alloc<ToStringIsInteresting2>().ToString());
Assert.AreEqual("ToStringIsInteresting1", typeWithGVM.Alloc<StructToString<ToStringIsInteresting1>>().ToString());
Assert.AreEqual("ToStringIsInteresting2", typeWithGVM.Alloc<StructToString<ToStringIsInteresting2>>().ToString());
}
}
Loading