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

Fix throwing exception when calling RunClassConstructor on a generic type with a static constructor #105513

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions src/coreclr/vm/methodtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3768,6 +3768,9 @@ void MethodTable::CheckRunClassInitThrowing()
if (IsSharedByGenericInstantiations())
return;

// For back-compat reasons, act like it has already been initialized
steveisok marked this conversation as resolved.
Show resolved Hide resolved
jkotas marked this conversation as resolved.
Show resolved Hide resolved
_ASSERTE(!ContainsGenericVariables());

EnsureStaticDataAllocated();
DoRunClassInitThrowing();
}
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/vm/reflectioninvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,8 @@ extern "C" void QCALLTYPE ReflectionInvocation_RunClassConstructor(QCall::TypeHa
return;

MethodTable *pMT = typeHnd.AsMethodTable();
if (pMT->IsClassInited())
// The ContainsGenericVariables check is to preserve back-compat where we assume the generic type is already initialized
if (pMT->IsClassInited() || pMT->ContainsGenericVariables())
return;

BEGIN_QCALL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public static void RunClassConstructor()
RuntimeTypeHandle t = typeof(HasCctor).TypeHandle;
RuntimeHelpers.RunClassConstructor(t);
Assert.Equal("Hello", HasCctorReceiver.S);
// Should not throw
RuntimeHelpers.RunClassConstructor(typeof(GenericHasCctor<>).TypeHandle);
steveisok marked this conversation as resolved.
Show resolved Hide resolved
return;
jkotas marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
Loading