Repository: dotnet/runtime
File: src/coreclr/vm/genericdict.cpp (C++)

Code before:
                _ASSERTE(nonExpansive);
                return NULL;
            }
            IfFailThrow(ptr.SkipExactlyOne());

            FALLTHROUGH;
        }

        case TypeHandleSlot:
        {
            TypeHandle th = ptr.GetTypeHandleThrowing(
                pLookupModule,
                &typeContext,
                (nonExpansive ? ClassLoader::DontLoadTypes : ClassLoader::LoadTypes),
                CLASS_LOADED,
                FALSE,
                NULL,
                pZapSigContext);
            if (th.IsNull())
            {
                _ASSERTE(nonExpansive);
                return NULL;
            }
            IfFailThrow(ptr.SkipExactlyOne());

            PTR_MethodTable pMT = th.IsByRef() ? th.GetTypeParam().GetMethodTable() : th.GetMethodTable();

            if (!declaringType.IsNull())
            {
                th = pMT->GetMethodTableMatchingParentClass(declaringType.AsMethodTable());
                pMT = th.GetMethodTable();
            }

            pMT->EnsureInstanceActive();

            result = (CORINFO_GENERIC_HANDLE)th.AsPtr();
            break;
        }

        case ConstrainedMethodEntrySlot:
        {
            constraintType = ptr.GetTypeHandleThrowing(
                pLookupModule,
                &typeContext,
                (nonExpansive ? ClassLoader::DontLoadTypes : ClassLoader::LoadTypes),
                CLASS_LOADED,
                FALSE,
                NULL,
                pZapSigContext);
            if (constraintType.IsNull())
            {

Review comment (bug):
I do not think this is the right fix. It is just going to crash if the method does something with T, e.g. change the method to `private void M() { Console.WriteLine(typeof(T)); }` to see the problem - I expect that it is going to crash even with this fix.

I think the fix should be in the unsafe accessor emitter.

Code after:
                _ASSERTE(nonExpansive);
                return NULL;
            }
            IfFailThrow(ptr.SkipExactlyOne());

            FALLTHROUGH;
        }

        case TypeHandleSlot:
        {
            TypeHandle th = ptr.GetTypeHandleThrowing(
                pLookupModule,
                &typeContext,
                (nonExpansive ? ClassLoader::DontLoadTypes : ClassLoader::LoadTypes),
                CLASS_LOADED,
                FALSE,
                NULL,
                pZapSigContext);
            if (th.IsNull())
            {
                _ASSERTE(nonExpansive);
                return NULL;
            }
            IfFailThrow(ptr.SkipExactlyOne());

            if (!declaringType.IsNull())
            {
                th = th.GetMethodTable()->GetMethodTableMatchingParentClass(declaringType.AsMethodTable());
            }

            if (!th.IsTypeDesc())
            {
                MethodTable* pMT = th.AsMethodTable();
                _ASSERTE(pMT != NULL);
                pMT->EnsureInstanceActive();
            }

            result = (CORINFO_GENERIC_HANDLE)th.AsPtr();
            break;
        }

        case ConstrainedMethodEntrySlot:
        {
            constraintType = ptr.GetTypeHandleThrowing(
                pLookupModule,
                &typeContext,
                (nonExpansive ? ClassLoader::DontLoadTypes : ClassLoader::LoadTypes),
                CLASS_LOADED,
                FALSE,
                NULL,
                pZapSigContext);