// We have the canonical version of the method - find the runtime determined version.
// This is simplified because we know the method is on a valuetype.
Debug.Assert(targetMethod.OwningType.IsValueType);
+
+ if (forceUseRuntimeLookup)
+ {
+ // The below logic would incorrectly resolve the lookup into the first match we found,
+ // but there was a compile-time ambiguity due to shared code. The correct fix should
+ // use the ConstrainedMethodUseLookupResult dictionary entry so that the exact
+ // dispatch can be computed with the help of the generic dictionary.
+ // We fail the compilation here to avoid bad codegen. This is not actually an invalid program.
+ // https://github.com/dotnet/runtimelab/issues/1431
+ ThrowHelper.ThrowInvalidProgramException();
+ }
+
MethodDesc targetOfLookup;
if (_constrained.IsRuntimeDeterminedType)
targetOfLookup = _compilation.TypeSystemContext.GetMethodForRuntimeDeterminedType(targetMethod.GetTypicalMethodDefinition(), (RuntimeDeterminedType)_constrained);