ILVerification fails unexpected on delegate construction with instance method
#96.695 geöffnet am 9. Jan. 2024
Repository-Metriken
- Stars
- (17.886 Stars)
- PR-Merge-Metriken
- (Durchschn. Merge 12T 11h) (661 gemergte PRs in 30 T)
Beschreibung
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
}
""");
}