dotnet/runtime

ILVerification fails unexpected on delegate construction with instance method

Open

#96,695 建立於 2024年1月9日

在 GitHub 查看
 (4 留言) (0 反應) (0 負責人)C# (5,445 fork)batch import
area-Tools-ILVerificationhelp wanted

倉庫指標

Star
 (17,886 star)
PR 合併指標
 (平均合併 12天 11小時) (30 天內合併 661 個 PR)

描述

As part of rolsyn PR https://github.com/dotnet/roslyn/pull/71544, we're updating to ILVerify version 8.0.0-rtm.23523.3 (from 7.0.0-alpha.1.22060.1).

The test below shows the verification failure. The error is "[Main]: Unrecognized arguments for delegate .ctor. { Offset = 0xe }".

As far as I can tell the problem is in IsDelegateAssignable which ends up doing an IsAssignable check for assigning an int32 (which comes from the firstArg parameter and represents the stack value) to object.

Please let us know if the problem turns out to be with the code generated by roslyn (see IL captured in test below).

        [Fact]
        public void StructDelegate()
        {
            var source = @"
using System;
class Program
{
    static void Main()
    {
        int x = 42;
        Func<string> f = x.ToString;
        Console.Write(f.Invoke());
    }
}";
            var verifier = CompileAndVerify(source, expectedOutput: "42",
                verify: Verification.FailsILVerify with { ILVerifyMessage = "[Main]: Unrecognized arguments for delegate .ctor. { Offset = 0xe }" });

            verifier.VerifyIL("Program.Main", """
{
  // Code size       30 (0x1e)
  .maxstack  2
  IL_0000:  ldc.i4.s   42
  IL_0002:  box        "int"
  IL_0007:  dup
  IL_0008:  ldvirtftn  "string object.ToString()"
  IL_000e:  newobj     "System.Func<string>..ctor(object, System.IntPtr)"
  IL_0013:  callvirt   "string System.Func<string>.Invoke()"
  IL_0018:  call       "void System.Console.Write(string)"
  IL_001d:  ret
}
""");
        }

貢獻者指南