Skip to content

Commit 5f9d281

Browse files
authored
Fix newobj handler for generic constructors (#3238)
***NO_CI***
1 parent 9892998 commit 5f9d281

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/CLR/Core/Interpreter.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,21 +2464,34 @@ HRESULT CLR_RT_Thread::Execute_IL(CLR_RT_StackFrame &stackArg)
24642464
}
24652465
top->SetObjectReference(nullptr);
24662466

2467-
// Stack: ... <null> <arg1> <arg2> ... <argN> -> ...
2468-
// ^
2469-
// Top points here.
2467+
// For constructors on generic classes, we need to use the class's generic type
2468+
// from the calling context, not the method's generic type.
2469+
// The calleeInst.genericType might point to an open generic from the MethodRef,
2470+
// but we need the closed generic from the caller's context (stack->m_call.genericType).
2471+
const CLR_RT_TypeSpec_Index *genericTypeForContext = nullptr;
2472+
2473+
// Prefer the caller's generic type if available and valid
2474+
if (stack->m_call.genericType != nullptr && NANOCLR_INDEX_IS_VALID(*stack->m_call.genericType))
2475+
{
2476+
genericTypeForContext = stack->m_call.genericType;
2477+
}
2478+
// Fallback to the callee's generic type if caller's is not available
2479+
else if (calleeInst.genericType != nullptr && NANOCLR_INDEX_IS_VALID(*calleeInst.genericType))
2480+
{
2481+
genericTypeForContext = calleeInst.genericType;
2482+
}
24702483

2471-
if (calleeInst.genericType == nullptr)
2484+
if (genericTypeForContext == nullptr)
24722485
{
24732486
NANOCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObject(top[0], cls));
24742487
}
24752488
else
24762489
{
2477-
CLR_RT_TypeSpec_Instance calleeInstGenericType;
2478-
calleeInstGenericType.InitializeFromIndex(*calleeInst.genericType);
2490+
CLR_RT_TypeSpec_Instance genericTypeInstance;
2491+
genericTypeInstance.InitializeFromIndex(*genericTypeForContext);
24792492

24802493
NANOCLR_CHECK_HRESULT(
2481-
g_CLR_RT_ExecutionEngine.NewObject(top[0], cls, &calleeInstGenericType));
2494+
g_CLR_RT_ExecutionEngine.NewObject(top[0], cls, &genericTypeInstance));
24822495
}
24832496

24842497
//

0 commit comments

Comments
 (0)