Match ILScanner with CorInfoImpl (#81683)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Tue, 7 Feb 2023 00:03:30 +0000 (09:03 +0900)
committerGitHub <noreply@github.com>
Tue, 7 Feb 2023 00:03:30 +0000 (09:03 +0900)
ILScanner and the JitInterface have to be in sync wrt exceptions throw or there are problems. Fixes one of the Pri0 tests.

src/coreclr/tools/aot/ILCompiler.Compiler/IL/ILImporter.Scanner.cs

index 7a8cb22..7c6f93b 100644 (file)
@@ -501,6 +501,18 @@ namespace Internal.IL
                 // 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);